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

150 lines
4.2 KiB
TypeScript
Raw Normal View History

2023-11-05 23:33:40 +00:00
import { Privilege, PrivilegeValueType, useThrottle } from "@frontend/kitui"
import { Box, SliderProps, Typography, useMediaQuery, useTheme } from "@mui/material"
import { CustomSlider } from "@root/components/CustomSlider"
import NumberInputWithUnitAdornment from "@root/components/NumberInputWithUnitAdornment"
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"
import { getDeclension } from "@root/utils/declension"
import { useEffect, useState } from "react"
2023-05-27 11:50:21 +00:00
const sliderSettingsByType: Record<PrivilegeValueType, Partial<SliderProps>> = {
2023-11-05 23:33:40 +00:00
день: { max: 365 },
шаблон: { 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-11-05 23:33:40 +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 isUserNko = useUserStore(state => state.userAccount?.status) === "nko"
const [value, setValue] = useState<number>(userValue)
const throttledValue = useThrottle(value, 200)
2023-05-27 11:50:21 +00:00
2023-11-05 23:33:40 +00:00
useEffect(
function setStoreValue() {
setCustomTariffsUserValue(
privilege.serviceKey,
privilege._id,
throttledValue,
discounts,
currentCartTotal,
purchasesAmount,
isUserNko,
)
},
[currentCartTotal, discounts, purchasesAmount, privilege._id, privilege.serviceKey, throttledValue, isUserNko]
)
2023-07-27 16:51:12 +00:00
function handleSliderChange(measurement: string) {
return (value: number | number[]) => {
if (value < 30 && measurement === "день") {
setValue(0)
return
}
2023-07-27 16:51:12 +00:00
if (Array.isArray(value)) throw new Error("Slider uses multiple values instead of one")
setValue(value)
}
}
2023-07-27 16:51:12 +00:00
2023-11-05 23:33:40 +00:00
const quantityText = `${value} ${getDeclension(value, privilege.value)}`
2023-07-27 16:51:12 +00:00
2023-11-05 23:33:40 +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">
{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}
value={value}
adornmentText={getDeclension(0, privilege.value)}
onChange={(value) => setValue(value)}
/>
</Box>
</Box>
)
2023-07-27 16:51:12 +00:00
2023-11-05 23:33:40 +00:00
const icon =
2023-07-28 13:24:21 +00:00
privilege.type === "day" ? (
2023-11-05 23:33:40 +00:00
<CalendarIcon color={theme.palette.orange.main} bgcolor="#FEDFD0" />
2023-07-28 13:24:21 +00:00
) : (
2023-11-05 23:33:40 +00:00
<PieChartIcon color={theme.palette.orange.main} bgcolor="#FEDFD0" />
)
2023-06-11 10:07:47 +00:00
2023-11-05 23:33:40 +00:00
return (
<Box>
<Typography sx={{ color: theme.palette.gray.dark, 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 sx={{ maxWidth: "100px" }} variant="h5">
{privilege.name}
</Typography>
</Box>
{upMd && quantityElement}
</Box>
<CustomSlider
value={value}
min={0}
2023-12-06 11:55:28 +00:00
max={sliderSettingsByType[privilege.value]?.max || 100}
onChange={handleSliderChange(privilege.value)}
2023-11-05 23:33:40 +00:00
/>
{!upMd && quantityElement}
</Box>
</Box>
)
}