Add recovery for public data loading failures
This commit is contained in:
parent
1171eee510
commit
daa5eba8bd
14 changed files with 186 additions and 58 deletions
|
|
@ -1,12 +1,58 @@
|
|||
// Rôle : charge le contenu éditorial et les données publiques nécessaires à l'application.
|
||||
import { useEffect, useState } from "react";
|
||||
// Rôle : charge et permet de relancer séparément le contenu éditorial et les données publiques.
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
import { INITIAL_ANIIMO_STATE, loadAniimoData, INITIAL_DIABLO4_STATE, INITIAL_MHWILDS_STATE, loadDiablo4Data, loadMhwildsData } from "../features/games/loaders.js";
|
||||
|
||||
function useJsonResource(url, initialValue, errorMessage, normalize = (value) => value) {
|
||||
const [data, setData] = useState(initialValue);
|
||||
const [error, setError] = useState("");
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [attempt, setAttempt] = useState(0);
|
||||
|
||||
const retry = useCallback(() => {
|
||||
setError("");
|
||||
setLoading(true);
|
||||
setAttempt((current) => current + 1);
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
const controller = new AbortController();
|
||||
const { signal } = controller;
|
||||
fetch(url, { signal })
|
||||
.then((response) => {
|
||||
if (!response.ok) throw new Error(errorMessage);
|
||||
return response.json();
|
||||
})
|
||||
.then((payload) => {
|
||||
if (signal.aborted) return;
|
||||
setData(normalize(payload));
|
||||
setError("");
|
||||
setLoading(false);
|
||||
})
|
||||
.catch(() => {
|
||||
if (signal.aborted) return;
|
||||
setError(errorMessage);
|
||||
setLoading(false);
|
||||
});
|
||||
return () => controller.abort();
|
||||
}, [attempt, errorMessage, normalize, url]);
|
||||
|
||||
return { data, error, loading, retry };
|
||||
}
|
||||
|
||||
const normalizeSiteContent = (payload) => payload;
|
||||
const normalizeGames = (payload) => Array.isArray(payload?.games) ? payload.games : [];
|
||||
|
||||
function useGameData(active, initialState, load) {
|
||||
const [requested, setRequested] = useState(active);
|
||||
const [data, setData] = useState(initialState);
|
||||
const [attempt, setAttempt] = useState(0);
|
||||
if (active && !requested) setRequested(true);
|
||||
|
||||
const retry = useCallback(() => {
|
||||
setData((current) => ({ ...current, loading: true, error: "" }));
|
||||
setAttempt((current) => current + 1);
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (!requested) return undefined;
|
||||
const controller = new AbortController();
|
||||
|
|
@ -15,55 +61,31 @@ function useGameData(active, initialState, load) {
|
|||
if (!controller.signal.aborted) setData(result);
|
||||
})
|
||||
.catch((error) => {
|
||||
if (!controller.signal.aborted) setData({ ...initialState, loaded: true, error: error.message });
|
||||
if (!controller.signal.aborted) setData((current) => ({ ...current, loaded: true, loading: false, error: error.message }));
|
||||
});
|
||||
return () => controller.abort();
|
||||
}, [requested, initialState, load]);
|
||||
}, [attempt, requested, load]);
|
||||
|
||||
// Une visite déclenche un seul chargement, conservé même si la route change.
|
||||
return requested && !data.loaded ? { ...data, loading: true } : data;
|
||||
return { ...data, loading: requested && (!data.loaded || data.loading), retry };
|
||||
}
|
||||
|
||||
export function useAppData(route) {
|
||||
const [siteContent, setSiteContent] = useState(null);
|
||||
const [siteContentError, setSiteContentError] = useState("");
|
||||
const [games, setGames] = useState([]);
|
||||
const [gamesError, setGamesError] = useState("");
|
||||
const site = useJsonResource("/data/site.json", null, "Impossible de charger le contenu du site.", normalizeSiteContent);
|
||||
const gameList = useJsonResource("/data/games.json", [], "Impossible de charger la liste des jeux.", normalizeGames);
|
||||
const mhwilds = useGameData(route.startsWith("/games/mhwilds"), INITIAL_MHWILDS_STATE, loadMhwildsData);
|
||||
const aniimo = useGameData(route.startsWith("/games/aniimo"), INITIAL_ANIIMO_STATE, loadAniimoData);
|
||||
const diablo4 = useGameData(route.startsWith("/games/diablo4"), INITIAL_DIABLO4_STATE, loadDiablo4Data);
|
||||
|
||||
useEffect(() => {
|
||||
const controller = new AbortController();
|
||||
const { signal } = controller;
|
||||
Promise.all([
|
||||
fetch("/data/site.json", { signal }).then((response) => {
|
||||
if (!response.ok) throw new Error("Impossible de charger le contenu du site.");
|
||||
return response.json();
|
||||
}).catch((error) => {
|
||||
if (!signal.aborted) setSiteContentError(error.message);
|
||||
return null;
|
||||
}),
|
||||
fetch("/data/games.json", { signal }).then((response) => {
|
||||
if (!response.ok) throw new Error("Impossible de charger la liste des jeux.");
|
||||
return response.json();
|
||||
}).catch((error) => {
|
||||
if (!signal.aborted) setGamesError(error.message);
|
||||
return { games: [] };
|
||||
})
|
||||
]).then(([content, gamesPayload]) => {
|
||||
if (signal.aborted) return;
|
||||
if (content) setSiteContent(content);
|
||||
setGames(Array.isArray(gamesPayload.games) ? gamesPayload.games : []);
|
||||
});
|
||||
return () => controller.abort();
|
||||
}, []);
|
||||
|
||||
return {
|
||||
siteContent,
|
||||
siteContentError,
|
||||
games,
|
||||
gamesError,
|
||||
siteContent: site.data,
|
||||
siteContentError: site.error,
|
||||
siteContentLoading: site.loading,
|
||||
retrySiteContent: site.retry,
|
||||
games: gameList.data,
|
||||
gamesError: gameList.error,
|
||||
gamesLoading: gameList.loading,
|
||||
retryGames: gameList.retry,
|
||||
mhwilds,
|
||||
diablo4,
|
||||
aniimo
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue