diff --git a/src/components/crapp/AdminLogin.tsx b/src/components/crapp/AdminLogin.tsx new file mode 100644 index 0000000..77b27c4 --- /dev/null +++ b/src/components/crapp/AdminLogin.tsx @@ -0,0 +1,79 @@ +import { useState } from "react"; +import { Loader2, Lock } from "lucide-react"; +import { toast } from "sonner"; +import { loginAdmin, logoutAdmin, useAdminSession } from "@/lib/admin-auth"; + +/** Sblocca le funzioni admin (scout, gestione eventi, sollecito presenze) con la password + * dell'account Strapi dei referenti — non più scegliendo semplicemente il proprio nome. */ +export function AdminLogin() { + const { attivo, email } = useAdminSession(); + const [inputEmail, setInputEmail] = useState(""); + const [password, setPassword] = useState(""); + const [inCorso, setInCorso] = useState(false); + + if (attivo) { + return ( +
+ + Admin attivo + {email} + + +
+ ); + } + + async function accedi(e: React.FormEvent) { + e.preventDefault(); + if (!inputEmail || !password || inCorso) return; + setInCorso(true); + const risultato = await loginAdmin(inputEmail, password); + setInCorso(false); + if (risultato.ok) { + setPassword(""); + toast.success("Modalità admin attiva"); + } else { + toast.error(risultato.errore); + } + } + + return ( +
+

+ Accedi come admin (account Strapi) +

+ setInputEmail(e.target.value)} + placeholder="Email" + autoComplete="username" + className="w-full rounded-xl border border-border bg-background px-3 py-2 text-sm" + /> + setPassword(e.target.value)} + placeholder="Password" + autoComplete="current-password" + className="w-full rounded-xl border border-border bg-background px-3 py-2 text-sm" + /> + +
+ ); +} diff --git a/src/components/crapp/RosaPresenze.tsx b/src/components/crapp/RosaPresenze.tsx index cfacc21..deed609 100644 --- a/src/components/crapp/RosaPresenze.tsx +++ b/src/components/crapp/RosaPresenze.tsx @@ -4,8 +4,9 @@ import { toast } from "sonner"; import { cn } from "@/lib/utils"; import { Avatar } from "@/components/crapp/Avatar"; import { Barra } from "@/components/motion/Barra"; -import { isAdmin, statoMeta, type Giocatore, type Stato } from "@/lib/crapp-data"; +import { statoMeta, type Giocatore, type Stato } from "@/lib/crapp-data"; import { useRosaBase } from "@/lib/rosa-base"; +import { useAdminSession } from "@/lib/admin-auth"; import { usePresenzeEvento, useSalvaPresenza } from "@/lib/presenze"; import { useGiocatoreCorrente } from "@/lib/user-store"; @@ -15,6 +16,7 @@ export function RosaPresenze({ eventoId }: { eventoId: string }) { const { risposte, isPending } = usePresenzeEvento(eventoId); const salva = useSalvaPresenza(); const io = useGiocatoreCorrente(); + const admin = useAdminSession(); const giocatori = useRosaBase(); const [sollecito, setSollecito] = useState(false); @@ -108,7 +110,7 @@ export function RosaPresenze({ eventoId }: { eventoId: string }) { ) : null} - {io && isAdmin(io) ? ( + {io && admin.attivo ? ( - {["Notifiche convocazioni", "Promemoria allenamenti", "Cambi orario", "Bacheca squadra"].map( - (v) => ( - - ), - )} + {[ + "Notifiche convocazioni", + "Promemoria allenamenti", + "Cambi orario", + "Bacheca squadra", + ].map((v) => ( + + ))} + +
+ +
); -} \ No newline at end of file +} diff --git a/src/routes/scout.tsx b/src/routes/scout.tsx index f6ea5f4..961307b 100644 --- a/src/routes/scout.tsx +++ b/src/routes/scout.tsx @@ -3,8 +3,10 @@ import { createFileRoute, useNavigate } from "@tanstack/react-router"; import { Undo2, Save, CheckCircle2, Radio, Lock, CalendarX2, LogOut } from "lucide-react"; import { toast } from "sonner"; import { cn } from "@/lib/utils"; -import { formatData, isAdmin } from "@/lib/crapp-data"; +import { formatData } from "@/lib/crapp-data"; import { useRosaBase } from "@/lib/rosa-base"; +import { useAdminSession } from "@/lib/admin-auth"; +import { AdminLogin } from "@/components/crapp/AdminLogin"; import type { Evento } from "@/lib/eventi"; import { useGiocatoreCorrente } from "@/lib/user-store"; import { usePresenzeEvento } from "@/lib/presenze"; @@ -82,6 +84,7 @@ function Blocco({ function Scout() { const { pronto, partita } = usePartitaDiOggi(); const io = useGiocatoreCorrente(); + const admin = useAdminSession(); const sessione = useSessioneScout(partita?.id ?? null); const statoSalvato = useStatoScout(partita?.id ?? null); const apri = useApriSessioneScout(); @@ -100,13 +103,17 @@ function Scout() { // eslint-disable-next-line react-hooks/exhaustive-deps }, [controllo, partita?.id, io?.id]); - if (io && !isAdmin(io)) { + if (!admin.attivo) { return ( } titolo="Scout riservato" testo="Lo scout live è uno strumento tecnico per allenatori e referenti della squadra." - /> + > +
+ +
+
); }