63 lines
1.6 KiB
TypeScript
63 lines
1.6 KiB
TypeScript
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 ?? [];
|
||
|
|
}
|