2023-06-30 15:35:31 +00:00
|
|
|
|
import { useState } from "react";
|
2023-03-23 12:03:08 +00:00
|
|
|
|
import { useLocation } from "react-router-dom";
|
2023-09-04 20:01:24 +00:00
|
|
|
|
import { Box, IconButton, Typography, useMediaQuery, useTheme } from "@mui/material";
|
2023-03-23 12:03:08 +00:00
|
|
|
|
import SectionWrapper from "@components/SectionWrapper";
|
2023-08-29 13:38:05 +00:00
|
|
|
|
import { useTariffStore } from "@root/stores/tariffs";
|
2023-06-16 20:09:56 +00:00
|
|
|
|
import { enqueueSnackbar } from "notistack";
|
2023-07-25 22:31:04 +00:00
|
|
|
|
import { Select } from "@root/components/Select";
|
|
|
|
|
import { Tabs } from "@root/components/Tabs";
|
2023-06-16 20:09:56 +00:00
|
|
|
|
import TariffCard from "./TariffCard";
|
|
|
|
|
import NumberIcon from "@root/components/NumberIcon";
|
|
|
|
|
import { currencyFormatter } from "@root/utils/currencyFormatter";
|
2023-07-13 18:59:23 +00:00
|
|
|
|
import { calcIndividualTariffPrices } from "@root/utils/calcTariffPrices";
|
2023-08-11 09:38:05 +00:00
|
|
|
|
import { Tariff, getMessageFromFetchError } from "@frontend/kitui";
|
2023-06-26 16:13:29 +00:00
|
|
|
|
import FreeTariffCard from "./FreeTariffCard";
|
2023-07-14 13:41:31 +00:00
|
|
|
|
import { addTariffToCart, useUserStore } from "@root/stores/user";
|
2023-07-10 20:22:06 +00:00
|
|
|
|
import { useDiscountStore } from "@root/stores/discounts";
|
2023-07-25 22:31:04 +00:00
|
|
|
|
import { Slider } from "./slider";
|
2023-07-14 13:41:31 +00:00
|
|
|
|
import { useCartStore } from "@root/stores/cart";
|
2023-09-04 20:01:24 +00:00
|
|
|
|
import ArrowBackIcon from "@mui/icons-material/ArrowBack";
|
|
|
|
|
import { usePrevLocation } from "@root/utils/hooks/handleCustomBackNavigation";
|
2023-06-16 20:09:56 +00:00
|
|
|
|
|
2023-07-25 22:31:04 +00:00
|
|
|
|
const subPages = ["Шаблонизатор", "Опросник", "Сокращатель ссылок"];
|
2023-03-23 12:03:08 +00:00
|
|
|
|
|
2023-08-11 16:09:32 +00:00
|
|
|
|
const StepperText: Record<string, string> = {
|
2023-08-16 18:06:40 +00:00
|
|
|
|
volume: "Тарифы на объём",
|
|
|
|
|
time: "Тарифы на время",
|
2023-08-11 16:09:32 +00:00
|
|
|
|
};
|
|
|
|
|
|
2023-06-16 20:09:56 +00:00
|
|
|
|
export default function TariffPage() {
|
2023-08-16 18:06:40 +00:00
|
|
|
|
const theme = useTheme();
|
|
|
|
|
const upMd = useMediaQuery(theme.breakpoints.up("md"));
|
|
|
|
|
const isMobile = useMediaQuery(theme.breakpoints.down(600));
|
2023-09-01 08:27:14 +00:00
|
|
|
|
const isTablet = useMediaQuery(theme.breakpoints.down(1000));
|
2023-08-16 18:06:40 +00:00
|
|
|
|
const location = useLocation();
|
|
|
|
|
const tariffs = useTariffStore((state) => state.tariffs);
|
|
|
|
|
const [selectedItem, setSelectedItem] = useState<number>(0);
|
|
|
|
|
const discounts = useDiscountStore((state) => state.discounts);
|
2023-08-27 13:04:26 +00:00
|
|
|
|
const purchasesAmount = useUserStore((state) => state.userAccount?.wallet.purchasesAmount) ?? 0;
|
2023-08-16 18:06:40 +00:00
|
|
|
|
const cartTariffMap = useCartStore((state) => state.cartTariffMap);
|
2023-10-14 10:19:43 +00:00
|
|
|
|
const isUserNko = useUserStore(state => state.userAccount?.status) === "nko";
|
2023-03-23 12:03:08 +00:00
|
|
|
|
|
2023-09-04 20:01:24 +00:00
|
|
|
|
const handleCustomBackNavigation = usePrevLocation(location);
|
|
|
|
|
|
2023-08-16 18:06:40 +00:00
|
|
|
|
const unit: string = String(location.pathname).slice(9);
|
2023-08-27 13:04:26 +00:00
|
|
|
|
const currentTariffs = Object.values(cartTariffMap).filter((tariff): tariff is Tariff => typeof tariff === "object");
|
2023-08-21 15:58:48 +00:00
|
|
|
|
|
2023-08-16 18:06:40 +00:00
|
|
|
|
function handleTariffItemClick(tariffId: string) {
|
|
|
|
|
addTariffToCart(tariffId)
|
|
|
|
|
.then(() => {
|
|
|
|
|
enqueueSnackbar("Тариф добавлен в корзину");
|
|
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
const message = getMessageFromFetchError(error);
|
|
|
|
|
if (message) enqueueSnackbar(message);
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-06-26 16:13:29 +00:00
|
|
|
|
|
2023-08-16 18:06:40 +00:00
|
|
|
|
const filteredTariffs = tariffs.filter((tariff) => {
|
2023-08-19 18:37:11 +00:00
|
|
|
|
return (
|
2023-09-15 23:16:58 +00:00
|
|
|
|
tariff.privileges.map((p) => p.type).includes("day") === (unit === "time") &&
|
2023-08-19 18:37:11 +00:00
|
|
|
|
!tariff.isDeleted &&
|
|
|
|
|
!tariff.isCustom
|
|
|
|
|
);
|
2023-08-16 18:06:40 +00:00
|
|
|
|
});
|
2023-08-11 09:38:05 +00:00
|
|
|
|
|
2023-08-19 18:37:11 +00:00
|
|
|
|
const isCustomTariffs = tariffs.filter((tariff) => {
|
|
|
|
|
return (
|
2023-09-15 23:16:58 +00:00
|
|
|
|
tariff.privileges.map((p) => p.type).includes("day") === (unit === "time") &&
|
2023-08-19 18:37:11 +00:00
|
|
|
|
!tariff.isDeleted &&
|
|
|
|
|
tariff.isCustom
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const createTariffElements = (filteredTariffs: Tariff[]) => {
|
|
|
|
|
const tariffElements = filteredTariffs
|
2023-09-15 23:16:58 +00:00
|
|
|
|
.filter((tariff) => tariff.privileges.length > 0)
|
2023-08-19 18:37:11 +00:00
|
|
|
|
.map((tariff, index) => {
|
2023-08-27 13:04:26 +00:00
|
|
|
|
const { priceBeforeDiscounts, priceAfterDiscounts } = calcIndividualTariffPrices(
|
|
|
|
|
tariff,
|
|
|
|
|
discounts,
|
|
|
|
|
purchasesAmount,
|
2023-10-14 10:19:43 +00:00
|
|
|
|
currentTariffs,
|
|
|
|
|
isUserNko,
|
2023-08-27 13:04:26 +00:00
|
|
|
|
);
|
2023-08-19 18:37:11 +00:00
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<TariffCard
|
|
|
|
|
key={tariff._id}
|
|
|
|
|
icon={
|
|
|
|
|
<NumberIcon
|
|
|
|
|
number={index + 1}
|
2023-08-24 13:10:47 +00:00
|
|
|
|
color={unit === "time" ? theme.palette.purple.main : theme.palette.orange.main}
|
2023-08-19 18:37:11 +00:00
|
|
|
|
backgroundColor={unit === "time" ? "#EEE4FC" : "#FEDFD0"}
|
|
|
|
|
/>
|
|
|
|
|
}
|
|
|
|
|
buttonProps={{
|
|
|
|
|
text: "Выбрать",
|
|
|
|
|
onClick: () => handleTariffItemClick(tariff._id),
|
|
|
|
|
}}
|
|
|
|
|
headerText={tariff.name}
|
2023-09-15 23:16:58 +00:00
|
|
|
|
text={tariff.privileges.map((p) => `${p.name} - ${p.amount}`)}
|
2023-08-19 18:37:11 +00:00
|
|
|
|
price={
|
|
|
|
|
<>
|
|
|
|
|
{priceBeforeDiscounts !== priceAfterDiscounts && (
|
2023-08-27 13:04:26 +00:00
|
|
|
|
<Typography variant="oldPrice">{currencyFormatter.format(priceBeforeDiscounts / 100)}</Typography>
|
2023-08-19 18:37:11 +00:00
|
|
|
|
)}
|
2023-08-27 13:04:26 +00:00
|
|
|
|
<Typography variant="price">{currencyFormatter.format(priceAfterDiscounts / 100)}</Typography>
|
2023-08-19 18:37:11 +00:00
|
|
|
|
</>
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
});
|
2023-08-16 18:06:40 +00:00
|
|
|
|
|
2023-08-27 13:04:26 +00:00
|
|
|
|
if (tariffElements.length < 6) tariffElements.push(<FreeTariffCard key="free_tariff_card" />);
|
2023-08-19 18:37:11 +00:00
|
|
|
|
else tariffElements.splice(5, 0, <FreeTariffCard key="free_tariff_card" />);
|
2023-08-11 09:38:05 +00:00
|
|
|
|
|
2023-08-19 18:37:11 +00:00
|
|
|
|
return tariffElements;
|
|
|
|
|
};
|
2023-06-26 16:13:29 +00:00
|
|
|
|
|
2023-08-16 18:06:40 +00:00
|
|
|
|
return (
|
|
|
|
|
<SectionWrapper
|
|
|
|
|
maxWidth="lg"
|
|
|
|
|
sx={{
|
|
|
|
|
mt: "20px",
|
2023-09-01 08:27:14 +00:00
|
|
|
|
mb: upMd ? "100px" : "63px",
|
2023-09-04 20:01:24 +00:00
|
|
|
|
px: isTablet ? (isMobile ? "18px" : "40px") : "20px",
|
2023-08-16 18:06:40 +00:00
|
|
|
|
display: "flex",
|
|
|
|
|
flexDirection: "column",
|
|
|
|
|
}}
|
|
|
|
|
>
|
2023-09-04 20:01:24 +00:00
|
|
|
|
<Box sx={{ display: "flex", alignItems: "center" }}>
|
|
|
|
|
{isMobile && (
|
|
|
|
|
<IconButton
|
|
|
|
|
onClick={handleCustomBackNavigation}
|
|
|
|
|
sx={{ p: 0, height: "28px", width: "28px", color: "black", marginRight: "10px" }}
|
|
|
|
|
>
|
|
|
|
|
<ArrowBackIcon />
|
|
|
|
|
</IconButton>
|
|
|
|
|
)}
|
|
|
|
|
<Typography
|
|
|
|
|
sx={{
|
|
|
|
|
marginBottom: "23px",
|
|
|
|
|
mt: "20px",
|
|
|
|
|
fontSize: isMobile ? "24px" : "36px",
|
|
|
|
|
fontWeight: "500",
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{StepperText[unit]}
|
|
|
|
|
</Typography>
|
|
|
|
|
</Box>
|
2023-08-16 18:06:40 +00:00
|
|
|
|
{isMobile ? (
|
2023-08-27 13:04:26 +00:00
|
|
|
|
<Select items={subPages} selectedItem={selectedItem} setSelectedItem={setSelectedItem} />
|
2023-08-16 18:06:40 +00:00
|
|
|
|
) : (
|
2023-08-27 13:04:26 +00:00
|
|
|
|
<Tabs items={subPages} selectedItem={selectedItem} setSelectedItem={setSelectedItem} />
|
2023-08-16 18:06:40 +00:00
|
|
|
|
)}
|
|
|
|
|
<Box
|
|
|
|
|
sx={{
|
|
|
|
|
justifyContent: "left",
|
|
|
|
|
mt: "40px",
|
|
|
|
|
mb: "30px",
|
|
|
|
|
display: "grid",
|
|
|
|
|
gap: "40px",
|
2023-09-01 08:27:14 +00:00
|
|
|
|
gridTemplateColumns: `repeat(auto-fit, minmax(300px, ${isTablet ? "436px" : "360px"}))`,
|
2023-08-16 18:06:40 +00:00
|
|
|
|
}}
|
|
|
|
|
>
|
2023-08-19 18:37:11 +00:00
|
|
|
|
{createTariffElements(filteredTariffs)}
|
2023-08-16 18:06:40 +00:00
|
|
|
|
</Box>
|
2023-08-23 13:24:47 +00:00
|
|
|
|
<Typography
|
|
|
|
|
sx={{
|
|
|
|
|
mt: "40px",
|
|
|
|
|
fontSize: isMobile ? "24px" : "36px",
|
|
|
|
|
fontWeight: "500",
|
|
|
|
|
}}
|
|
|
|
|
>
|
2023-09-04 20:01:24 +00:00
|
|
|
|
Ранее вы
|
2023-08-16 18:06:40 +00:00
|
|
|
|
</Typography>
|
2023-08-19 18:37:11 +00:00
|
|
|
|
<Slider items={createTariffElements(isCustomTariffs)} />
|
2023-08-16 18:06:40 +00:00
|
|
|
|
</SectionWrapper>
|
|
|
|
|
);
|
2023-06-01 13:42:38 +00:00
|
|
|
|
}
|