"use client"; import { useEffect, useState } from "react"; import { Loader2, Pencil } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Switch } from "@/components/ui/switch"; import { getAssociations, updateAssociation } from "@/lib/api"; import type { Association } from "@/lib/models"; export function EditAssociationDialog({ association, onSaved, }: { association: Association; onSaved: (association: Association) => void; }) { const [open, setOpen] = useState(false); const [saving, setSaving] = useState(false); const [name, setName] = useState(association.name); const [address, setAddress] = useState(association.address); const [vatNumber, setVatNumber] = useState(association.vatNumber ?? ""); const [runtsId, setRuntsId] = useState(association.runtsId ?? ""); const [nasFolderNumber, setNasFolderNumber] = useState(association.nasFolderNumber?.toString() ?? ""); const [isShown, setIsShown] = useState(association.isShown ?? true); useEffect(() => { if (open) { setName(association.name); setAddress(association.address); setVatNumber(association.vatNumber ?? ""); setRuntsId(association.runtsId ?? ""); setNasFolderNumber(association.nasFolderNumber?.toString() ?? ""); setIsShown(association.isShown ?? true); } }, [open, association]); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setSaving(true); try { if (nasFolderNumber.trim() !== "") { const folderNumber = Number(nasFolderNumber); const others = await getAssociations(); const duplicate = others.find( (a) => a.id !== association.id && a.nasFolderNumber === folderNumber ); if (duplicate) { const proceed = confirm( `Il numero cartella ${folderNumber} è già usato da "${duplicate.name}". Continuare comunque?` ); if (!proceed) { setSaving(false); return; } } } const updated = await updateAssociation(association.id!, { ...association, name, address, vatNumber, runtsId, nasFolderNumber: nasFolderNumber.trim() === "" ? undefined : Number(nasFolderNumber), isShown, }); onSaved(updated); setOpen(false); } catch (error) { console.error("Failed to update association:", error); alert("Impossibile salvare le modifiche"); } finally { setSaving(false); } }; return ( Modifica associazione
setName(e.target.value)} required />
setAddress(e.target.value)} required />
setVatNumber(e.target.value)} />
setRuntsId(e.target.value)} />
setNasFolderNumber(e.target.value)} placeholder="Usato per generare il codice di ratifica" />

Le associazioni non attive non compaiono nell'elenco

); }