front-hub/src/pages/TariffConstructor/CustomTariffCard.tsx

124 lines
4.9 KiB
TypeScript
Raw Normal View History

2023-05-27 11:50:21 +00:00
import { Box, Divider, Typography, useMediaQuery, useTheme } from "@mui/material";
import CustomButton from "../../components/CustomButton";
2023-06-11 10:07:47 +00:00
import { Privilege } from "@root/model/privilege";
2023-05-27 11:50:21 +00:00
import TariffPrivilegeSlider from "./TariffItem";
2023-06-11 10:07:47 +00:00
import { createAndSendTariff, useCustomTariffsStore } from "@root/stores/customTariffs";
2023-06-16 20:09:56 +00:00
import { cardShadow } from "@root/utils/themes/shadow";
import { currencyFormatter } from "@root/utils/currencyFormatter";
2023-06-23 11:58:51 +00:00
import { devlog, getMessageFromFetchError } from "@frontend/kitui";
import { enqueueSnackbar } from "notistack";
2023-07-12 00:31:00 +00:00
import { findServiceDiscount } from "@root/utils/calcTariffPrices";
import { useDiscountStore } from "@root/stores/discounts";
import { useCartStore } from "@root/stores/cart";
2023-05-27 11:50:21 +00:00
interface Props {
serviceKey: string;
2023-06-11 10:07:47 +00:00
tariffs: Privilege[];
2023-05-27 11:50:21 +00:00
}
export default function CustomTariffCard({ serviceKey, tariffs }: Props) {
const theme = useTheme();
const upMd = useMediaQuery(theme.breakpoints.up("md"));
const summaryPrice = useCustomTariffsStore(state => state.summaryPrice);
const tariffPrice = summaryPrice[serviceKey] ?? 0;
2023-07-12 00:31:00 +00:00
let discounted = tariffPrice;
const discounts = useDiscountStore.getState().discounts;
const cartservice = useCartStore.getState().cart.services.find(e => e.serviceKey === serviceKey)?.price;
const serviceDiscount = findServiceDiscount(serviceKey, tariffPrice + (cartservice || 0), discounts);
if (serviceDiscount) discounted *= serviceDiscount.target.factor;
2023-06-23 11:58:51 +00:00
async function handleConfirmClick() {
createAndSendTariff(serviceKey).then(result => {
devlog(result);
enqueueSnackbar("Тариф создан");
}).catch(error => {
const message = getMessageFromFetchError(error, "Не удалось создать тариф");
if (message) enqueueSnackbar(message);
});
2023-06-11 10:07:47 +00:00
}
2023-05-27 11:50:21 +00:00
return (
<Box sx={{
backgroundColor: "white",
width: "100%",
display: "flex",
flexDirection: upMd ? "row" : "column",
borderRadius: "12px",
2023-06-16 20:09:56 +00:00
boxShadow: cardShadow,
2023-05-27 11:50:21 +00:00
}}>
<Box sx={{
width: upMd ? "68.5%" : undefined,
p: "20px",
pr: upMd ? "35px" : undefined,
display: "flex",
flexWrap: "wrap",
flexDirection: "column",
gap: "25px",
}}>
{tariffs.map(tariff =>
<TariffPrivilegeSlider
key={tariff._id}
tariff={tariff}
/>
)}
</Box>
2023-06-11 10:07:47 +00:00
{!upMd && <Divider sx={{ mx: "20px", my: "10px", borderColor: theme.palette.grey2.main }} />}
2023-05-27 11:50:21 +00:00
<Box sx={{
display: "flex",
flexDirection: "column",
justifyContent: "space-between",
alignItems: "start",
color: theme.palette.grey3.main,
width: upMd ? "31.5%" : undefined,
p: "20px",
pl: upMd ? "33px" : undefined,
borderLeft: upMd ? `1px solid ${theme.palette.grey2.main}` : undefined,
}}>
<Box sx={{
display: "flex",
justifyContent: "space-between",
gap: "15%",
mb: "auto",
width: "100%",
}}>
<Typography>Чем больше пакеты, тем дешевле подписки и опции </Typography>
<Box sx={{
px: "6.7px",
height: "36px",
color: "white",
backgroundColor: theme.palette.orange.main,
display: "flex",
justifyContent: "center",
alignItems: "center",
borderRadius: "8px",
}}>
{"-60%"}
</Box>
</Box>
<Typography mb="20px" mt="18px">
Сумма с учетом скидки
</Typography>
<Box sx={{
display: "flex",
alignItems: "center",
gap: "20px",
mb: "30px",
}}>
2023-07-12 00:31:00 +00:00
<Typography variant="price">{currencyFormatter.format(discounted / 100)}</Typography>
<Typography variant="oldPrice" pt="3px">{currencyFormatter.format(tariffPrice / 100)}</Typography>
2023-05-27 11:50:21 +00:00
</Box>
<CustomButton
variant="contained"
2023-06-11 10:07:47 +00:00
onClick={handleConfirmClick}
2023-05-27 11:50:21 +00:00
sx={{
backgroundColor: theme.palette.brightPurple.main,
}}
>
Выбрать
</CustomButton>
</Box>
</Box>
);
}