GeneIT/geneit_app/src/routes/FamiliesListRoute.tsx

154 lines
4.1 KiB
TypeScript
Raw Normal View History

2023-07-04 17:05:36 +00:00
import AddIcon from "@mui/icons-material/Add";
import ConfirmationNumberIcon from "@mui/icons-material/ConfirmationNumber";
import { Button, IconButton, Tooltip, Typography } from "@mui/material";
2023-06-27 15:13:12 +00:00
import React from "react";
2023-07-04 17:05:36 +00:00
import { Family, FamilyApi } from "../api/FamilyApi";
2023-06-27 15:13:12 +00:00
import { CreateFamilyDialog } from "../dialogs/CreateFamilyDialog";
2023-06-27 16:52:49 +00:00
import { JoinFamilyDialog } from "../dialogs/JoinFamilyDialog";
2023-07-04 17:05:36 +00:00
import { AsyncWidget } from "../widgets/AsyncWidget";
2023-06-27 15:13:12 +00:00
export function FamiliesListRoute(): React.ReactElement {
const loadKey = React.useRef(1);
const [families, setFamilies] = React.useState<Family[] | null>(null);
const [createFamily, setCreateFamily] = React.useState(false);
2023-06-27 16:52:49 +00:00
const [joinFamily, setJoinFamily] = React.useState(false);
2023-06-27 15:13:12 +00:00
const load = async () => {
2023-07-04 17:05:36 +00:00
setFamilies(await FamilyApi.GetList());
2023-06-27 15:13:12 +00:00
};
const reload = () => {
loadKey.current += 1;
};
const onRequestCreateFamily = async () => {
setCreateFamily(true);
};
2023-06-27 16:52:49 +00:00
const onRequestJoinFamily = async () => {
setJoinFamily(true);
};
2023-06-27 15:13:12 +00:00
return (
<AsyncWidget
2023-06-27 17:03:39 +00:00
ready={families !== null}
loadKey={`families-list-${loadKey.current}`}
2023-06-27 15:13:12 +00:00
load={load}
errMsg="Echec du chargement de la liste des familles"
build={() => (
<>
{families!!.length === 0 ? (
2023-06-27 16:52:49 +00:00
<NoFamilyWidget
onRequestCreateFamily={onRequestCreateFamily}
onRequestJoinFamily={onRequestJoinFamily}
/>
2023-06-27 15:13:12 +00:00
) : (
2023-07-04 17:05:36 +00:00
<HasFamilysWidget
onRequestCreateFamily={onRequestCreateFamily}
onRequestJoinFamily={onRequestJoinFamily}
families={families!}
/>
2023-06-27 15:13:12 +00:00
)}
{/** Create family dialog anchor */}
<CreateFamilyDialog
open={createFamily}
onClose={() => setCreateFamily(false)}
onCreated={() => {
setCreateFamily(false);
reload();
}}
/>
2023-06-27 16:52:49 +00:00
{/** Join family dialog anchor */}
<JoinFamilyDialog
open={joinFamily}
onClose={() => setJoinFamily(false)}
onJoined={() => {
setJoinFamily(false);
reload();
}}
/>
2023-06-27 15:13:12 +00:00
</>
)}
/>
);
}
function NoFamilyWidget(p: {
onRequestCreateFamily: () => void;
2023-06-27 16:52:49 +00:00
onRequestJoinFamily: () => void;
2023-06-27 15:13:12 +00:00
}): React.ReactElement {
return (
<div style={{ flex: 1, display: "flex", alignItems: "center" }}>
2023-07-04 16:47:37 +00:00
<Typography variant="h3" style={{ flex: 3, textAlign: "center" }}>
2023-06-27 15:13:12 +00:00
Vous n'appartenez à aucune famille !
</Typography>
<div
style={{
2023-07-04 16:47:37 +00:00
flex: 2,
2023-06-27 15:13:12 +00:00
display: "flex",
flexDirection: "column",
alignItems: "center",
}}
>
<NoFamilyButton
label="Créer une famille"
onClick={p.onRequestCreateFamily}
/>
2023-06-27 16:52:49 +00:00
<NoFamilyButton
label="Rejoindre une famille"
onClick={p.onRequestJoinFamily}
/>
2023-06-27 15:13:12 +00:00
</div>
</div>
);
}
function NoFamilyButton(p: {
label: string;
onClick: () => void;
}): React.ReactElement {
return (
<Button
variant="outlined"
size="large"
style={{ width: "300px", marginBottom: "10px" }}
onClick={p.onClick}
>
{p.label}
</Button>
);
}
function HasFamilysWidget(p: {
families: Family[];
2023-07-04 17:05:36 +00:00
onRequestCreateFamily: () => void;
onRequestJoinFamily: () => void;
2023-06-27 15:13:12 +00:00
}): React.ReactElement {
2023-07-04 17:05:36 +00:00
return (
<div style={{ width: "100%", maxWidth: "800px", margin: "20px auto" }}>
<div style={{ display: "flex", justifyContent: "space-between" }}>
<Typography variant="h4">Mes familles</Typography>
<div>
<IconButton aria-label="create" onClick={p.onRequestCreateFamily}>
<Tooltip title="Créer une nouvelle famille">
<AddIcon />
</Tooltip>
</IconButton>
<IconButton aria-label="create" onClick={p.onRequestJoinFamily}>
<Tooltip title="Rejoindre une famille">
<ConfirmationNumberIcon />
</Tooltip>
</IconButton>
</div>
</div>
TODO list of families
</div>
);
2023-06-27 15:13:12 +00:00
}