2023-07-28 13:24:21 +00:00
|
|
|
|
import {
|
2023-08-09 17:14:12 +00:00
|
|
|
|
Box,
|
2023-08-24 13:10:47 +00:00
|
|
|
|
Button,
|
2023-08-09 17:14:12 +00:00
|
|
|
|
Divider,
|
|
|
|
|
Typography,
|
|
|
|
|
useMediaQuery,
|
|
|
|
|
useTheme,
|
2023-07-28 13:24:21 +00:00
|
|
|
|
} from "@mui/material";
|
2023-05-27 11:50:21 +00:00
|
|
|
|
import TariffPrivilegeSlider from "./TariffItem";
|
2023-07-28 13:24:21 +00:00
|
|
|
|
import {
|
2023-08-09 17:14:12 +00:00
|
|
|
|
createAndSendTariff,
|
|
|
|
|
useCustomTariffsStore,
|
2023-07-28 13:24:21 +00:00
|
|
|
|
} from "@root/stores/customTariffs";
|
2023-08-22 10:28:22 +00:00
|
|
|
|
import { cardShadow } from "@root/utils/theme";
|
2023-06-16 20:09:56 +00:00
|
|
|
|
import { currencyFormatter } from "@root/utils/currencyFormatter";
|
2023-08-11 13:56:53 +00:00
|
|
|
|
import { Privilege, getMessageFromFetchError } from "@frontend/kitui";
|
2023-06-23 11:58:51 +00:00
|
|
|
|
import { enqueueSnackbar } from "notistack";
|
2023-08-09 17:14:12 +00:00
|
|
|
|
import { updateTariffs } from "@root/stores/tariffs";
|
2023-08-11 13:56:53 +00:00
|
|
|
|
import { addTariffToCart } from "@root/stores/user";
|
2023-05-27 11:50:21 +00:00
|
|
|
|
|
|
|
|
|
interface Props {
|
2023-08-09 17:14:12 +00:00
|
|
|
|
serviceKey: string;
|
|
|
|
|
privileges: Privilege[];
|
2023-05-27 11:50:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-27 16:51:12 +00:00
|
|
|
|
export default function CustomTariffCard({ serviceKey, privileges }: Props) {
|
2023-08-09 17:14:12 +00:00
|
|
|
|
const theme = useTheme();
|
|
|
|
|
const upMd = useMediaQuery(theme.breakpoints.up("md"));
|
|
|
|
|
const summaryPriceBeforeDiscounts = useCustomTariffsStore(
|
|
|
|
|
(state) => state.summaryPriceBeforeDiscountsMap
|
|
|
|
|
);
|
|
|
|
|
const summaryPriceAfterDiscounts = useCustomTariffsStore(
|
|
|
|
|
(state) => state.summaryPriceAfterDiscountsMap
|
|
|
|
|
);
|
2023-07-13 18:59:23 +00:00
|
|
|
|
|
2023-08-09 17:14:12 +00:00
|
|
|
|
const priceBeforeDiscounts = summaryPriceBeforeDiscounts[serviceKey] ?? 0;
|
|
|
|
|
const priceAfterDiscounts = summaryPriceAfterDiscounts[serviceKey] ?? 0;
|
2023-05-27 11:50:21 +00:00
|
|
|
|
|
2023-08-09 17:14:12 +00:00
|
|
|
|
async function handleConfirmClick() {
|
2023-08-30 11:31:49 +00:00
|
|
|
|
const [createTariffResponse, createTariffError] = await createAndSendTariff(
|
|
|
|
|
serviceKey
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (createTariffError) {
|
|
|
|
|
return enqueueSnackbar(createTariffError);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (createTariffResponse) {
|
|
|
|
|
updateTariffs([createTariffResponse]);
|
|
|
|
|
|
|
|
|
|
await addTariffToCart(createTariffResponse._id);
|
|
|
|
|
|
2023-08-12 14:56:51 +00:00
|
|
|
|
enqueueSnackbar("Тариф добавлен в корзину");
|
2023-08-11 13:56:53 +00:00
|
|
|
|
}
|
2023-08-09 17:14:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
2023-07-28 13:24:21 +00:00
|
|
|
|
<Box
|
2023-08-09 17:14:12 +00:00
|
|
|
|
sx={{
|
|
|
|
|
backgroundColor: "white",
|
|
|
|
|
width: "100%",
|
|
|
|
|
display: "flex",
|
|
|
|
|
flexDirection: upMd ? "row" : "column",
|
|
|
|
|
borderRadius: "12px",
|
|
|
|
|
boxShadow: cardShadow,
|
|
|
|
|
}}
|
2023-07-28 13:24:21 +00:00
|
|
|
|
>
|
2023-08-09 17:14:12 +00:00
|
|
|
|
<Box
|
|
|
|
|
sx={{
|
|
|
|
|
p: "20px",
|
|
|
|
|
pr: upMd ? "35px" : undefined,
|
|
|
|
|
display: "flex",
|
|
|
|
|
flexBasis: 0,
|
|
|
|
|
flexGrow: 2.37,
|
|
|
|
|
flexWrap: "wrap",
|
|
|
|
|
flexDirection: "column",
|
|
|
|
|
gap: "25px",
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{privileges.map((privilege) => (
|
|
|
|
|
<TariffPrivilegeSlider key={privilege._id} privilege={privilege} />
|
|
|
|
|
))}
|
|
|
|
|
</Box>
|
|
|
|
|
{!upMd && (
|
|
|
|
|
<Divider
|
2023-08-22 10:28:22 +00:00
|
|
|
|
sx={{ mx: "20px", my: "10px", borderColor: theme.palette.gray.main }}
|
2023-08-09 17:14:12 +00:00
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
<Box
|
|
|
|
|
sx={{
|
|
|
|
|
display: "flex",
|
|
|
|
|
flexBasis: 0,
|
|
|
|
|
flexGrow: 1,
|
|
|
|
|
flexDirection: "column",
|
|
|
|
|
justifyContent: "space-between",
|
|
|
|
|
alignItems: "start",
|
2023-08-22 10:28:22 +00:00
|
|
|
|
color: theme.palette.gray.dark,
|
2023-08-09 17:14:12 +00:00
|
|
|
|
p: "20px",
|
|
|
|
|
pl: upMd ? "33px" : undefined,
|
|
|
|
|
borderLeft: upMd
|
2023-08-22 10:28:22 +00:00
|
|
|
|
? `1px solid ${theme.palette.gray.main}`
|
2023-08-09 17:14:12 +00:00
|
|
|
|
: undefined,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Box
|
|
|
|
|
sx={{
|
|
|
|
|
display: "flex",
|
|
|
|
|
justifyContent: "space-between",
|
|
|
|
|
gap: "15%",
|
|
|
|
|
mb: "auto",
|
|
|
|
|
width: "100%",
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Typography>
|
|
|
|
|
Чем больше пакеты, тем дешевле подписки и опции{" "}
|
|
|
|
|
</Typography>
|
|
|
|
|
</Box>
|
|
|
|
|
<Typography mb="20px" mt="18px">
|
|
|
|
|
Сумма с учетом скидки
|
|
|
|
|
</Typography>
|
|
|
|
|
<Box
|
|
|
|
|
sx={{
|
|
|
|
|
display: "flex",
|
|
|
|
|
alignItems: "center",
|
|
|
|
|
gap: "20px",
|
|
|
|
|
mb: "30px",
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Typography variant="price">
|
|
|
|
|
{currencyFormatter.format(priceAfterDiscounts / 100)}
|
|
|
|
|
</Typography>
|
|
|
|
|
<Typography variant="oldPrice" pt="3px">
|
|
|
|
|
{currencyFormatter.format(priceBeforeDiscounts / 100)}
|
|
|
|
|
</Typography>
|
|
|
|
|
</Box>
|
2023-08-24 13:10:47 +00:00
|
|
|
|
<Button
|
|
|
|
|
variant="pena-contained-dark"
|
2023-08-09 17:14:12 +00:00
|
|
|
|
onClick={handleConfirmClick}
|
2023-08-24 13:10:47 +00:00
|
|
|
|
>Выбрать</Button>
|
2023-08-09 17:14:12 +00:00
|
|
|
|
</Box>
|
2023-05-27 11:50:21 +00:00
|
|
|
|
</Box>
|
2023-08-09 17:14:12 +00:00
|
|
|
|
);
|
2023-05-27 11:50:21 +00:00
|
|
|
|
}
|