import * as React from "react"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter, } from "@/components/ui/dialog"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Spinner } from "@/components/ui/spinner"; import { AddressAutocomplete } from "@/components/ui/form-primitives"; // --------------------------------------------------------------------------- // Types // --------------------------------------------------------------------------- export interface CreateContactFormData { firstName: string; lastName: string; email: string; phone: string; address?: string; assignedStaffId?: string; } export interface StaffOption { id: string; displayName: string; } export interface CreateContactModalProps { open: boolean; onOpenChange: (open: boolean) => void; onSubmit: (data: CreateContactFormData) => void; isLoading?: boolean; /** When provided, renders an "Assign to" staff selector (Admin role). */ staffOptions?: StaffOption[]; className?: string; } // --------------------------------------------------------------------------- // Helpers // --------------------------------------------------------------------------- function Field({ id, label, required, children, }: { id: string; label: string; required?: boolean; children: React.ReactNode; }) { return (
{children}
); } // --------------------------------------------------------------------------- // Component // --------------------------------------------------------------------------- const EMPTY_FORM: CreateContactFormData = { firstName: "", lastName: "", email: "", phone: "", address: "", }; export function CreateContactModal({ open, onOpenChange, onSubmit, isLoading = false, staffOptions, className, }: CreateContactModalProps) { const [form, setForm] = React.useState(EMPTY_FORM); // Reset form when dialog opens React.useEffect(() => { if (open) setForm(EMPTY_FORM); }, [open]); function set(field: keyof CreateContactFormData, value: string) { setForm((prev) => ({ ...prev, [field]: value })); } const isValid = form.firstName.trim() !== "" && form.lastName.trim() !== "" && form.email.trim() !== ""; function handleSubmit(e: React.FormEvent) { e.preventDefault(); if (!isValid || isLoading) return; onSubmit(form); } return ( Add New Client
{/* Name row */}
set("firstName", e.target.value)} disabled={isLoading} autoComplete="given-name" /> set("lastName", e.target.value)} disabled={isLoading} autoComplete="family-name" />
{/* Email */} set("email", e.target.value)} disabled={isLoading} autoComplete="email" /> {/* Phone */} set("phone", e.target.value)} disabled={isLoading} autoComplete="tel" /> {/* Address */} set("address", v)} onSelect={(opt) => set("address", opt.label)} disabled={isLoading} /> {/* Staff selector — Admin role only */} {staffOptions && staffOptions.length > 0 && ( )}
); }