diff --git a/geneit_app/src/routes/ProfileRoute.tsx b/geneit_app/src/routes/ProfileRoute.tsx index 9094ecd..c4db970 100644 --- a/geneit_app/src/routes/ProfileRoute.tsx +++ b/geneit_app/src/routes/ProfileRoute.tsx @@ -10,47 +10,33 @@ import { TextField, Typography, } from "@mui/material"; -import React, { useRef } from "react"; +import React from "react"; import { ServerApi } from "../api/ServerApi"; import { ReplacePasswordResponse, User, UserApi } from "../api/UserApi"; -import { AsyncWidget } from "../widgets/AsyncWidget"; +import { useAlert } from "../widgets/AlertDialogProvider"; +import { useUser } from "../widgets/BaseAuthenticatedPage"; +import { useConfirm } from "../widgets/ConfirmDialogProvider"; import { PasswordInput } from "../widgets/PasswordInput"; import { formatDate } from "../widgets/TimeWidget"; -import { useConfirm } from "../widgets/ConfirmDialogProvider"; -import { useAlert } from "../widgets/AlertDialogProvider"; export function ProfileRoute(): React.ReactElement { - const [user, setUser] = React.useState(null); - - const load = async () => { - const u = await UserApi.GetUserInfo(); - setUser(u); - }; - - const counter = useRef(0); + const user = useUser(); return ( - ( -
- Profil +
+ Profil - (counter.current += 1)} - /> - {user?.has_password && } - -
- )} - /> + user.reloadUserInfo()} + /> + {user.user.has_password && } + +
); } -function ProfileSettingsCard(p: { user: User; onUpdate: () => {} }) { +function ProfileSettingsCard(p: { user: User; onUpdate: () => void }) { const [newName, setNewName] = React.useState(p.user.name); const [error, setError] = React.useState(null); diff --git a/geneit_app/src/widgets/BaseAuthenticatedPage.tsx b/geneit_app/src/widgets/BaseAuthenticatedPage.tsx index ef33a50..268549a 100644 --- a/geneit_app/src/widgets/BaseAuthenticatedPage.tsx +++ b/geneit_app/src/widgets/BaseAuthenticatedPage.tsx @@ -15,6 +15,13 @@ import { User, UserApi } from "../api/UserApi"; import { AsyncWidget } from "./AsyncWidget"; import { RouterLink } from "./RouterLink"; +interface UserContext { + user: User; + reloadUserInfo: () => void; +} + +const UserContextK = React.createContext(null); + export function BaseAuthenticatedPage(): React.ReactElement { const [user, setUser] = React.useState(null); @@ -48,69 +55,80 @@ export function BaseAuthenticatedPage(): React.ReactElement { load={load} errMsg="Echec du chargement des informations utilisateur !" build={() => ( -
- - - +
+ + + - - GeneIT - + + GeneIT + -
- +
+ - - - - Profil - - Déconnexion - -
- - - -
+ + + + + Profil + + Déconnexion + +
+
+
+ +
+ )} /> ); } + +export function useUser(): UserContext { + return React.useContext(UserContextK)!; +}