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

170 lines
4.5 KiB
TypeScript
Raw Normal View History

2023-06-17 16:01:02 +00:00
import { useThrottle } from "@frontend/kitui";
2023-07-28 13:24:21 +00:00
import {
Box,
SliderProps,
Typography,
useMediaQuery,
useTheme,
} from "@mui/material";
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";
2023-06-11 10:07:47 +00:00
import { Privilege, PrivilegeValueType } from "@root/model/privilege";
import { useCartStore } from "@root/stores/cart";
2023-07-28 13:24:21 +00:00
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 },
шаблон: { max: 1000000 },
МБ: { max: 1000000 },
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;
const discounts = useDiscountStore((state) => state.discounts);
const currentCartTotal = useCartStore(
(state) => state.cart.priceAfterDiscounts
);
const purchasesAmount =
useUserStore((state) => state.userAccount?.wallet.purchasesAmount) ?? 0;
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,
purchasesAmount
);
},
[
currentCartTotal,
discounts,
purchasesAmount,
privilege._id,
privilege.serviceKey,
throttledValue,
]
);
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.brightPurple.main}
textAlign="end"
>
{quantityText}
</Typography>
<Box
sx={{
display: "flex",
gap: "15px",
alignItems: "center",
flexWrap: "wrap",
}}
>
<Typography sx={{ fontSize: "16px", lineHeight: "19px", mt: "1px" }}>
или
</Typography>
<NumberInputWithUnitAdornment
id={"privilege_input_" + privilege._id}
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.grey3.main, mb: "auto" }}>
{privilege.description}
</Typography>
<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 variant="h5">{privilege.name}</Typography>
</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>
);
}