2023-08-24 13:10:47 +00:00
|
|
|
|
import { Alert, Box, Button, Typography, useMediaQuery, useTheme } from "@mui/material";
|
2023-06-30 15:35:31 +00:00
|
|
|
|
import { currencyFormatter } from "@root/utils/currencyFormatter";
|
2023-07-13 18:59:23 +00:00
|
|
|
|
import { payCart } from "@root/api/cart";
|
|
|
|
|
import { setUserAccount } from "@root/stores/user";
|
2023-07-07 13:53:08 +00:00
|
|
|
|
import { isAxiosError } from "axios";
|
|
|
|
|
import { enqueueSnackbar } from "notistack";
|
|
|
|
|
import { useNavigate } from "react-router-dom";
|
2023-07-22 11:31:49 +00:00
|
|
|
|
import { useState } from "react";
|
2023-07-07 13:53:08 +00:00
|
|
|
|
|
2023-07-14 12:47:36 +00:00
|
|
|
|
interface Props {
|
2023-08-23 13:24:47 +00:00
|
|
|
|
priceBeforeDiscounts: number;
|
|
|
|
|
priceAfterDiscounts: number;
|
2023-07-14 12:47:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-27 13:14:17 +00:00
|
|
|
|
export default function TotalPrice({ priceAfterDiscounts, priceBeforeDiscounts }: Props) {
|
2023-08-23 13:24:47 +00:00
|
|
|
|
const theme = useTheme();
|
|
|
|
|
const upMd = useMediaQuery(theme.breakpoints.up("md"));
|
|
|
|
|
const isMobile = useMediaQuery(theme.breakpoints.down(550));
|
|
|
|
|
const [notEnoughMoneyAmount, setNotEnoughMoneyAmount] = useState<number>(0);
|
|
|
|
|
const navigate = useNavigate();
|
2023-07-07 13:53:08 +00:00
|
|
|
|
|
2023-08-23 13:24:47 +00:00
|
|
|
|
function handlePayClick() {
|
|
|
|
|
payCart()
|
|
|
|
|
.then((result) => {
|
|
|
|
|
setUserAccount(result);
|
|
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
if (isAxiosError(error) && error.response?.status === 402) {
|
|
|
|
|
const notEnoughMoneyAmount = parseInt(
|
2023-08-27 13:14:17 +00:00
|
|
|
|
(error.response.data.message as string).replace("insufficient funds: ", "")
|
2023-08-23 13:24:47 +00:00
|
|
|
|
);
|
|
|
|
|
setNotEnoughMoneyAmount(notEnoughMoneyAmount);
|
|
|
|
|
} else {
|
|
|
|
|
const message = getMessageFromFetchError(error);
|
|
|
|
|
if (message) enqueueSnackbar(message);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-03-27 12:45:44 +00:00
|
|
|
|
|
2023-08-23 13:24:47 +00:00
|
|
|
|
function handleReplenishWallet() {
|
|
|
|
|
navigate("/payment", { state: { notEnoughMoneyAmount } });
|
|
|
|
|
}
|
2023-07-07 13:53:08 +00:00
|
|
|
|
|
2023-08-23 13:24:47 +00:00
|
|
|
|
return (
|
|
|
|
|
<Box
|
|
|
|
|
sx={{
|
|
|
|
|
display: "flex",
|
|
|
|
|
flexDirection: upMd ? "row" : "column",
|
|
|
|
|
mt: upMd ? "50px" : "70px",
|
|
|
|
|
pt: upMd ? "30px" : undefined,
|
2023-08-28 11:50:58 +00:00
|
|
|
|
borderTop: upMd ? `1px solid ${theme.palette.gray.dark}` : undefined,
|
2023-08-23 13:24:47 +00:00
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Box
|
|
|
|
|
sx={{
|
|
|
|
|
width: upMd ? "68.5%" : undefined,
|
|
|
|
|
pr: upMd ? "15%" : undefined,
|
|
|
|
|
display: "flex",
|
|
|
|
|
flexWrap: "wrap",
|
|
|
|
|
flexDirection: "column",
|
|
|
|
|
}}
|
|
|
|
|
>
|
2023-08-21 15:58:48 +00:00
|
|
|
|
<Typography variant="h4" mb={upMd ? "18px" : "30px"}>
|
2023-08-23 13:24:47 +00:00
|
|
|
|
Итоговая цена
|
|
|
|
|
</Typography>
|
2023-08-21 15:58:48 +00:00
|
|
|
|
<Typography color={theme.palette.gray.main}>
|
2023-08-22 10:28:22 +00:00
|
|
|
|
Текст-заполнитель — это текст, который имеет Текст-заполнитель — это текст, который имеет Текст-заполнитель —
|
|
|
|
|
это текст, который имеет Текст-заполнитель — это текст, который имеет Текст-заполнитель
|
2023-08-23 13:24:47 +00:00
|
|
|
|
</Typography>
|
|
|
|
|
</Box>
|
|
|
|
|
<Box
|
|
|
|
|
sx={{
|
2023-08-22 10:28:22 +00:00
|
|
|
|
color: theme.palette.gray.dark,
|
2023-08-23 13:24:47 +00:00
|
|
|
|
width: upMd ? "31.5%" : undefined,
|
|
|
|
|
pl: upMd ? "33px" : undefined,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Box
|
|
|
|
|
sx={{
|
2023-07-13 18:59:23 +00:00
|
|
|
|
display: "flex",
|
2023-08-23 13:24:47 +00:00
|
|
|
|
flexDirection: upMd ? "column" : "row",
|
|
|
|
|
alignItems: upMd ? "start" : "center",
|
|
|
|
|
mt: upMd ? "10px" : "30px",
|
|
|
|
|
mb: "15px",
|
|
|
|
|
gap: "15px",
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Typography variant="oldPrice" sx={{ order: upMd ? 1 : 2 }}>
|
|
|
|
|
{currencyFormatter.format(priceBeforeDiscounts / 100)}
|
|
|
|
|
</Typography>
|
|
|
|
|
<Typography
|
|
|
|
|
variant="price"
|
|
|
|
|
sx={{
|
|
|
|
|
fontWeight: 500,
|
|
|
|
|
fontSize: "26px",
|
|
|
|
|
lineHeight: "31px",
|
|
|
|
|
order: upMd ? 2 : 1,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{currencyFormatter.format(priceAfterDiscounts / 100)}
|
|
|
|
|
</Typography>
|
2023-07-07 13:53:08 +00:00
|
|
|
|
</Box>
|
2023-08-23 13:24:47 +00:00
|
|
|
|
{notEnoughMoneyAmount > 0 && (
|
|
|
|
|
<Alert severity="error" variant="filled">
|
|
|
|
|
Нехватает {currencyFormatter.format(notEnoughMoneyAmount / 100)}
|
|
|
|
|
</Alert>
|
|
|
|
|
)}
|
2023-08-28 11:50:58 +00:00
|
|
|
|
<Button
|
|
|
|
|
variant="pena-contained-dark"
|
2023-08-27 13:14:17 +00:00
|
|
|
|
onClick={notEnoughMoneyAmount === 0 ? handlePayClick : handleReplenishWallet}
|
2023-08-28 11:50:58 +00:00
|
|
|
|
sx={{ mt: "10px" }}
|
|
|
|
|
>{notEnoughMoneyAmount === 0 ? "Оплатить" : "Пополнить"}</Button>
|
2023-08-23 13:24:47 +00:00
|
|
|
|
</Box>
|
|
|
|
|
</Box>
|
|
|
|
|
);
|
2023-03-27 12:45:44 +00:00
|
|
|
|
}
|