"use client"; import { useEffect, useState } from "react"; import { Loader2 } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Checkbox } from "@/components/ui/checkbox"; import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import type { Associate } from "@/lib/models"; const ROLE_OPTIONS: { value: Associate["role"]; label: string }[] = [ { value: "PRESIDENT", label: "Presidente" }, { value: "VICE_PRESIDENT", label: "Vicepresidente" }, { value: "SECRETARY", label: "Segretario" }, { value: "ASSOCIATE", label: "Socio" }, ]; const EMPTY_ASSOCIATE: Associate = { name: "", surname: "", birthDate: "", address: "", fiscalCode: "", italianCitizen: true, role: "ASSOCIATE", residencePermitNumber: "", }; export function AssociateFormDialog({ open, onOpenChange, initialValue, onSubmit, }: { open: boolean; onOpenChange: (open: boolean) => void; initialValue?: Associate; onSubmit: (associate: Associate) => Promise | void; }) { const [value, setValue] = useState(initialValue ?? EMPTY_ASSOCIATE); const [saving, setSaving] = useState(false); useEffect(() => { if (open) { setValue(initialValue ?? EMPTY_ASSOCIATE); } }, [open, initialValue]); const update = (key: K, fieldValue: Associate[K]) => setValue((prev) => ({ ...prev, [key]: fieldValue })); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setSaving(true); try { await onSubmit(value); onOpenChange(false); } catch (error) { console.error("Failed to save associate:", error); alert("Impossibile salvare il socio"); } finally { setSaving(false); } }; return ( {initialValue ? "Modifica socio" : "Aggiungi socio"}
update("name", e.target.value)} required />
update("surname", e.target.value)} required />
update("birthDate", e.target.value)} required />
update("address", e.target.value)} required />
update("fiscalCode", e.target.value.toUpperCase())} required />
{!value.italianCitizen && (
update("residencePermitNumber", e.target.value)} />
)}
update("italianCitizen", checked === true)} />
); }