"use client"; import { useState } from "react"; import Link from "next/link"; import { api } from "~/trpc/react"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "~/components/ui/card"; import { Button } from "~/components/ui/button"; import { Input } from "~/components/ui/input"; import { Label } from "~/components/ui/label"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "~/components/ui/dialog"; import { toast } from "sonner"; import { Mail, Phone, MapPin, Edit, Trash2, Eye, Plus, Search, } from "lucide-react"; export function ClientList() { const [searchTerm, setSearchTerm] = useState(""); const [deleteDialogOpen, setDeleteDialogOpen] = useState(false); const [clientToDelete, setClientToDelete] = useState(null); const { data: clients, isLoading, refetch } = api.clients.getAll.useQuery(); const deleteClient = api.clients.delete.useMutation({ onSuccess: () => { toast.success("Client deleted successfully"); void refetch(); setDeleteDialogOpen(false); setClientToDelete(null); }, onError: (error) => { toast.error(error.message || "Failed to delete client"); }, }); const filteredClients = clients?.filter( (client) => client.name.toLowerCase().includes(searchTerm.toLowerCase()) || client.email?.toLowerCase().includes(searchTerm.toLowerCase()), ) ?? []; const handleDelete = (clientId: string) => { setClientToDelete(clientId); setDeleteDialogOpen(true); }; const confirmDelete = () => { if (clientToDelete) { deleteClient.mutate({ id: clientToDelete }); } }; if (isLoading) { return (
{Array.from({ length: 3 }, (_, i: number) => (
))}
); } if (!clients || clients.length === 0) { return ( No Clients Yet Get started by adding your first client ); } return (
setSearchTerm(e.target.value)} className="h-12 pl-10" />
{filteredClients.map((client) => ( {client.name}
{client.email && (
{client.email}
)} {client.phone && (
{client.phone}
)} {(client.addressLine1 ?? client.city ?? client.state) && (
{client.addressLine1 &&
{client.addressLine1}
} {client.addressLine2 &&
{client.addressLine2}
} {(client.city ?? client.state ?? client.postalCode) && (
{[client.city, client.state, client.postalCode] .filter(Boolean) .join(", ")}
)} {client.country &&
{client.country}
}
)}
))}
Delete Client Are you sure you want to delete this client? This action cannot be undone.
); }