api: reconfigered api handling
This commit is contained in:
@@ -1,145 +0,0 @@
|
||||
import React, {
|
||||
createContext,
|
||||
useCallback,
|
||||
useContext,
|
||||
useState,
|
||||
ReactNode,
|
||||
} from "react";
|
||||
import { getPersonHistory, PersonHistoryEntry } from "@/apis/personApi";
|
||||
|
||||
type PersonAppearances = {
|
||||
raw: PersonHistoryEntry[];
|
||||
byShow: Record<number, number[]>;
|
||||
showIds: number[];
|
||||
partnersByShow: Record<
|
||||
number,
|
||||
{ seasonNumber: number; partner?: PersonHistoryEntry["partner"] }[]
|
||||
>;
|
||||
};
|
||||
|
||||
type PersonContextType = {
|
||||
getPersonAppearances: (personId: number) => Promise<PersonAppearances>;
|
||||
getShowIds: (personId: number) => Promise<number[]>;
|
||||
getSeasonsForShow: (personId: number, showId: number) => Promise<number[]>;
|
||||
isLoading: (personId: number) => boolean;
|
||||
getError: (personId: number) => string | null;
|
||||
invalidatePerson: (personId: number) => void;
|
||||
};
|
||||
|
||||
const PersonContext = createContext<PersonContextType | null>(null);
|
||||
|
||||
export const PersonProvider = ({ children }: { children: ReactNode }) => {
|
||||
const [cache, setCache] = useState<Record<number, PersonAppearances>>({});
|
||||
const [loading, setLoading] = useState<Record<number, boolean>>({});
|
||||
const [errors, setErrors] = useState<Record<number, string | null>>({});
|
||||
|
||||
const buildAppearances = (
|
||||
entries: PersonHistoryEntry[]
|
||||
): PersonAppearances => {
|
||||
const byShowSet: Record<number, Set<number>> = {};
|
||||
const partnersByShow: PersonAppearances["partnersByShow"] = {};
|
||||
for (const e of entries) {
|
||||
if (!byShowSet[e.showId]) byShowSet[e.showId] = new Set();
|
||||
byShowSet[e.showId].add(e.seasonNumber);
|
||||
if (!partnersByShow[e.showId]) partnersByShow[e.showId] = [];
|
||||
partnersByShow[e.showId].push({
|
||||
seasonNumber: e.seasonNumber,
|
||||
partner: e.partner ?? undefined,
|
||||
});
|
||||
}
|
||||
const byShow: Record<number, number[]> = Object.fromEntries(
|
||||
Object.entries(byShowSet).map(([showId, seasons]) => [
|
||||
Number(showId),
|
||||
Array.from(seasons).sort((a, b) => a - b),
|
||||
])
|
||||
);
|
||||
|
||||
Object.values(partnersByShow).forEach((arr) =>
|
||||
arr.sort((a, b) => a.seasonNumber - b.seasonNumber)
|
||||
);
|
||||
return {
|
||||
raw: entries,
|
||||
byShow,
|
||||
showIds: Object.keys(byShow)
|
||||
.map(Number)
|
||||
.sort((a, b) => a - b),
|
||||
partnersByShow,
|
||||
};
|
||||
};
|
||||
|
||||
const fetchAndCache = useCallback(async (personId: number) => {
|
||||
setLoading((l) => ({ ...l, [personId]: true }));
|
||||
setErrors((e) => ({ ...e, [personId]: null }));
|
||||
try {
|
||||
const data = await getPersonHistory(personId);
|
||||
const appearances = buildAppearances(data);
|
||||
setCache((c) => ({ ...c, [personId]: appearances }));
|
||||
return appearances;
|
||||
} catch (e: any) {
|
||||
setErrors((err) => ({
|
||||
...err,
|
||||
[personId]: e?.message || "Fehler beim Laden",
|
||||
}));
|
||||
return { raw: [], byShow: {}, showIds: [], partnersByShow: {} };
|
||||
} finally {
|
||||
setLoading((l) => ({ ...l, [personId]: false }));
|
||||
}
|
||||
}, []);
|
||||
|
||||
const getPersonAppearances = useCallback(
|
||||
async (personId: number) => {
|
||||
if (cache[personId]) return cache[personId];
|
||||
return await fetchAndCache(personId);
|
||||
},
|
||||
[cache, fetchAndCache]
|
||||
);
|
||||
|
||||
const getShowIds = useCallback(
|
||||
async (personId: number) => {
|
||||
const app = await getPersonAppearances(personId);
|
||||
return app.showIds;
|
||||
},
|
||||
[getPersonAppearances]
|
||||
);
|
||||
|
||||
const getSeasonsForShow = useCallback(
|
||||
async (personId: number, showId: number) => {
|
||||
const app = await getPersonAppearances(personId);
|
||||
return (app.byShow as Record<number, number[]>)[showId] || [];
|
||||
},
|
||||
[getPersonAppearances]
|
||||
);
|
||||
|
||||
const isLoading = (personId: number) => !!loading[personId];
|
||||
const getError = (personId: number) => errors[personId] || null;
|
||||
|
||||
const invalidatePerson = (personId: number) => {
|
||||
setCache((c) => {
|
||||
const copy = { ...c };
|
||||
delete copy[personId];
|
||||
return copy;
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<PersonContext.Provider
|
||||
value={{
|
||||
getPersonAppearances,
|
||||
getShowIds,
|
||||
getSeasonsForShow,
|
||||
isLoading,
|
||||
getError,
|
||||
invalidatePerson,
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</PersonContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export const usePersonContext = () => {
|
||||
const ctx = useContext(PersonContext);
|
||||
if (!ctx)
|
||||
throw new Error("usePersonContext must be used within PersonProvider");
|
||||
return ctx;
|
||||
};
|
||||
@@ -1,7 +1,6 @@
|
||||
import { getSeason, SeasonParticipant } from "@/apis/seasonApi";
|
||||
import React, {
|
||||
createContext,
|
||||
ReactNode,
|
||||
useCallback,
|
||||
useContext,
|
||||
useState,
|
||||
@@ -13,17 +12,24 @@ type SeasonContextType = {
|
||||
seasonNumber: number
|
||||
) => Promise<SeasonParticipant[]>;
|
||||
fetchSeasonCount: (showId: number) => Promise<number>;
|
||||
fetchSeasonDates: (
|
||||
showId: number,
|
||||
seasonNumber: number
|
||||
) => Promise<{ startDate?: string; endDate?: string | null } | null>;
|
||||
};
|
||||
|
||||
const SeasonContext = createContext<SeasonContextType | null>(null);
|
||||
|
||||
export const SeasonProvider = ({ children }: { children: ReactNode }) => {
|
||||
export const SeasonProvider = ({ children }: { children: React.ReactNode }) => {
|
||||
const [seasonCache, setSeasonCache] = useState<
|
||||
Record<string, SeasonParticipant[]>
|
||||
>({});
|
||||
const [seasonCountCache, setSeasonCountCache] = useState<
|
||||
Record<number, number>
|
||||
>({});
|
||||
const [datesCache, setDatesCache] = useState<
|
||||
Record<string, { startDate?: string; endDate?: string | null }>
|
||||
>({});
|
||||
|
||||
const fetchSeasonParticipants = useCallback(
|
||||
async (showId: number, seasonNumber: number) => {
|
||||
@@ -61,15 +67,34 @@ export const SeasonProvider = ({ children }: { children: ReactNode }) => {
|
||||
[seasonCountCache]
|
||||
);
|
||||
|
||||
const fetchSeasonDates = useCallback(
|
||||
async (showId: number, seasonNumber: number) => {
|
||||
const key = `${showId}-${seasonNumber}`;
|
||||
if (datesCache[key]) return datesCache[key];
|
||||
try {
|
||||
const season = await getSeason(showId, seasonNumber);
|
||||
const dates = season
|
||||
? { startDate: season.startDate, endDate: season.endDate }
|
||||
: null;
|
||||
if (dates) setDatesCache((c) => ({ ...c, [key]: dates }));
|
||||
return dates;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
},
|
||||
[datesCache]
|
||||
);
|
||||
|
||||
return (
|
||||
<SeasonContext.Provider
|
||||
value={{ fetchSeasonParticipants, fetchSeasonCount }}
|
||||
value={{ fetchSeasonParticipants, fetchSeasonCount, fetchSeasonDates }}
|
||||
>
|
||||
{children}
|
||||
</SeasonContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
export const useSeasonContext = () => {
|
||||
const ctx = useContext(SeasonContext);
|
||||
if (!ctx)
|
||||
|
||||
Reference in New Issue
Block a user