"use client"; import Link from "next/link"; import type { ColumnDef } from "@tanstack/react-table"; import { Button } from "~/components/ui/button"; import { DataTable, DataTableColumnHeader } from "~/components/ui/data-table"; import { UserPlus, Pencil, Trash2 } from "lucide-react"; import { useState } from "react"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "~/components/ui/dialog"; import { api } from "~/trpc/react"; import { toast } from "sonner"; // Type for client data interface Client { id: string; name: string; email: string | null; phone: string | null; addressLine1: string | null; addressLine2: string | null; city: string | null; state: string | null; postalCode: string | null; country: string | null; createdById: string; createdAt: Date; updatedAt: Date | null; } interface ClientsDataTableProps { clients: Client[]; } const formatAddress = (client: Client) => { const parts = [ client.addressLine1, client.addressLine2, client.city, client.state, client.postalCode, ].filter(Boolean); return parts.join(", ") || "—"; }; export function ClientsDataTable({ clients: initialClients, }: ClientsDataTableProps) { const [clients, setClients] = useState(initialClients); const [clientToDelete, setClientToDelete] = useState(null); const utils = api.useUtils(); const deleteClientMutation = api.clients.delete.useMutation({ onSuccess: () => { toast.success("Client deleted successfully"); setClients(clients.filter((c) => c.id !== clientToDelete?.id)); setClientToDelete(null); void utils.clients.getAll.invalidate(); }, onError: (error) => { toast.error(`Failed to delete client: ${error.message}`); }, }); const handleDelete = () => { if (!clientToDelete) return; deleteClientMutation.mutate({ id: clientToDelete.id }); }; const columns: ColumnDef[] = [ { accessorKey: "name", header: ({ column }) => ( ), cell: ({ row }) => { const client = row.original; return (

{client.name}

{client.email || "—"}

); }, }, { accessorKey: "phone", header: ({ column }) => ( ), cell: ({ row }) => row.original.phone || "—", meta: { headerClassName: "hidden md:table-cell", cellClassName: "hidden md:table-cell", }, }, { id: "address", header: "Address", cell: ({ row }) => formatAddress(row.original), meta: { headerClassName: "hidden lg:table-cell", cellClassName: "hidden lg:table-cell", }, }, { accessorKey: "createdAt", header: ({ column }) => ( ), cell: ({ row }) => { const date = row.getValue("createdAt") as Date; return new Intl.DateTimeFormat("en-US", { month: "short", day: "2-digit", year: "numeric", }).format(new Date(date)); }, meta: { headerClassName: "hidden xl:table-cell", cellClassName: "hidden xl:table-cell", }, }, { id: "actions", cell: ({ row }) => { const client = row.original; return (
); }, }, ]; return ( <> {/* Delete confirmation dialog */} !open && setClientToDelete(null)} > Are you sure? This action cannot be undone. This will permanently delete the client "{clientToDelete?.name}" and remove all associated data. ); }