JEISA-WEBAPP/src/components/customers/data-table.tsx
Dave 99e9d947cf feat(quotes,customers): address + VAT on quotes, 30-day validity, searchable customer
A quote should look like a document from a real company, so this adds
the customer details a quote needs, gives every quote a valid-until
date, and makes the customer picker usable once there are 20+ of them.

Customer details on quotes
- The `customers` collection only had a name, so the quote PDF's
  "Customer Information" block could only ever print a name. Added
  optional address, VAT number, email and phone fields.
- The PDF block now grows to fit whatever is on file and prints the
  VAT number and contact line, so a filled-in customer produces a
  professional quote.
- The quote details dialog shows the same details.
- The customer form gained the new fields, and "Edit customer" now
  works - it was a menu item that did nothing, so there was no way
  to add a VAT number to an existing customer.
- The customers table shows Address and VAT Number columns.

Valid until
- Quotes were created with an empty validUntil, so the PDF printed
  "Valid Until: N/A". The form now defaults it to 30 days after the
  quote date and keeps it in step when the date moves. The three
  existing quotes without one were backfilled the same way.

Searchable customer picker
- The customer dropdown listed every customer in a plain select with
  no way to add one that was missing. It is now a searchable combobox
  (by name, address or VAT number) that offers to create the customer
  when nothing matches, and creates it inline.

Sidebar
- Settings moved to a gear in the corner next to the app name, and
  Logs moved into Settings as a tab. The nav is now eight items with
  no grouping - grouping them would add a layer for no gain at this
  size. The old /logs link still lands on the logs tab.

Also fixes a real PDF bug: item descriptions were cut to 40 characters,
which is wider than the description column, so long names printed on
top of the SKU column. Descriptions and SKUs are now measured against
the real column width and truncated with an ellipsis.

Live PocketBase changes (see scripts/add-customer-fields.mjs):
- customers update and delete rules were null (superusers only), so a
  member could not edit the customer they had just added. They now
  match the other collections.
2026-10-21 04:45:58 +02:00

187 lines
No EOL
5.5 KiB
TypeScript

"use client"
import * as React from "react"
import {
ColumnDef,
ColumnFiltersState,
SortingState,
VisibilityState,
flexRender,
getCoreRowModel,
getFilteredRowModel,
getPaginationRowModel,
getSortedRowModel,
useReactTable,
} from "@tanstack/react-table"
import { Columns } from "lucide-react"
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@/components/ui/table"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
import {
DropdownMenu,
DropdownMenuCheckboxItem,
DropdownMenuContent,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"
interface DataTableProps<TData, TValue> {
columns: ColumnDef<TData, TValue>[]
data: TData[]
onDelete?: (id: string) => void
onEdit?: (customer: TData) => void
}
interface TableMeta<TData> {
onDelete?: (id: string) => void
onEdit?: (customer: TData) => void
}
export function DataTable<TData, TValue>({
columns,
data,
onDelete,
onEdit,
}: DataTableProps<TData, TValue>) {
const [sorting, setSorting] = React.useState<SortingState>([])
const [columnFilters, setColumnFilters] = React.useState<ColumnFiltersState>([])
const [columnVisibility, setColumnVisibility] = React.useState<VisibilityState>({})
const [rowSelection, setRowSelection] = React.useState({})
const table = useReactTable({
data,
columns,
getCoreRowModel: getCoreRowModel(),
getPaginationRowModel: getPaginationRowModel(),
onSortingChange: setSorting,
getSortedRowModel: getSortedRowModel(),
onColumnFiltersChange: setColumnFilters,
getFilteredRowModel: getFilteredRowModel(),
onColumnVisibilityChange: setColumnVisibility,
onRowSelectionChange: setRowSelection,
meta: { onDelete, onEdit },
state: {
sorting,
columnFilters,
columnVisibility,
rowSelection,
},
})
return (
<div>
<div className="flex items-center py-4">
<Input
placeholder="Filter by company name..."
value={(table.getColumn("companyName")?.getFilterValue() as string) ?? ""}
onChange={(event) =>
table.getColumn("companyName")?.setFilterValue(event.target.value)
}
className="max-w-sm"
/>
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="outline" size="icon" className="ml-auto">
<Columns className="h-4 w-4" />
<span className="sr-only">Toggle columns</span>
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
{table
.getAllColumns()
.filter(
(column) => column.getCanHide()
)
.map((column) => {
return (
<DropdownMenuCheckboxItem
key={column.id}
className="capitalize"
checked={column.getIsVisible()}
onCheckedChange={(value) =>
column.toggleVisibility(!!value)
}
>
{column.id}
</DropdownMenuCheckboxItem>
)
})}
</DropdownMenuContent>
</DropdownMenu>
</div>
<div className="rounded-md border bg-white">
<Table>
<TableHeader>
{table.getHeaderGroups().map((headerGroup) => (
<TableRow key={headerGroup.id}>
{headerGroup.headers.map((header) => {
return (
<TableHead key={header.id}>
{header.isPlaceholder
? null
: flexRender(
header.column.columnDef.header,
header.getContext()
)}
</TableHead>
)
})}
</TableRow>
))}
</TableHeader>
<TableBody>
{table.getRowModel().rows?.length ? (
table.getRowModel().rows.map((row) => (
<TableRow
key={row.id}
data-state={row.getIsSelected() && "selected"}
>
{row.getVisibleCells().map((cell) => (
<TableCell key={cell.id}>
{flexRender(cell.column.columnDef.cell, cell.getContext())}
</TableCell>
))}
</TableRow>
))
) : (
<TableRow>
<TableCell colSpan={columns.length} className="h-24 text-center">
No results.
</TableCell>
</TableRow>
)}
</TableBody>
</Table>
</div>
<div className="flex items-center justify-end space-x-2 py-4">
<div className="flex-1 text-sm text-muted-foreground">
{table.getFilteredSelectedRowModel().rows.length} of{" "}
{table.getFilteredRowModel().rows.length} row(s) selected.
</div>
<Button
variant="outline"
size="sm"
onClick={() => table.previousPage()}
disabled={!table.getCanPreviousPage()}
>
Previous
</Button>
<Button
variant="outline"
size="sm"
onClick={() => table.nextPage()}
disabled={!table.getCanNextPage()}
>
Next
</Button>
</div>
</div>
)
}