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

145 lines
3.9 KiB
TypeScript
Raw Normal View History

2023-07-28 13:24:21 +00:00
import {
2023-08-30 14:30:41 +00:00
Box,
Button,
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-30 14:30:41 +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 {
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) {
const theme = useTheme();
const upMd = useMediaQuery(theme.breakpoints.up("md"));
2023-08-30 14:30:41 +00:00
const summaryPriceBeforeDiscounts = useCustomTariffsStore(
(state) => state.summaryPriceBeforeDiscountsMap
);
const summaryPriceAfterDiscounts = useCustomTariffsStore(
(state) => state.summaryPriceAfterDiscountsMap
);
const priceBeforeDiscounts = summaryPriceBeforeDiscounts[serviceKey] ?? 0;
const priceAfterDiscounts = summaryPriceAfterDiscounts[serviceKey] ?? 0;
2023-05-27 11:50:21 +00:00
async function handleConfirmClick() {
2023-08-30 14:30:41 +00:00
const [createTariffResponse, createTariffError] = await createAndSendTariff(
serviceKey
2023-08-09 17:14:12 +00:00
);
2023-08-30 14:30:41 +00:00
if (createTariffError) {
return enqueueSnackbar(createTariffError);
}
if (createTariffResponse) {
updateTariffs([createTariffResponse]);
await addTariffToCart(createTariffResponse._id);
2023-05-27 11:50:21 +00:00
enqueueSnackbar("Тариф добавлен в корзину");
2023-08-09 17:14:12 +00:00
}
}
2023-08-09 17:14:12 +00:00
return (
<Box
sx={{
backgroundColor: "white",
width: "100%",
display: "flex",
flexDirection: upMd ? "row" : "column",
borderRadius: "12px",
boxShadow: cardShadow,
}}
>
<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>
2023-08-30 14:30:41 +00:00
{!upMd && (
<Divider
sx={{ mx: "20px", my: "10px", borderColor: theme.palette.gray.main }}
/>
)}
<Box
sx={{
display: "flex",
flexBasis: 0,
flexGrow: 1,
flexDirection: "column",
justifyContent: "space-between",
alignItems: "start",
color: theme.palette.gray.dark,
p: "20px",
pl: upMd ? "33px" : undefined,
borderLeft: upMd ? `1px solid ${theme.palette.gray.main}` : undefined,
}}
>
2023-07-28 13:24:21 +00:00
<Box
sx={{
display: "flex",
justifyContent: "space-between",
gap: "15%",
mb: "auto",
width: "100%",
}}
2023-07-28 13:24:21 +00:00
>
2023-08-30 14:30:41 +00:00
<Typography>
Чем больше пакеты, тем дешевле подписки и опции{" "}
</Typography>
2023-05-27 11:50:21 +00:00
</Box>
<Typography mb="20px" mt="18px">
Сумма с учетом скидки
</Typography>
<Box
sx={{
display: "flex",
alignItems: "center",
gap: "20px",
mb: "30px",
}}
>
2023-08-30 14:30:41 +00:00
<Typography variant="price">
{currencyFormatter.format(priceAfterDiscounts / 100)}
</Typography>
<Typography variant="oldPrice" pt="3px">
{currencyFormatter.format(priceBeforeDiscounts / 100)}
</Typography>
</Box>
2023-08-30 14:30:41 +00:00
<Button
disabled={!priceBeforeDiscounts}
variant="pena-contained-dark"
onClick={handleConfirmClick}
>
Выбрать
</Button>
</Box>
</Box>
);
2023-05-27 11:50:21 +00:00
}