Sposto rosa, classifica e storico partite dagli array hardcoded al CMS Strapi
Nuovi moduli src/lib/strapi-client.ts + rosa-base.ts/classifica-csi.ts/ storico-match.ts (hook TanStack Query, cache lunga, nessun polling). L'id stabile gN diventa il campo `codice` su Strapi, per non rompere le FK verso pagelle/cacche/mvp/palloni/presenze/azioni scout già chiavate su quella stringa. isAdmin ora prende il Giocatore invece di un id, così non dipende più dal roster. Rifattorizzati di conseguenza tutti i punti che importavano `giocatori`/`classifica`/`storicoMatch` come array sincroni (componenti, hook, due route server) per usare gli hook o un parametro esplicito. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { strapiFetch } from "./strapi-client";
|
||||
import type { Giocatore } from "./crapp-data";
|
||||
|
||||
function inizialiDa(nome: string) {
|
||||
return nome
|
||||
.split(" ")
|
||||
.map((p) => p[0] ?? "")
|
||||
.join("")
|
||||
.slice(0, 2)
|
||||
.toUpperCase();
|
||||
}
|
||||
|
||||
type GiocatoreStrapi = {
|
||||
codice: string;
|
||||
nome: string;
|
||||
numero: number;
|
||||
ruolo: string;
|
||||
nascita: string;
|
||||
};
|
||||
|
||||
/** Anagrafica base (senza statistiche, calcolate a parte da useRosa/giocatoriConScout). */
|
||||
function daEntry(e: GiocatoreStrapi): Giocatore {
|
||||
return {
|
||||
id: e.codice,
|
||||
nome: e.nome,
|
||||
numero: e.numero,
|
||||
ruolo: e.ruolo,
|
||||
nascita: e.nascita,
|
||||
iniziali: inizialiDa(e.nome),
|
||||
totaliEventi: 0,
|
||||
presenze: 0,
|
||||
streak: 0,
|
||||
serieAllenamenti: 0,
|
||||
seriePartite: 0,
|
||||
serieConferme: 0,
|
||||
mvp: 0,
|
||||
mediaVoto: 0,
|
||||
palloni: 0,
|
||||
cacche: 0,
|
||||
cacchePartita: 0,
|
||||
infortuni: 0,
|
||||
ritardi: 0,
|
||||
};
|
||||
}
|
||||
|
||||
export const ROSA_BASE_KEY = ["rosa-base"] as const;
|
||||
|
||||
/** Usabile anche fuori da React (route server, script): stessa fonte dati dell'hook. */
|
||||
export async function fetchRosaBase(): Promise<Giocatore[]> {
|
||||
const entries = await strapiFetch<GiocatoreStrapi>("giocatoris?sort=numero");
|
||||
return entries.map((e) => daEntry(e));
|
||||
}
|
||||
|
||||
/** Anagrafica rosa dal CMS: cambia a stagione, non a sessione — cache lunga, niente polling. */
|
||||
export function useRosaBaseQuery() {
|
||||
return useQuery({ queryKey: ROSA_BASE_KEY, queryFn: fetchRosaBase, staleTime: 60 * 60_000 });
|
||||
}
|
||||
|
||||
export function useRosaBase(): Giocatore[] {
|
||||
return useRosaBaseQuery().data ?? [];
|
||||
}
|
||||
Reference in New Issue
Block a user