2023-06-09 16:27:03 +00:00
|
|
|
import { mdiFamilyTree } from "@mdi/js";
|
|
|
|
import Icon from "@mdi/react";
|
|
|
|
import SettingsIcon from "@mui/icons-material/Settings";
|
|
|
|
import { Alert, Button, CircularProgress } from "@mui/material";
|
2023-06-09 09:19:40 +00:00
|
|
|
import AppBar from "@mui/material/AppBar";
|
|
|
|
import Menu from "@mui/material/Menu";
|
|
|
|
import MenuItem from "@mui/material/MenuItem";
|
|
|
|
import Toolbar from "@mui/material/Toolbar";
|
|
|
|
import Typography from "@mui/material/Typography";
|
2023-06-09 16:27:03 +00:00
|
|
|
import { useSetAtom } from "jotai";
|
2023-06-09 09:19:40 +00:00
|
|
|
import * as React from "react";
|
2023-06-09 16:27:03 +00:00
|
|
|
import { Outlet, useNavigate } from "react-router-dom";
|
|
|
|
import { AuthApi } from "../api/AuthApi";
|
2023-06-09 09:19:40 +00:00
|
|
|
import { User, UserApi } from "../api/UserApi";
|
|
|
|
|
|
|
|
export function BaseAuthenticatedPage(): React.ReactElement {
|
|
|
|
const [user, setUser] = React.useState<null | User>(null);
|
|
|
|
const [error, setError] = React.useState(false);
|
|
|
|
|
2023-06-09 16:27:03 +00:00
|
|
|
const setSignedIn = useSetAtom(AuthApi.authStatus);
|
|
|
|
const navigate = useNavigate();
|
|
|
|
|
2023-06-09 09:19:40 +00:00
|
|
|
const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>(null);
|
|
|
|
|
|
|
|
const handleMenu = (event: React.MouseEvent<HTMLElement>) => {
|
|
|
|
setAnchorEl(event.currentTarget);
|
|
|
|
};
|
|
|
|
|
2023-06-09 16:27:03 +00:00
|
|
|
const handleCloseMenu = () => {
|
2023-06-09 09:19:40 +00:00
|
|
|
setAnchorEl(null);
|
|
|
|
};
|
|
|
|
|
2023-06-09 16:27:03 +00:00
|
|
|
const signOut = () => {
|
|
|
|
handleCloseMenu();
|
|
|
|
AuthApi.SignOut();
|
|
|
|
navigate("/");
|
|
|
|
setSignedIn(false);
|
|
|
|
};
|
|
|
|
|
2023-06-09 09:19:40 +00:00
|
|
|
React.useEffect(() => {
|
|
|
|
const load = async () => {
|
|
|
|
if (error || user != null) return;
|
|
|
|
try {
|
|
|
|
const user = await UserApi.GetUserInfo();
|
|
|
|
setUser(user);
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
setError(true);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
load();
|
|
|
|
});
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
style={{
|
|
|
|
height: "100vh",
|
|
|
|
display: "flex",
|
|
|
|
flexDirection: "column",
|
|
|
|
alignItems: "center",
|
|
|
|
justifyContent: "center",
|
|
|
|
textAlign: "center",
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Alert
|
|
|
|
severity="error"
|
|
|
|
style={{ maxWidth: "300px", marginBottom: "10px" }}
|
|
|
|
>
|
|
|
|
Echec du chagement des informations utilisateur !
|
|
|
|
</Alert>
|
|
|
|
|
|
|
|
<a href="/">Réessayer</a>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
|
|
|
if (user === null)
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
style={{
|
|
|
|
height: "100vh",
|
|
|
|
justifyContent: "center",
|
|
|
|
alignItems: "center",
|
|
|
|
display: "flex",
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<CircularProgress />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<AppBar position="fixed">
|
|
|
|
<Toolbar>
|
2023-06-09 16:27:03 +00:00
|
|
|
<Icon path={mdiFamilyTree} size={1} style={{ marginRight: "1rem" }} />
|
2023-06-09 09:19:40 +00:00
|
|
|
<Typography variant="h6" component="div" sx={{ flexGrow: 1 }}>
|
|
|
|
GeneIT
|
|
|
|
</Typography>
|
|
|
|
<div>
|
|
|
|
<Button size="large" color="inherit">
|
|
|
|
{user.name}
|
|
|
|
</Button>
|
|
|
|
|
|
|
|
<Button
|
|
|
|
size="large"
|
|
|
|
aria-label="account of current user"
|
|
|
|
aria-controls="menu-appbar"
|
|
|
|
aria-haspopup="true"
|
|
|
|
onClick={handleMenu}
|
|
|
|
color="inherit"
|
|
|
|
>
|
|
|
|
<SettingsIcon />
|
|
|
|
</Button>
|
|
|
|
<Menu
|
|
|
|
id="menu-appbar"
|
|
|
|
anchorEl={anchorEl}
|
|
|
|
anchorOrigin={{
|
|
|
|
vertical: "top",
|
|
|
|
horizontal: "right",
|
|
|
|
}}
|
|
|
|
keepMounted
|
|
|
|
transformOrigin={{
|
|
|
|
vertical: "top",
|
|
|
|
horizontal: "right",
|
|
|
|
}}
|
|
|
|
open={Boolean(anchorEl)}
|
2023-06-09 16:27:03 +00:00
|
|
|
onClose={handleCloseMenu}
|
2023-06-09 09:19:40 +00:00
|
|
|
>
|
2023-06-09 16:27:03 +00:00
|
|
|
<MenuItem onClick={handleCloseMenu}>Profil</MenuItem>
|
|
|
|
<MenuItem onClick={signOut}>Déconnexion</MenuItem>
|
2023-06-09 09:19:40 +00:00
|
|
|
</Menu>
|
|
|
|
</div>
|
|
|
|
</Toolbar>
|
|
|
|
</AppBar>
|
|
|
|
<Outlet />
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|