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

147 lines
4.9 KiB
TypeScript
Raw Normal View History

2023-07-28 13:24:21 +00:00
import {
2023-08-09 17:14:12 +00:00
Box,
Divider,
Typography,
useMediaQuery,
useTheme,
2023-07-28 13:24:21 +00:00
} from "@mui/material";
2023-05-27 11:50:21 +00:00
import CustomButton from "../../components/CustomButton";
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-06-16 20:09:56 +00:00
import { cardShadow } from "@root/utils/themes/shadow";
import { currencyFormatter } from "@root/utils/currencyFormatter";
2023-08-07 15:59:37 +00:00
import { Privilege, devlog, 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-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-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() {
createAndSendTariff(serviceKey)
.then((result) => {
devlog(result);
2023-08-11 10:50:30 +00:00
enqueueSnackbar("Тариф создан");
2023-06-11 10:07:47 +00:00
2023-08-11 10:50:30 +00:00
updateTariffs([result]);
2023-08-09 17:14:12 +00:00
})
.catch((error) => {
const message = getMessageFromFetchError(
error,
"Не удалось создать тариф"
);
if (message) enqueueSnackbar(message);
});
}
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
sx={{ mx: "20px", my: "10px", borderColor: theme.palette.grey2.main }}
/>
)}
<Box
sx={{
display: "flex",
flexBasis: 0,
flexGrow: 1,
flexDirection: "column",
justifyContent: "space-between",
alignItems: "start",
color: theme.palette.grey3.main,
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>
<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>
<CustomButton
variant="contained"
onClick={handleConfirmClick}
sx={{
backgroundColor: theme.palette.brightPurple.main,
}}
>
Выбрать
</CustomButton>
</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
}