import { useState } from "react"; import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; import { apiRequest } from "@/lib/queryClient"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { useToast } from "@/hooks/use-toast"; import { DeleteConfirmationDialog } from "@/components/ui/deleteDialog"; import { Plus, Pencil, Trash2, X, Check } from "lucide-react"; const EMPTY_FORM = { name: "", phoneNumber: "" }; export function InsuranceContactCard() { const { toast } = useToast(); const queryClient = useQueryClient(); const [showForm, setShowForm] = useState(false); const [editingId, setEditingId] = useState(null); const [form, setForm] = useState(EMPTY_FORM); const [deleteTarget, setDeleteTarget] = useState(null); const { data: contacts = [], isLoading } = useQuery({ queryKey: ["/api/insurance-contacts"], queryFn: async () => { const res = await apiRequest("GET", "/api/insurance-contacts"); if (!res.ok) throw new Error("Failed to fetch"); return res.json(); }, }); const invalidate = () => queryClient.invalidateQueries({ queryKey: ["/api/insurance-contacts"] }); const createMutation = useMutation({ mutationFn: async (data) => { const res = await apiRequest("POST", "/api/insurance-contacts", data); if (!res.ok) { const e = await res.json().catch(() => null); throw new Error(e?.message || "Failed to save"); } return res.json(); }, onSuccess: () => { invalidate(); setShowForm(false); setForm(EMPTY_FORM); toast({ title: "Insurance contact added" }); }, onError: (e) => toast({ title: "Error", description: e.message, variant: "destructive" }), }); const updateMutation = useMutation({ mutationFn: async ({ id, data }) => { const res = await apiRequest("PUT", `/api/insurance-contacts/${id}`, data); if (!res.ok) { const e = await res.json().catch(() => null); throw new Error(e?.message || "Failed to update"); } return res.json(); }, onSuccess: () => { invalidate(); setEditingId(null); setForm(EMPTY_FORM); toast({ title: "Insurance contact updated" }); }, onError: (e) => toast({ title: "Error", description: e.message, variant: "destructive" }), }); const deleteMutation = useMutation({ mutationFn: async (id) => { const res = await apiRequest("DELETE", `/api/insurance-contacts/${id}`); if (!res.ok) throw new Error("Failed to delete"); }, onSuccess: () => { invalidate(); setDeleteTarget(null); toast({ title: "Insurance contact deleted" }); }, onError: (e) => toast({ title: "Error", description: e.message, variant: "destructive" }), }); const openAdd = () => { setEditingId(null); setForm(EMPTY_FORM); setShowForm(true); }; const openEdit = (c) => { setShowForm(false); setEditingId(c.id); setForm({ name: c.name, phoneNumber: c.phoneNumber ?? "" }); }; const cancelEdit = () => { setEditingId(null); setForm(EMPTY_FORM); }; const cancelAdd = () => { setShowForm(false); setForm(EMPTY_FORM); }; const handleSave = () => { if (!form.name.trim()) { toast({ title: "Name is required", variant: "destructive" }); return; } if (editingId !== null) { updateMutation.mutate({ id: editingId, data: form }); } else { createMutation.mutate(form); } }; const isSaving = createMutation.isPending || updateMutation.isPending; return (
{/* Header */}

Insurance Contacts

Phone numbers for insurance companies

{/* Add form */} {showForm && (

New Insurance Contact

setForm((f) => ({ ...f, name: e.target.value }))} className="h-9 text-sm"/>
setForm((f) => ({ ...f, phoneNumber: e.target.value }))} className="h-9 text-sm"/>
)} {/* Table */}
{isLoading ? () : contacts.length === 0 ? () : (contacts.map((c) => editingId === c.id ? ( /* Inline edit row */ ) : ( /* Display row */ )))}
Insurance Company Phone Number
Loading...
No insurance contacts yet. Click "Add Contact" to add one.
setForm((f) => ({ ...f, name: e.target.value }))} className="h-8 text-sm" placeholder="Company name"/> setForm((f) => ({ ...f, phoneNumber: e.target.value }))} className="h-8 text-sm" placeholder="Phone number"/>
{c.name} {c.phoneNumber || }
deleteTarget && deleteMutation.mutate(deleteTarget.id)} onCancel={() => setDeleteTarget(null)} entityName={deleteTarget?.name}/>
); }