- Modello: tipo sugli slot, inRosa() al posto del filtro su attivo in tutte le letture della rosa di gioco (presenze, convocati, voti, palloni, classifiche, badge, sollecito); gli allenatori restano solo nei compleanni e in Squadra. - Permessi: user_roles letto una volta per tutti i ruoli; usePuoGestireEventi() apre /eventi e il sollecito presenze anche all'allenatore, lato server con richiediGestoreEventi(). Palloni, sondaggio e messaggi restano all'admin. - Identità: useGiocatoreInCampo() è null per l'allenatore, così non risponde alle presenze e non vota; useSonoAllenatore() decide cosa si vede. - Profilo: per l'allenatore solo Docs (nome, cognome, nascita, telefono, email) e Opzioni; card avatar e tab Opzioni estratte per riusarle. - Home, Squadra e partita: niente statistiche personali, badge, cacche e obiettivi per l'allenatore; in Rosa compare con la dicitura «Allenatore». - Admin: tipo in «Aggiungi», allenatori a parte in Profili, fuori da conteggi ed export CSI. Test unit su rosa di gioco e completamento, route test sull'allenatore. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
61 lines
2.2 KiB
TypeScript
61 lines
2.2 KiB
TypeScript
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
import { supabase } from "@/integrations/supabase/client";
|
|
import { completaTurni } from "./palloni-core";
|
|
import { useEventi } from "./eventi";
|
|
import { inRosa, nomeCompleto, useGiocatoriSquadra } from "./giocatori-squadra";
|
|
|
|
export const TURNI_KEY = ["turni-palloni"] as const;
|
|
|
|
type RigaTurno = {
|
|
evento_id: string;
|
|
giocatore_id: string;
|
|
aggiornato_da: string | null;
|
|
};
|
|
|
|
async function fetchTurni(): Promise<Record<string, string>> {
|
|
const { data, error } = await supabase
|
|
.from("turni_palloni")
|
|
.select("evento_id, giocatore_id, aggiornato_da");
|
|
if (error) throw error;
|
|
const mappa: Record<string, string> = {};
|
|
for (const riga of (data ?? []) as RigaTurno[]) mappa[riga.evento_id] = riga.giocatore_id;
|
|
return mappa;
|
|
}
|
|
|
|
/** Turni salvati + proposta automatica (solo partite/eventi) per i mancanti. */
|
|
export function useTurniPalloni() {
|
|
// Cambia raramente: una lettura per sessione è sufficiente.
|
|
const query = useQuery({ queryKey: TURNI_KEY, queryFn: fetchTurni, staleTime: 30 * 60_000 });
|
|
const { eventi } = useEventi();
|
|
const { righe: squadra } = useGiocatoriSquadra();
|
|
const salvati = query.data ?? {};
|
|
const rosa = squadra.filter(inRosa).map((g) => ({ id: g.id, nome: nomeCompleto(g) }));
|
|
return { ...query, salvati, turni: completaTurni(salvati, eventi, rosa) };
|
|
}
|
|
|
|
export function useAssegnaTurno() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: async (input: { eventoId: string; giocatoreId: string; da: string | null }) => {
|
|
const { error } = await supabase.from("turni_palloni").upsert(
|
|
{
|
|
evento_id: input.eventoId,
|
|
giocatore_id: input.giocatoreId,
|
|
aggiornato_da: input.da,
|
|
aggiornato_il: new Date().toISOString(),
|
|
},
|
|
{ onConflict: "evento_id" },
|
|
);
|
|
if (error) throw error;
|
|
return input;
|
|
},
|
|
// Scrittura unica + aggiornamento cache locale, senza rilettura.
|
|
onSuccess: (input) => {
|
|
queryClient.setQueryData<Record<string, string>>(TURNI_KEY, (prec) => ({
|
|
...(prec ?? {}),
|
|
[input.eventoId]: input.giocatoreId,
|
|
}));
|
|
},
|
|
});
|
|
}
|