front-hub/src/components/TotalPrice.tsx

181 lines
6.4 KiB
TypeScript
Raw Normal View History

2023-12-15 21:53:29 +00:00
import { useState } from "react";
import { useNavigate } from "react-router-dom";
import { enqueueSnackbar } from "notistack";
import { Alert, Box, Button, Badge, Typography, useMediaQuery, useTheme } from "@mui/material";
2023-10-06 11:56:50 +00:00
2023-12-15 21:53:29 +00:00
import { Loader } from "./Loader";
2023-10-06 11:56:50 +00:00
2023-12-15 21:53:29 +00:00
import { currencyFormatter } from "@root/utils/currencyFormatter";
import { payCart } from "@root/api/cart";
import { setUserAccount } from "@root/stores/user";
import { setNotEnoughMoneyAmount, useCartStore } from "@root/stores/cart";
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;
isConstructor?: boolean;
2023-07-14 12:47:36 +00:00
}
export default function TotalPrice({ priceAfterDiscounts, priceBeforeDiscounts, isConstructor = false }: Props) {
2023-12-15 21:53:29 +00:00
const theme = useTheme();
const upMd = useMediaQuery(theme.breakpoints.up("md"));
const notEnoughMoneyAmount = useCartStore(state => state.notEnoughMoneyAmount);
2023-12-15 21:53:29 +00:00
const [loading, setLoading] = useState<boolean>(false);
const navigate = useNavigate();
2023-07-07 13:53:08 +00:00
2024-03-30 22:26:42 +00:00
console.log("TOTALPRICE", priceAfterDiscounts)
2023-12-15 21:53:29 +00:00
async function handlePayClick() {
setLoading(true);
2023-10-06 11:56:50 +00:00
2023-12-15 21:53:29 +00:00
const [payCartResponse, payCartError] = await payCart();
2023-08-30 09:56:14 +00:00
2023-12-15 21:53:29 +00:00
if (payCartError) {
if (payCartError.includes("insufficient funds: ")) {
const notEnoughMoneyAmount = parseInt(payCartError.replace(/^.*insufficient\sfunds:\s(?=\d+$)/, ""));
2023-08-30 09:56:14 +00:00
console.log(notEnoughMoneyAmount);
2023-12-15 21:53:29 +00:00
setNotEnoughMoneyAmount(notEnoughMoneyAmount);
}
2023-10-26 11:27:41 +00:00
2023-12-15 21:53:29 +00:00
setLoading(false);
if (!payCartError.includes("insufficient funds: ")) enqueueSnackbar(payCartError);
return
2023-12-15 21:53:29 +00:00
}
2023-08-30 09:56:14 +00:00
2023-12-15 21:53:29 +00:00
if (payCartResponse) {
setUserAccount(payCartResponse);
}
2023-10-06 11:56:50 +00:00
2023-12-15 21:53:29 +00:00
setLoading(false);
}
2023-03-27 12:45:44 +00:00
2023-12-15 21:53:29 +00:00
function handleReplenishWallet() {
navigate("/payment", { state: { notEnoughMoneyAmount } });
}
2023-07-07 13:53:08 +00:00
2023-12-15 21:53:29 +00:00
return (
<Box
sx={{
display: "flex",
flexDirection: upMd ? "row" : "column",
mt: upMd ? "80px" : "70px",
pt: upMd ? "30px" : undefined,
borderTop: upMd ? `1px solid ${theme.palette.gray.dark}` : undefined,
}}
>
<Box
sx={{
width: upMd ? "68.5%" : undefined,
pr: upMd ? "15%" : undefined,
display: "flex",
flexWrap: "wrap",
flexDirection: "column",
}}
>
<Typography variant="h4" mb={upMd ? "18px" : "30px"}>
2023-08-23 13:24:47 +00:00
Итоговая цена
2023-12-15 21:53:29 +00:00
</Typography>
{!isConstructor &&
<>
<Typography color={theme.palette.gray.main} mb={"20px"}>
Здесь написана окончательная стоимость всех услуг сложенных в корзину с учётом всех скидок.
</Typography>
<Typography color={theme.palette.gray.main}>
После нажатия кнопки оплатить, вы будете перенаправлены на форму оплаты, для оплаты ВСЕЙ корзины (рекомендуем перед оплатой, убрать все лишнее)
</Typography>
</>
}
2023-12-15 21:53:29 +00:00
</Box>
<Box
sx={{
color: theme.palette.gray.dark,
width: upMd ? "31.5%" : undefined,
display: "flex",
alignItems: upMd ? "center" : "start",
flexDirection: upMd ? "row" : "column",
gap: "30px"
2023-12-15 21:53:29 +00:00
}}
>
<Badge
badgeContent={
(priceAfterDiscounts - priceBeforeDiscounts) ? (
<span
style={{
backgroundColor: "#ff4904",
color: "white",
fontSize: "14px",
}}
>
-
{`${((priceBeforeDiscounts - priceAfterDiscounts) / (priceBeforeDiscounts / 100)).toFixed(0)}%`}
</span>
) : null
}
color="success"
sx={{
"& .MuiBadge-dot": {
backgroundColor: "#ff4904",
width: "10px",
height: "10px",
},
"& .MuiBadge-anchorOriginTopRightRectangle": {
backgroundColor: "#ff4904",
top: "5px",
right: "5px",
},
"& .MuiBadge-anchorOriginTopRightRectangular": {
backgroundColor: "#ff4904",
height: "31px",
padding: "5px 10px",
right: "0px",
top: upMd ? 0 : "17px",
},
}}
2023-12-15 21:53:29 +00:00
>
<Box
sx={{
display: "flex",
flexDirection: upMd ? "column" : "row",
alignItems: upMd ? "start" : "center",
mt: upMd ? "10px" : "30px",
mb: "15px",
gap: "15px",
}}
>
<Typography variant="oldPrice" sx={{ opacity: (priceBeforeDiscounts - priceAfterDiscounts)?1:0 , order: upMd ? 1 : 2 }}>
2023-12-15 21:53:29 +00:00
{currencyFormatter.format(priceBeforeDiscounts / 100)}
</Typography>
<Typography
variant="price"
sx={{
fontWeight: 500,
fontSize: "26px",
lineHeight: "31px",
order: upMd ? 2 : 1,
}}
>
2024-04-12 16:02:47 +00:00
{currencyFormatter.format(Math.trunc(priceAfterDiscounts) / 100)}
2023-12-15 21:53:29 +00:00
</Typography>
</Box>
2023-12-15 19:49:10 +00:00
</Badge>
<Box sx={{display: "flex", alignItems: "center",
gap: "30px", flexWrap: "wrap",}}>
{notEnoughMoneyAmount > 0 && (
<Alert severity="error" variant="filled">
2024-04-12 16:02:47 +00:00
Не хватает {currencyFormatter.format(Math.trunc(notEnoughMoneyAmount) / 100)}
</Alert>
)}
<Button
variant="pena-contained-dark"
disabled = {priceAfterDiscounts === 0}
onClick={() => (notEnoughMoneyAmount === 0 ? !loading && handlePayClick() : handleReplenishWallet())}
>
{loading ? <Loader size={24} /> : notEnoughMoneyAmount === 0 ? "Оплатить" : "Пополнить"}
</Button>
</Box>
2023-12-15 21:53:29 +00:00
</Box>
</Box>
);
2023-03-27 12:45:44 +00:00
}