import { useState } from "react"; import { createFileRoute } from "@tanstack/react-router"; import { CalendarPlus, Loader2, Pencil, Trash2 } from "lucide-react"; import { toast } from "sonner"; import { cn } from "@/lib/utils"; import { Campo, classiInput, PageHeader, Section } from "@/components/crapp/ui-bits"; import { Drawer, DrawerClose, DrawerContent, DrawerDescription, DrawerFooter, DrawerHeader, DrawerTitle, } from "@/components/ui/drawer"; import { formatData } from "@/lib/crapp-data"; import { nomeCompleto, useGiocatoriSquadra } from "@/lib/giocatori-squadra"; import { categoriaEvento, daCategoria, eventoVuoto, useEliminaEvento, useEventi, useSalvaEvento, type CategoriaEvento, type Evento, } from "@/lib/eventi"; import { useGiocatoreBase } from "@/lib/user-store"; import { useIsAdmin } from "@/lib/ruoli"; export const Route = createFileRoute("/eventi")({ head: () => ({ meta: [ { title: "Gestione eventi — CrAPP" }, { name: "description", content: "Area riservata ai referenti CRAP Volley: crea, modifica ed elimina allenamenti, partite ed eventi di squadra.", }, { property: "og:title", content: "Gestione eventi — CrAPP" }, { property: "og:description", content: "Crea e modifica il calendario della squadra e scegli i convocati.", }, { property: "og:type", content: "website" }, { name: "twitter:card", content: "summary" }, ], }), component: GestioneEventi, }); const tipi: Array<{ id: CategoriaEvento; label: string }> = [ { id: "allenamento", label: "Allenamento" }, { id: "partita", label: "Partita" }, { id: "amichevole", label: "Amichevole" }, { id: "evento", label: "Evento" }, ]; function GestioneEventi() { // Solo verità (`!io`, gate admin): `useGiocatoreBase` basta, niente statistiche. const io = useGiocatoreBase(); const admin = useIsAdmin(); const { eventi, isPending, isError, error, refetch } = useEventi(); const { righe: squadra } = useGiocatoriSquadra(); const salva = useSalvaEvento(); const elimina = useEliminaEvento(); const [bozza, setBozza] = useState(null); const [daEliminare, setDaEliminare] = useState(null); const rosa = squadra.filter((g) => g.attivo); if (!io || !admin) { return ( <>

Solo i referenti della squadra possono creare o modificare gli eventi.

); } function aggiorna(patch: Partial) { setBozza((b) => (b ? { ...b, ...patch } : b)); } async function conferma() { if (!bozza) return; if (!bozza.titolo.trim()) { toast.error("Serve un titolo per l'evento"); return; } try { await salva.mutateAsync({ ...bozza, titolo: bozza.titolo.trim() }); toast.success("Evento salvato"); setBozza(null); } catch { toast.error("Non sono riuscito a salvare l'evento"); } } async function rimuovi(id: string) { try { await elimina.mutateAsync(id); toast.success("Evento eliminato"); if (bozza?.id === id) setBozza(null); } catch { toast.error("Non sono riuscito a eliminare l'evento"); } finally { setDaEliminare(null); } } return ( <>
{bozza ? (
e.id === bozza.id) ? "Modifica evento" : "Nuovo evento"} >
{tipi.map((t) => ( ))}
aggiorna({ titolo: e.target.value })} placeholder="Es. CRAP Volley vs Aurora Nera" className={classiInput} />
aggiorna({ data: e.target.value })} className={classiInput} /> aggiorna({ ora: e.target.value })} className={classiInput} />
aggiorna({ luogo: e.target.value })} className={classiInput} />