diff --git a/geneit_app/src/api/FamilyApi.ts b/geneit_app/src/api/FamilyApi.ts index 0e2c663..3c471bc 100644 --- a/geneit_app/src/api/FamilyApi.ts +++ b/geneit_app/src/api/FamilyApi.ts @@ -1,6 +1,15 @@ import { APIClient } from "./ApiClient"; -export interface Family {} +export interface Family { + user_id: number; + family_id: number; + name: string; + time_create: number; + is_admin: boolean; + invitation_code: string; + count_members: number; + count_admins: number; +} export enum JoinFamilyResult { TooManyRequests, @@ -50,10 +59,29 @@ export class FamilyApi { /** * Get the list of families */ - static async GetList():Promise { - return (await APIClient.exec({ - method: "GET", - uri: "/family/list", + static async GetList(): Promise { + return ( + await APIClient.exec({ + method: "GET", + uri: "/family/list", + }) + ).data; + } - })).data - }} + /** + * Check if the current user user can leave a family + */ + static CanLeaveFamily(f: Family): boolean { + return !f.is_admin || f.count_admins > 1; + } + + /** + * Attempt to leave a family + */ + static async LeaveFamily(id: number): Promise { + await APIClient.exec({ + method: "POST", + uri: `/family/${id}/leave`, + }); + } +} diff --git a/geneit_app/src/routes/FamiliesListRoute.tsx b/geneit_app/src/routes/FamiliesListRoute.tsx index e693c98..fce943b 100644 --- a/geneit_app/src/routes/FamiliesListRoute.tsx +++ b/geneit_app/src/routes/FamiliesListRoute.tsx @@ -1,11 +1,23 @@ import AddIcon from "@mui/icons-material/Add"; import ConfirmationNumberIcon from "@mui/icons-material/ConfirmationNumber"; -import { Button, IconButton, Tooltip, Typography } from "@mui/material"; +import { + Button, + Card, + CardActionArea, + CardActions, + CardContent, + IconButton, + Tooltip, + Typography, +} from "@mui/material"; import React from "react"; import { Family, FamilyApi } from "../api/FamilyApi"; import { CreateFamilyDialog } from "../dialogs/CreateFamilyDialog"; import { JoinFamilyDialog } from "../dialogs/JoinFamilyDialog"; import { AsyncWidget } from "../widgets/AsyncWidget"; +import { TimeWidget } from "../widgets/TimeWidget"; +import { useConfirm } from "../widgets/ConfirmDialogProvider"; +import { useAlert } from "../widgets/AlertDialogProvider"; export function FamiliesListRoute(): React.ReactElement { const loadKey = React.useRef(1); @@ -21,6 +33,7 @@ export function FamiliesListRoute(): React.ReactElement { const reload = () => { loadKey.current += 1; + setFamilies(null); }; const onRequestCreateFamily = async () => { @@ -45,9 +58,10 @@ export function FamiliesListRoute(): React.ReactElement { onRequestJoinFamily={onRequestJoinFamily} /> ) : ( - )} @@ -125,14 +139,21 @@ function NoFamilyButton(p: { ); } -function HasFamilysWidget(p: { +function HasFamiliesWidget(p: { families: Family[]; onRequestCreateFamily: () => void; onRequestJoinFamily: () => void; + onRequestRefresh: () => void; }): React.ReactElement { return (
-
+
Mes familles
@@ -147,7 +168,86 @@ function HasFamilysWidget(p: {
- TODO list of families + + {/* Display user memberships */} + {p.families.map((f) => ( + + ))}
); } + +function FamilyCard(p: { + f: Family; + onFamilyLeft: () => void; +}): React.ReactElement { + const canLeave = FamilyApi.CanLeaveFamily(p.f); + const confirm = useConfirm(); + const alert = useAlert(); + + const leaveFamily = async () => { + try { + if ( + !canLeave || + !(await confirm.confirm( + "Voulez-vous vraiment quitter la famille " + p.f.name + " ?" + )) + ) + return; + + await FamilyApi.LeaveFamily(p.f.family_id); + + p.onFamilyLeft(); + + alert.alert( + `La famille ${p.f.name} a été retirée de votre liste de familles !` + ); + } catch (e) { + console.error(e); + alert.alert("Echec du retrait de la famille !"); + } + }; + + return ( + + + + + {p.f.name} + + + {p.f.count_members === 1 + ? "1 membre" + : p.f.count_members + " membres"}{" "} +
+ {p.f.count_admins === 1 + ? "1 administrateur" + : p.f.count_admins + " administrateurs"} +
+ Famille créée il y a +
+
+
+ + + + + + + +
+ ); +} diff --git a/geneit_app/src/widgets/AsyncWidget.tsx b/geneit_app/src/widgets/AsyncWidget.tsx index d213c21..39bb48c 100644 --- a/geneit_app/src/widgets/AsyncWidget.tsx +++ b/geneit_app/src/widgets/AsyncWidget.tsx @@ -36,21 +36,6 @@ export function AsyncWidget(p: { load(); }); - if (state === State.Loading || p.ready === false) - return ( -
- -
- ); - if (state === State.Error) return (
Réessayer
); - console.log(p, counter.current); + + if (state === State.Loading || p.ready === false) + return ( +
+ +
+ ); + return p.build(); }