Feat(frontend): navigation and registration page
This commit is contained in:
67
frontend/src/components/AccountMenu.tsx
Normal file
67
frontend/src/components/AccountMenu.tsx
Normal file
@@ -0,0 +1,67 @@
|
||||
import axios from "axios";
|
||||
import Divider from "@mui/material/Divider";
|
||||
import IconButton from "@mui/material/IconButton";
|
||||
import Menu from "@mui/material/Menu";
|
||||
import MenuItem from "@mui/material/MenuItem";
|
||||
import AccountCircle from "@mui/icons-material/AccountCircle";
|
||||
import { useQueryClient } from "@tanstack/react-query";
|
||||
import React, { useContext, useState } from "react";
|
||||
import { Link } from "react-router-dom";
|
||||
|
||||
import { AuthContext } from "src/AuthContext";
|
||||
import { useMutation } from "src/query";
|
||||
|
||||
const useLogout = () => {
|
||||
const { setAuthenticated } = useContext(AuthContext);
|
||||
const queryClient = useQueryClient();
|
||||
const { mutate: logout } = useMutation(
|
||||
async () => await axios.delete("/sessions/"),
|
||||
{
|
||||
onSuccess: () => {
|
||||
setAuthenticated(false);
|
||||
queryClient.clear();
|
||||
},
|
||||
},
|
||||
);
|
||||
return logout;
|
||||
};
|
||||
|
||||
const AccountMenu = () => {
|
||||
const logout = useLogout();
|
||||
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
|
||||
|
||||
const onMenuOpen = (event: React.MouseEvent<HTMLElement>) =>
|
||||
setAnchorEl(event.currentTarget);
|
||||
const onMenuClose = () => setAnchorEl(null);
|
||||
|
||||
return (
|
||||
<>
|
||||
<IconButton color="inherit" onClick={onMenuOpen}>
|
||||
<AccountCircle />
|
||||
</IconButton>
|
||||
<Menu
|
||||
anchorEl={anchorEl}
|
||||
anchorOrigin={{ horizontal: "right", vertical: "top" }}
|
||||
keepMounted
|
||||
onClose={onMenuClose}
|
||||
open={Boolean(anchorEl)}
|
||||
transformOrigin={{ horizontal: "right", vertical: "top" }}
|
||||
>
|
||||
<MenuItem component={Link} onClick={onMenuClose} to="/change-password/">
|
||||
Change password
|
||||
</MenuItem>
|
||||
<Divider />
|
||||
<MenuItem
|
||||
onClick={() => {
|
||||
logout();
|
||||
onMenuClose();
|
||||
}}
|
||||
>
|
||||
Logout
|
||||
</MenuItem>
|
||||
</Menu>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export default AccountMenu;
|
37
frontend/src/components/TopBar.tsx
Normal file
37
frontend/src/components/TopBar.tsx
Normal file
@@ -0,0 +1,37 @@
|
||||
import AppBar from "@mui/material/AppBar";
|
||||
import Box from "@mui/material/Box";
|
||||
import Button from "@mui/material/Button";
|
||||
import Toolbar from "@mui/material/Toolbar";
|
||||
import React, { useContext } from "react";
|
||||
import { Link } from "react-router-dom";
|
||||
|
||||
import { AuthContext } from "src/AuthContext";
|
||||
import AccountMenu from "src/components/AccountMenu";
|
||||
|
||||
const sxToolbar = {
|
||||
paddingLeft: "env(safe-area-inset-left)",
|
||||
paddingRight: "env(safe-area-inset-right)",
|
||||
paddingTop: "env(safe-area-inset-top)",
|
||||
};
|
||||
|
||||
const TopBar = () => {
|
||||
const { authenticated } = useContext(AuthContext);
|
||||
|
||||
return (
|
||||
<>
|
||||
<AppBar position="fixed">
|
||||
<Toolbar sx={sxToolbar}>
|
||||
<Box sx={{ flexGrow: 1 }}>
|
||||
<Button color="inherit" component={Link} to="/">
|
||||
Todo
|
||||
</Button>
|
||||
</Box>
|
||||
{authenticated ? <AccountMenu /> : null}
|
||||
</Toolbar>
|
||||
</AppBar>
|
||||
<Toolbar sx={{ ...sxToolbar, marginBottom: 2 }} />
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export default TopBar;
|
Reference in New Issue
Block a user