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

168 lines
4.7 KiB
TypeScript
Raw Normal View History

2023-05-29 09:27:18 +00:00
import { Link, useLocation, useNavigate } from "react-router-dom";
2023-07-25 22:31:04 +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 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";
2023-08-03 12:14:31 +00:00
2023-07-07 13:53:08 +00:00
import { currencyFormatter } from "@root/utils/currencyFormatter";
2022-11-21 18:19:51 +00:00
2023-08-03 12:14:31 +00:00
import walletIcon from "@root/assets/Icons/wallet_icon.svg";
2023-08-02 08:31:58 +00:00
import type { ReactNode } from "react";
2022-11-21 18:19:51 +00:00
interface Props {
2023-07-25 22:31:04 +00:00
isLoggedIn: boolean;
2023-08-02 08:31:58 +00:00
children: ReactNode;
2022-11-21 18:19:51 +00:00
}
2023-08-02 08:31:58 +00:00
export default function NavbarFull({ isLoggedIn, children }: Props) {
2023-07-25 22:31:04 +00:00
const theme = useTheme();
const navigate = useNavigate();
const location = useLocation();
2023-07-25 22:31:04 +00:00
const user = useUserStore((state) => state.user);
const cash = useUserStore((state) => state.userAccount?.wallet.cash) ?? 0;
2023-03-22 11:51:16 +00:00
2023-07-25 22:31:04 +00:00
async function handleLogoutClick() {
try {
await logout();
clearAuthToken();
clearUserData();
clearCustomTariffs();
navigate("/");
} catch (error: any) {
const message = getMessageFromFetchError(error, "Не удалось выйти");
if (message) enqueueSnackbar(message);
2023-05-29 09:27:18 +00:00
}
2023-07-25 22:31:04 +00:00
}
2022-11-21 18:19:51 +00:00
2023-08-02 08:31:58 +00:00
return (
<Box>
{isLoggedIn ? (
<Container
component="nav"
disableGutters
maxWidth={false}
sx={{
2023-08-08 15:47:02 +00:00
zIndex: 1,
2023-08-07 13:36:59 +00:00
position: "fixed",
top: "0",
2023-08-02 08:31:58 +00:00
px: "16px",
display: "flex",
height: "80px",
alignItems: "center",
gap: "60px",
bgcolor: "white",
borderBottom: "1px solid #E3E3E3",
}}
2023-07-25 22:31:04 +00:00
>
2023-08-02 08:31:58 +00:00
<Link to="/">
<PenaLogo width={124} />
</Link>
<Menu />
2023-08-03 12:14:31 +00:00
<Box sx={{ display: "flex", ml: "auto" }}>
2023-08-02 08:31:58 +00:00
<Drawers />
<IconButton
2023-08-03 12:14:31 +00:00
sx={{
display: "flex",
alignItems: "center",
ml: "20px",
bgcolor: "#F2F3F7",
borderRadius: "6px",
height: "36px",
width: "36px",
}}
2023-08-02 08:31:58 +00:00
onClick={() => navigate("/wallet")}
>
2023-08-03 12:14:31 +00:00
<img src={walletIcon} alt="wallet" />
2023-08-02 08:31:58 +00:00
</IconButton>
<Box sx={{ ml: "8px", whiteSpace: "nowrap" }}>
<Typography
sx={{
fontSize: "12px",
lineHeight: "14px",
color: theme.palette.grey3.main,
}}
>
Мой баланс
</Typography>
<Typography
variant="body2"
color={theme.palette.brightPurple.main}
>
{currencyFormatter.format(cash / 100)}
</Typography>
</Box>
<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",
}}
>
Личный кабинет
</Button>
</SectionWrapper>
</>
)}
<Box>{children}</Box>
</Box>
2023-07-25 22:31:04 +00:00
);
}