front-hub/src/components/Drawers.tsx

320 lines
10 KiB
TypeScript
Raw Normal View History

2023-08-29 11:54:35 +00:00
import { useState, useRef } from "react";
2023-08-30 09:56:14 +00:00
import {
Typography,
Drawer,
useMediaQuery,
useTheme,
Box,
IconButton,
Badge,
Button,
} from "@mui/material";
2023-08-29 11:54:35 +00:00
import { getMessageFromFetchError } from "@frontend/kitui";
2023-03-27 12:45:44 +00:00
import SectionWrapper from "./SectionWrapper";
import CustomWrapperDrawer from "./CustomWrapperDrawer";
2023-08-04 13:49:35 +00:00
import { NotificationsModal } from "./NotificationsModal";
2023-06-30 15:35:31 +00:00
import { useCart } from "@root/utils/hooks/useCart";
import { currencyFormatter } from "@root/utils/currencyFormatter";
2023-08-30 09:56:14 +00:00
import {
closeCartDrawer,
openCartDrawer,
useCartStore,
} from "@root/stores/cart";
2023-08-27 13:14:17 +00:00
import { setUserAccount, useUserStore } from "@root/stores/user";
2023-08-29 11:54:35 +00:00
import { useTicketStore } from "@root/stores/tickets";
2023-08-03 12:14:31 +00:00
2023-08-04 09:34:14 +00:00
import { ReactComponent as BellIcon } from "@root/assets/Icons/bell.svg";
import { ReactComponent as CartIcon } from "@root/assets/Icons/cart.svg";
2023-08-10 09:26:44 +00:00
import { ReactComponent as CrossIcon } from "@root/assets/Icons/cross.svg";
2023-03-27 12:45:44 +00:00
2023-08-27 13:14:17 +00:00
import { payCart } from "@root/api/cart";
import { isAxiosError } from "axios";
import { enqueueSnackbar } from "notistack";
2023-08-28 12:09:25 +00:00
import { Link, useNavigate } from "react-router-dom";
2023-07-28 13:24:21 +00:00
export default function Drawers() {
2023-08-30 09:56:14 +00:00
const [openNotificationsModal, setOpenNotificationsModal] =
useState<boolean>(false);
2023-08-04 13:49:35 +00:00
const bellRef = useRef<HTMLButtonElement | null>(null);
2023-07-25 22:31:04 +00:00
const navigate = useNavigate();
const theme = useTheme();
const upMd = useMediaQuery(theme.breakpoints.up("md"));
2023-08-23 09:27:57 +00:00
const isTablet = useMediaQuery(theme.breakpoints.down(1000));
2023-07-25 22:31:04 +00:00
const isDrawerOpen = useCartStore((state) => state.isDrawerOpen);
const cart = useCart();
2023-07-28 13:24:21 +00:00
const userAccount = useUserStore((state) => state.userAccount);
2023-08-29 11:54:35 +00:00
const tickets = useTicketStore((state) => state.tickets);
2023-08-27 13:14:17 +00:00
const [notEnoughMoneyAmount, setNotEnoughMoneyAmount] = useState<number>(0);
2023-08-03 12:14:31 +00:00
2023-08-07 15:16:34 +00:00
const notificationsCount = tickets.filter(
2023-08-30 09:56:14 +00:00
({ user, top_message }) =>
user !== top_message.user_id && top_message.shown.me !== 1
2023-08-07 15:16:34 +00:00
).length;
2023-08-30 09:56:14 +00:00
async function handlePayClick() {
const [payCartResponse, payCartError] = await payCart();
if (payCartError) {
const notEnoughMoneyAmount = parseInt(
payCartError.replace("insufficient funds: ", "")
);
setNotEnoughMoneyAmount(notEnoughMoneyAmount);
return enqueueSnackbar(payCartError);
}
if (payCartResponse) {
setUserAccount(payCartResponse);
}
2023-08-27 13:14:17 +00:00
}
function handleReplenishWallet() {
navigate("/payment", { state: { notEnoughMoneyAmount } });
}
2023-08-30 09:56:14 +00:00
return (
<Box sx={{ display: "flex", gap: isTablet ? "10px" : "20px" }}>
<IconButton
ref={bellRef}
aria-label="cart"
onClick={() => setOpenNotificationsModal((isOpened) => !isOpened)}
sx={{
cursor: "pointer",
borderRadius: "6px",
background: openNotificationsModal
? theme.palette.purple.main
: theme.palette.background.default,
"& .MuiBadge-badge": {
background: openNotificationsModal
? theme.palette.background.default
: theme.palette.purple.main,
color: openNotificationsModal
? theme.palette.purple.main
: theme.palette.background.default,
},
"& svg > path:first-child": {
fill: openNotificationsModal ? "#FFFFFF" : "#9A9AAF",
},
"& svg > path:last-child": {
stroke: openNotificationsModal ? "#FFFFFF" : "#9A9AAF",
},
"&:hover": {
background: theme.palette.purple.main,
"& .MuiBox-root": {
background: theme.palette.purple.main,
},
"& .MuiBadge-badge": {
background: theme.palette.background.default,
color: theme.palette.purple.main,
},
"& svg > path:first-child": { fill: "#FFFFFF" },
"& svg > path:last-child": { stroke: "#FFFFFF" },
},
}}
>
<Badge
badgeContent={notificationsCount}
sx={{
"& .MuiBadge-badge": {
display: notificationsCount ? "flex" : "none",
color: "#FFFFFF",
background: theme.palette.purple.main,
transform: "scale(0.8) translate(50%, -50%)",
top: "2px",
right: "2px",
fontWeight: 400,
},
}}
>
<BellIcon />
</Badge>
</IconButton>
<NotificationsModal
open={openNotificationsModal}
setOpen={setOpenNotificationsModal}
anchorElement={bellRef.current}
notifications={tickets
.filter(({ user, top_message }) => user !== top_message.user_id)
.map((ticket) => ({
text: "У вас новое сообщение от техподдержки",
date: new Date(ticket.updated_at).toLocaleDateString(),
url: `/support/${ticket.id}`,
watched:
ticket.user === ticket.top_message.user_id ||
ticket.top_message.shown.me === 1,
}))}
/>
<IconButton
onClick={openCartDrawer}
component="div"
sx={{
cursor: "pointer",
background: theme.palette.background.default,
borderRadius: "6px",
"&:hover": {
background: theme.palette.purple.main,
"& .MuiBox-root": {
background: theme.palette.purple.main,
},
"& .MuiBadge-badge": {
background: theme.palette.background.default,
color: theme.palette.purple.main,
},
"& svg > path:nth-child(1)": { fill: "#FFFFFF" },
"& svg > path:nth-child(2)": { fill: "#FFFFFF" },
"& svg > path:nth-child(3)": { stroke: "#FFFFFF" },
},
}}
>
<Badge
badgeContent={userAccount?.cart.length}
sx={{
"& .MuiBadge-badge": {
display: userAccount?.cart.length ? "flex" : "none",
color: "#FFFFFF",
background: theme.palette.purple.main,
transform: "scale(0.8) translate(50%, -50%)",
top: "2px",
right: "2px",
fontWeight: 400,
},
}}
>
<CartIcon />
</Badge>
</IconButton>
<Drawer
anchor={"right"}
open={isDrawerOpen}
onClose={closeCartDrawer}
sx={{ background: "rgba(0, 0, 0, 0.55)" }}
>
<SectionWrapper
maxWidth="lg"
sx={{
pl: "0px",
pr: "0px",
width: "450px",
}}
>
<Box
sx={{
width: "100%",
pt: "12px",
pb: "12px",
display: "flex",
justifyContent: "space-between",
bgcolor: "#F2F3F7",
gap: "10px",
pl: "20px",
pr: "20px",
}}
>
<Typography
component="div"
sx={{
fontSize: "18px",
lineHeight: "21px",
font: "Rubick",
}}
>
2023-08-30 09:56:14 +00:00
Корзина
</Typography>
<IconButton onClick={closeCartDrawer} sx={{ p: 0 }}>
<CrossIcon />
</IconButton>
2023-08-30 09:56:14 +00:00
</Box>
<Box sx={{ pl: "20px", pr: "20px" }}>
{cart.services.map((serviceData) => (
<CustomWrapperDrawer
key={serviceData.serviceKey}
serviceData={serviceData}
/>
))}
<Box
sx={{
mt: "40px",
pt: upMd ? "30px" : undefined,
borderTop: upMd
? `1px solid ${theme.palette.gray.main}`
: undefined,
}}
>
<Box
2023-07-25 22:31:04 +00:00
sx={{
2023-08-30 09:56:14 +00:00
width: upMd ? "100%" : undefined,
display: "flex",
flexWrap: "wrap",
flexDirection: "column",
2023-07-25 22:31:04 +00:00
}}
2023-08-30 09:56:14 +00:00
>
<Typography variant="h4" mb={upMd ? "18px" : "30px"}>
Итоговая цена
</Typography>
<Typography color={theme.palette.gray.dark}>
Текст-заполнитель это текст, который имеет Текст-заполнитель
это текст, который имеет Текст-заполнитель это текст,
который имеет Текст-заполнитель это текст, который имеет
Текст-заполнитель
</Typography>
</Box>
<Box
sx={{
color: theme.palette.gray.dark,
pb: "100px",
pt: "38px",
}}
>
<Box
sx={{
display: "flex",
flexDirection: upMd ? "column" : "row",
alignItems: upMd ? "start" : "center",
mt: upMd ? "10px" : "30px",
gap: "15px",
}}
>
<Typography
color={theme.palette.orange.main}
2023-03-27 12:45:44 +00:00
sx={{
2023-08-30 09:56:14 +00:00
textDecoration: "line-through",
order: upMd ? 1 : 2,
2023-03-27 12:45:44 +00:00
}}
2023-08-30 09:56:14 +00:00
>
{currencyFormatter.format(cart.priceBeforeDiscounts / 100)}
</Typography>
<Typography
variant="p1"
2023-03-27 12:45:44 +00:00
sx={{
2023-08-30 09:56:14 +00:00
fontWeight: 500,
fontSize: "26px",
lineHeight: "31px",
order: upMd ? 2 : 1,
2023-03-27 12:45:44 +00:00
}}
2023-08-30 09:56:14 +00:00
>
{currencyFormatter.format(cart.priceAfterDiscounts / 100)}
</Typography>
</Box>
<Button
variant="pena-contained-dark"
component={Link}
to="/cart"
onClick={
notEnoughMoneyAmount === 0
? handlePayClick
: handleReplenishWallet
}
sx={{ mt: "25px" }}
2023-03-27 12:45:44 +00:00
>
2023-08-30 09:56:14 +00:00
Оплатить
</Button>
</Box>
</Box>
</Box>
</SectionWrapper>
</Drawer>
</Box>
);
2023-03-27 12:45:44 +00:00
}