Build family information provider

This commit is contained in:
2023-07-08 11:59:55 +02:00
parent ce7e21d20d
commit d9993cab21
4 changed files with 75 additions and 0 deletions

View File

@ -0,0 +1,54 @@
import { Outlet, useParams } from "react-router-dom";
import { Family, FamilyApi } from "../api/FamilyApi";
import React from "react";
import { AsyncWidget } from "./AsyncWidget";
interface FamilyContext {
family: Family;
reloadFamilyInfo: () => void;
}
const FamilyContextK = React.createContext<FamilyContext | null>(null);
export function BaseFamilyRoute(): React.ReactElement {
const { familyId } = useParams();
const [family, setFamily] = React.useState<null | Family>(null);
const loadKey = React.useRef(1);
const load = async () => {
setFamily(await FamilyApi.GetSingle(Number(familyId)));
};
const onReload = () => {
loadKey.current += 1;
setFamily(null);
};
return (
<AsyncWidget
ready={family != null}
loadKey={`${familyId}-${loadKey}`}
load={load}
errMsg="Échec du chargement des informations de la famille !"
build={() => (
<FamilyContextK.Provider
value={{
family: family!,
reloadFamilyInfo: onReload,
}}
>
<div>
<p>base family route</p>
<Outlet />
</div>
</FamilyContextK.Provider>
)}
/>
);
}
export function useFamily(): FamilyContext {
return React.useContext(FamilyContextK)!;
}