From 359dd2f9eea651b4db5e06893a31f6fe2519ce50 Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Wed, 9 Aug 2023 08:55:37 +0200 Subject: [PATCH] Make members information globally available --- geneit_app/src/api/MemberApi.ts | 32 ++- .../src/routes/family/FamilyMemberRoute.tsx | 7 +- geneit_app/src/widgets/BaseFamilyRoute.tsx | 200 ++++++++++-------- 3 files changed, 143 insertions(+), 96 deletions(-) diff --git a/geneit_app/src/api/MemberApi.ts b/geneit_app/src/api/MemberApi.ts index d3c7d13..05419b9 100644 --- a/geneit_app/src/api/MemberApi.ts +++ b/geneit_app/src/api/MemberApi.ts @@ -2,7 +2,7 @@ import { APIClient } from "./ApiClient"; export type Sex = "M" | "F"; -export interface MemberApi { +export interface MemberDataApi { id: number; family_id: number; first_name?: string; @@ -31,7 +31,7 @@ export interface MemberApi { note?: string; } -export class Member implements MemberApi { +export class Member implements MemberDataApi { id: number; family_id: number; first_name?: string; @@ -59,7 +59,7 @@ export class Member implements MemberApi { death_day?: number; note?: string; - constructor(m: MemberApi) { + constructor(m: MemberDataApi) { this.id = m.id; this.family_id = m.family_id; this.first_name = m.first_name; @@ -108,6 +108,20 @@ export class Member implements MemberApi { } } +export class MembersList { + private list: Member[]; + private map: Map; + + constructor(list: Member[]) { + this.list = list; + this.map = new Map(); + + for (const m of list) { + this.map.set(m.id, m); + } + } +} + export class MemberApi { /** * Create a new member @@ -137,6 +151,18 @@ export class MemberApi { return new Member(res.data); } + /** + * Get the entire list of family members of a family + */ + static async GetEntireList(family_id: number): Promise { + const res = await APIClient.exec({ + uri: `/family/${family_id}/members`, + method: "GET", + }); + + return new MembersList(res.data.map((d: any) => new Member(d))); + } + /** * Update a member information */ diff --git a/geneit_app/src/routes/family/FamilyMemberRoute.tsx b/geneit_app/src/routes/family/FamilyMemberRoute.tsx index a78999a..0ed710e 100644 --- a/geneit_app/src/routes/family/FamilyMemberRoute.tsx +++ b/geneit_app/src/routes/family/FamilyMemberRoute.tsx @@ -33,7 +33,7 @@ export function FamilyCreateMemberRoute(): React.ReactElement { try { const r = await MemberApi.Create(m); - // TODO : trigger update + await family.reloadMembersList(); setShouldQuit(true); n(family.family.URL(`member/${r.id}`)); @@ -92,7 +92,7 @@ export function FamilyMemberRoute(): React.ReactElement { snackbar("La fiche de membre a été supprimée avec succès !"); n(family.family.URL("members")); - // TODO : refresh cached members list + await family.reloadMembersList(); } catch (e) { console.error(e); alert("Échec de la suppression du membre !"); @@ -148,7 +148,7 @@ export function FamilyEditMemberRoute(): React.ReactElement { snackbar("Les informations du membre ont été mises à jour avec succès !"); - // TODO : update family hook info + await family.reloadMembersList(); setShouldQuit(true); n(family.family.URL(`member/${member!.id}`)); @@ -193,7 +193,6 @@ export function MemberPage(p: { const [member, setMember] = React.useState(structuredClone(p.member)); const updatedMember = () => { - // TODO : add confirmation setChanged(true); setMember(structuredClone(member)); }; diff --git a/geneit_app/src/widgets/BaseFamilyRoute.tsx b/geneit_app/src/widgets/BaseFamilyRoute.tsx index 7115138..df1c338 100644 --- a/geneit_app/src/widgets/BaseFamilyRoute.tsx +++ b/geneit_app/src/widgets/BaseFamilyRoute.tsx @@ -30,11 +30,14 @@ import { RouterLink } from "./RouterLink"; import { useSnackbar } from "../hooks/context_providers/SnackbarProvider"; import { useConfirm } from "../hooks/context_providers/ConfirmDialogProvider"; import { useAlert } from "../hooks/context_providers/AlertDialogProvider"; +import { Member, MemberApi, MembersList } from "../api/MemberApi"; interface FamilyContext { family: Family; + members: MembersList; familyId: number; reloadFamilyInfo: () => void; + reloadMembersList: () => Promise; } const FamilyContextK = React.createContext(null); @@ -46,16 +49,26 @@ export function BaseFamilyRoute(): React.ReactElement { const confirm = useConfirm(); const [family, setFamily] = React.useState(null); + const [members, setMembers] = React.useState(null); const loadKey = React.useRef(1); + const loadPromise = React.useRef<() => void>(); + const load = async () => { - setFamily(await FamilyApi.GetSingle(Number(familyId))); + const familyID = Number(familyId); + setFamily(await FamilyApi.GetSingle(familyID)); + setMembers(await MemberApi.GetEntireList(familyID)); }; - const onReload = () => { + const onReload = async () => { loadKey.current += 1; setFamily(null); + setMembers(null); + + return new Promise((res, _rej) => { + loadPromise.current = () => res(); + }); }; const copyInvitationCode = async () => { @@ -85,113 +98,122 @@ export function BaseFamilyRoute(): React.ReactElement { return ( ( - - { + if (loadPromise.current != null) { + loadPromise.current?.(); + loadPromise.current = undefined; + } + + return ( + - - } label="Accueil" uri="" /> + + } label="Accueil" uri="" /> - } - label="Membres" - uri="members" - /> + } + label="Membres" + uri="members" + /> - } - label="Couples" - uri="couples" - /> + } + label="Couples" + uri="couples" + /> - } - label="Arbre" - uri="tree" - /> + } + label="Arbre" + uri="tree" + /> - - Administration + + Administration - } - label="Utilisateurs" - uri="users" - /> + } + label="Utilisateurs" + uri="users" + /> - } - label="Paramètres" - uri="settings" - /> + } + label="Paramètres" + uri="settings" + /> - {/* Invitation code */} + {/* Invitation code */} - - - - - - - - {family!.is_admin && ( - - - + + + + - )} - - } - > - - - - - - - - + {family!.is_admin && ( + + + + + + )} + + } + > + + + + + + + + + + - - - )} + + ); + }} /> ); }