"use client"; import * as React from "react"; import { IconChevronDown, IconChevronLeft, IconChevronRight, IconChevronsLeft, IconChevronsRight, IconDotsVertical, IconLayoutColumns, IconSearch, IconMail, IconPhone, } from "@tabler/icons-react"; import { ColumnDef, ColumnFiltersState, flexRender, getCoreRowModel, getFacetedRowModel, getFacetedUniqueValues, getFilteredRowModel, getPaginationRowModel, getSortedRowModel, SortingState, useReactTable, VisibilityState, } from "@tanstack/react-table"; import { Button } from "@/components/ui/button"; import { Checkbox } from "@/components/ui/checkbox"; import { DropdownMenu, DropdownMenuCheckboxItem, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; type Patient = { id: string; name: string; email: string; phone: string | null; dateOfBirth: Date | null; medicalHistory: string | null; createdAt: Date; appointmentsAsPatient: Array<{ id: string; status: string; }>; payments: Array<{ id: string; amount: number; }>; }; const columns: ColumnDef[] = [ { id: "select", header: ({ table }) => (
table.toggleAllPageRowsSelected(!!value)} aria-label="Select all" />
), cell: ({ row }) => (
row.toggleSelected(!!value)} aria-label="Select row" />
), enableSorting: false, enableHiding: false, }, { accessorKey: "name", header: "Name", cell: ({ row }) => {row.original.name}, enableHiding: false, }, { accessorKey: "contact", header: "Contact", cell: ({ row }) => (
{row.original.email}
{row.original.phone && (
{row.original.phone}
)}
), }, { accessorKey: "dateOfBirth", header: "Date of Birth", cell: ({ row }) => row.original.dateOfBirth ? new Date(row.original.dateOfBirth).toLocaleDateString() : "-", }, { accessorKey: "appointments", header: "Appointments", cell: ({ row }) => row.original.appointmentsAsPatient.length, }, { accessorKey: "totalSpent", header: () =>
Total Spent
, cell: ({ row }) => { const totalSpent = row.original.payments.reduce( (sum, payment) => sum + payment.amount, 0 ); return
₱{totalSpent.toFixed(2)}
; }, }, { accessorKey: "createdAt", header: "Joined", cell: ({ row }) => new Date(row.original.createdAt).toLocaleDateString(), }, { id: "actions", cell: () => ( View Details Edit Profile View History Delete ), }, ]; type AdminPatientsTableProps = { patients: Patient[]; }; export function AdminPatientsTable({ patients }: AdminPatientsTableProps) { const [rowSelection, setRowSelection] = React.useState({}); const [columnVisibility, setColumnVisibility] = React.useState({}); const [columnFilters, setColumnFilters] = React.useState( [] ); const [sorting, setSorting] = React.useState([]); const [pagination, setPagination] = React.useState({ pageIndex: 0, pageSize: 10, }); const table = useReactTable({ data: patients, columns, state: { sorting, columnVisibility, rowSelection, columnFilters, pagination, }, enableRowSelection: true, onRowSelectionChange: setRowSelection, onSortingChange: setSorting, onColumnFiltersChange: setColumnFilters, onColumnVisibilityChange: setColumnVisibility, onPaginationChange: setPagination, getCoreRowModel: getCoreRowModel(), getFilteredRowModel: getFilteredRowModel(), getPaginationRowModel: getPaginationRowModel(), getSortedRowModel: getSortedRowModel(), getFacetedRowModel: getFacetedRowModel(), getFacetedUniqueValues: getFacetedUniqueValues(), }); return (
table.getColumn("name")?.setFilterValue(event.target.value) } className="pl-8" />
{table .getAllColumns() .filter( (column) => typeof column.accessorFn !== "undefined" && column.getCanHide() ) .map((column) => { return ( column.toggleVisibility(!!value) } > {column.id} ); })}
{/* Bulk Actions Toolbar */} {table.getFilteredSelectedRowModel().rows.length > 0 && (
{table.getFilteredSelectedRowModel().rows.length} selected
Export Selected Add to Group Send Appointment Reminder Delete Selected
)}
{table.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header) => { return ( {header.isPlaceholder ? null : flexRender( header.column.columnDef.header, header.getContext() )} ); })} ))} {table.getRowModel().rows?.length ? ( table.getRowModel().rows.map((row) => ( {row.getVisibleCells().map((cell) => ( {flexRender( cell.column.columnDef.cell, cell.getContext() )} ))} )) ) : ( No patients found. )}
{table.getFilteredSelectedRowModel().rows.length} of{" "} {table.getFilteredRowModel().rows.length} row(s) selected.
Page {table.getState().pagination.pageIndex + 1} of{" "} {table.getPageCount()}
); }