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

143 lines
4.5 KiB
TypeScript
Raw Normal View History

2023-08-07 15:59:37 +00:00
import { Privilege, PrivilegeValueType, useThrottle } from "@frontend/kitui";
import { Box, SliderProps, Typography, useMediaQuery, useTheme } from "@mui/material";
2023-07-28 13:24:21 +00:00
import { CustomSlider } from "@root/components/CustomSlider";
2023-07-27 16:51:12 +00:00
import NumberInputWithUnitAdornment from "@root/components/NumberInputWithUnitAdornment";
2023-05-27 11:50:21 +00:00
import CalendarIcon from "@root/components/icons/CalendarIcon";
import PieChartIcon from "@root/components/icons/PieChartIcon";
import { useCartStore } from "@root/stores/cart";
import { setCustomTariffsUserValue, useCustomTariffsStore } from "@root/stores/customTariffs";
import { useDiscountStore } from "@root/stores/discounts";
import { useUserStore } from "@root/stores/user";
2023-05-27 11:50:21 +00:00
import { getDeclension } from "@root/utils/declension";
2023-06-11 10:07:47 +00:00
import { useEffect, useState } from "react";
2023-05-27 11:50:21 +00:00
const sliderSettingsByType: Record<PrivilegeValueType, Partial<SliderProps>> = {
2023-07-28 13:24:21 +00:00
день: { max: 365 },
2023-08-01 00:56:03 +00:00
шаблон: { max: 5000 },
МБ: { max: 5000 },
2023-05-27 11:50:21 +00:00
};
interface Props {
2023-07-28 13:24:21 +00:00
privilege: Privilege;
2023-05-27 11:50:21 +00:00
}
2023-07-27 16:51:12 +00:00
export default function TariffPrivilegeSlider({ privilege }: Props) {
2023-07-28 13:24:21 +00:00
const theme = useTheme();
const upMd = useMediaQuery(theme.breakpoints.up("md"));
const userValue = useCustomTariffsStore((state) => state.userValuesMap[privilege.serviceKey]?.[privilege._id]) ?? 0;
2023-07-28 13:24:21 +00:00
const discounts = useDiscountStore((state) => state.discounts);
const currentCartTotal = useCartStore((state) => state.cart.priceAfterDiscounts);
const purchasesAmount = useUserStore((state) => state.userAccount?.wallet.purchasesAmount) ?? 0;
2023-10-26 12:33:03 +00:00
const isUserNko = useUserStore(state => state.userAccount?.status) === "nko";
2023-07-28 13:24:21 +00:00
const [value, setValue] = useState<number>(userValue);
const throttledValue = useThrottle(value, 200);
2023-05-27 11:50:21 +00:00
2023-07-28 13:24:21 +00:00
useEffect(
function setStoreValue() {
setCustomTariffsUserValue(
privilege.serviceKey,
privilege._id,
throttledValue,
discounts,
currentCartTotal,
2023-10-26 12:33:03 +00:00
purchasesAmount,
isUserNko,
2023-07-28 13:24:21 +00:00
);
},
2023-10-26 12:33:03 +00:00
[currentCartTotal, discounts, purchasesAmount, privilege._id, privilege.serviceKey, throttledValue, isUserNko]
2023-07-28 13:24:21 +00:00
);
2023-07-27 16:51:12 +00:00
2023-07-28 13:24:21 +00:00
function handleSliderChange(value: number | number[]) {
if (Array.isArray(value)) throw new Error("Slider uses multiple values instead of one");
2023-07-27 16:51:12 +00:00
2023-07-28 13:24:21 +00:00
setValue(value);
}
2023-07-27 16:51:12 +00:00
2023-07-28 13:24:21 +00:00
const quantityText = `${value} ${getDeclension(value, privilege.value)}`;
2023-07-27 16:51:12 +00:00
2023-07-28 13:24:21 +00:00
const quantityElement = (
<Box
sx={{
display: "flex",
gap: "15px",
alignItems: "center",
justifyContent: upMd ? "end" : undefined,
flexWrap: "wrap",
mt: upMd ? undefined : "12px",
}}
>
<Typography variant="p1" color={theme.palette.purple.main} textAlign="end">
2023-07-28 13:24:21 +00:00
{quantityText}
</Typography>
<Box
sx={{
display: "flex",
gap: "15px",
alignItems: "center",
flexWrap: "wrap",
}}
>
<Typography sx={{ fontSize: "16px", lineHeight: "19px", mt: "1px" }}>или</Typography>
2023-07-28 13:24:21 +00:00
<NumberInputWithUnitAdornment
id={"privilege_input_" + privilege._id}
value={value}
2023-07-28 13:24:21 +00:00
adornmentText={getDeclension(0, privilege.value)}
onChange={(value) => setValue(value)}
/>
</Box>
</Box>
);
2023-07-27 16:51:12 +00:00
2023-07-28 13:24:21 +00:00
const icon =
privilege.type === "day" ? (
<CalendarIcon color={theme.palette.orange.main} bgcolor="#FEDFD0" />
) : (
<PieChartIcon color={theme.palette.orange.main} bgcolor="#FEDFD0" />
);
2023-06-11 10:07:47 +00:00
2023-07-28 13:24:21 +00:00
return (
<Box>
<Typography sx={{ color: theme.palette.gray.dark, mb: "auto" }}>{privilege.description}</Typography>
2023-07-28 13:24:21 +00:00
<Box
sx={{
display: "flex",
flexDirection: "column",
mt: "40px",
}}
>
<Box
sx={{
display: "flex",
// flexWrap: "wrap",
alignItems: "center",
mb: "8px",
justifyContent: "space-between",
gap: "10px",
}}
>
<Box
sx={{
display: "flex",
alignItems: "center",
gap: "22px",
}}
>
{icon}
<Typography sx={{ maxWidth: "100px" }} variant="h5">
{privilege.name}
</Typography>
2023-07-28 13:24:21 +00:00
</Box>
{upMd && quantityElement}
2023-05-27 11:50:21 +00:00
</Box>
2023-07-28 13:24:21 +00:00
<CustomSlider
value={value}
min={0}
max={sliderSettingsByType[privilege.value].max || 100}
onChange={handleSliderChange}
/>
{!upMd && quantityElement}
</Box>
</Box>
);
}