front-hub/src/components/Navbar/NavbarFull.tsx

129 lines
3.6 KiB
TypeScript
Raw Normal View History

2023-05-29 09:27:18 +00:00
import { Link, useLocation, useNavigate } from "react-router-dom";
2023-05-26 11:19:10 +00:00
import { Box, Button, Container, IconButton, Typography, useTheme } from "@mui/material";
2022-11-21 18:19:51 +00:00
import SectionWrapper from "../SectionWrapper";
2023-03-22 11:51:16 +00:00
import LogoutIcon from "../icons/LogoutIcon";
import WalletIcon from "../icons/WalletIcon";
import CustomAvatar from "./Avatar";
2023-03-27 12:45:44 +00:00
import Drawers from "../Drawers";
2023-03-22 11:51:16 +00:00
import PenaLogo from "../PenaLogo";
2023-03-27 12:45:44 +00:00
import Menu from "../Menu";
2023-05-29 09:27:18 +00:00
import { logout } from "@root/api/auth";
import { enqueueSnackbar } from "notistack";
2023-06-24 18:17:43 +00:00
import { clearUserData, useUserStore } from "@root/stores/user";
import { clearAuthToken, getMessageFromFetchError } from "@frontend/kitui";
2023-07-03 12:59:09 +00:00
import { clearCustomTariffs } from "@root/stores/customTariffs";
2022-11-21 18:19:51 +00:00
interface Props {
isLoggedIn: boolean;
2022-11-21 18:19:51 +00:00
}
export default function NavbarFull({ isLoggedIn }: Props) {
const theme = useTheme();
const location = useLocation();
const navigate = useNavigate();
const user = useUserStore((state) => state.user);
2023-03-22 11:51:16 +00:00
async function handleLogoutClick() {
try {
await logout();
2023-06-24 18:17:43 +00:00
clearAuthToken();
clearUserData();
2023-07-03 12:59:09 +00:00
clearCustomTariffs();
navigate("/");
} catch (error: any) {
const message = getMessageFromFetchError(error, "Не удалось выйти");
if (message) enqueueSnackbar(message);
2023-05-29 09:27:18 +00:00
}
}
2022-11-21 18:19:51 +00:00
return isLoggedIn ? (
<Container
component="nav"
disableGutters
maxWidth={false}
sx={{
px: "16px",
display: "flex",
height: "80px",
alignItems: "center",
gap: "60px",
bgcolor: "white",
borderBottom: "1px solid #E3E3E3",
}}
>
<Link to="/"><PenaLogo width={124} /></Link>
<Menu />
<Box
sx={{
display: "flex",
ml: "auto",
}}
>
<Drawers />
<IconButton sx={{ p: 0, ml: "8px" }}>
<WalletIcon color={theme.palette.grey2.main} bgcolor="#F2F3F7" />
</IconButton>
<Box sx={{ ml: "8px", whiteSpace: "nowrap" }}>
<Typography
2022-11-21 18:19:51 +00:00
sx={{
fontSize: "12px",
lineHeight: "14px",
color: theme.palette.grey3.main,
2022-11-21 18:19:51 +00:00
}}
>
Мой баланс
</Typography>
<Typography variant="body2" color={theme.palette.brightPurple.main}>
00.00 руб.
</Typography>
</Box>
2023-06-24 12:00:55 +00:00
<CustomAvatar />
<IconButton
onClick={handleLogoutClick}
sx={{ ml: "20px", bgcolor: "#F2F3F7", borderRadius: "6px", height: "36px", width: "36px" }}
>
<LogoutIcon />
</IconButton>
</Box>
</Container>
) : (
<>
<SectionWrapper
component="nav"
maxWidth="lg"
outerContainerSx={{
backgroundColor: theme.palette.lightPurple.main,
borderBottom: "1px solid #E3E3E3",
}}
sx={{
px: "20px",
display: "flex",
justifyContent: "space-between",
height: "80px",
alignItems: "center",
gap: "50px",
}}
>
<PenaLogo width={150} />
<Menu />
<Button
component={Link}
to={user ? "/tariffs" : "/signin"}
state={user ? undefined : { backgroundLocation: location }}
variant="outlined"
sx={{
px: "18px",
py: "10px",
borderColor: "white",
borderRadius: "8px",
whiteSpace: "nowrap",
minWidth: "180px",
}}
2022-11-21 18:19:51 +00:00
>
Личный кабинет
</Button>
</SectionWrapper>
</>
);
}