front-hub/src/pages/Tariffs/TariffsPage.tsx

126 lines
5.1 KiB
TypeScript
Raw Normal View History

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-06-16 20:09:56 +00:00
import { Box, Tabs, Typography, useMediaQuery, useTheme } from "@mui/material";
2023-03-23 12:03:08 +00:00
import SectionWrapper from "@components/SectionWrapper";
2023-06-16 20:09:56 +00:00
import ComplexNavText from "@root/components/ComplexNavText";
import { useTariffs } from "@root/utils/hooks/useTariffs";
2023-06-30 15:35:31 +00:00
import { updateTariffs, useTariffStore } from "@root/stores/tariffs";
2023-06-16 20:09:56 +00:00
import { enqueueSnackbar } from "notistack";
import { CustomTab } from "@root/components/CustomTab";
import TariffCard from "./TariffCard";
import NumberIcon from "@root/components/NumberIcon";
import { currencyFormatter } from "@root/utils/currencyFormatter";
import { calcIndividualTariffPrices } from "@root/utils/calcTariffPrices";
2023-06-17 16:01:02 +00:00
import { getMessageFromFetchError } from "@frontend/kitui";
2023-06-26 16:13:29 +00:00
import FreeTariffCard from "./FreeTariffCard";
2023-06-30 15:35:31 +00:00
import { addTariffToCart } from "@root/stores/user";
2023-07-10 20:22:06 +00:00
import { useDiscountStore } from "@root/stores/discounts";
import { useCustomTariffsStore } from "@root/stores/customTariffs";
2023-06-16 20:09:56 +00:00
2023-03-23 12:03:08 +00:00
2023-06-16 20:09:56 +00:00
export default function TariffPage() {
const theme = useTheme();
const upMd = useMediaQuery(theme.breakpoints.up("md"));
const location = useLocation();
const tariffs = useTariffStore(state => state.tariffs);
const [tabIndex, setTabIndex] = useState<number>(0);
2023-07-10 20:22:06 +00:00
const discounts = useDiscountStore(state => state.discounts);
const customTariffs = useCustomTariffsStore(state => state.customTariffsMap);
2023-03-23 12:03:08 +00:00
2023-06-16 20:09:56 +00:00
const unit: string = String(location.pathname).slice(9);
2023-03-23 12:03:08 +00:00
2023-06-16 20:09:56 +00:00
const StepperText: Record<string, string> = { volume: "Тарифы на объём", time: "Тарифы на время" };
2023-03-23 12:03:08 +00:00
2023-06-16 20:09:56 +00:00
useTariffs({
apiPage: 0,
tariffsPerPage: 100,
2023-06-30 15:35:31 +00:00
onNewTariffs: updateTariffs,
onError: error => {
2023-06-16 20:09:56 +00:00
const errorMessage = getMessageFromFetchError(error);
if (errorMessage) enqueueSnackbar(errorMessage);
2023-06-30 15:35:31 +00:00
}
2023-06-16 20:09:56 +00:00
});
2023-03-23 12:03:08 +00:00
2023-06-30 15:35:31 +00:00
function handleTariffItemClick(tariffId: string) {
addTariffToCart(tariffId).then(() => {
enqueueSnackbar("Тариф добавлен в корзину");
}).catch(error => {
const message = getMessageFromFetchError(error);
if (message) enqueueSnackbar(message);
});
}
2023-06-16 20:09:56 +00:00
const filteredTariffs = tariffs.filter(tariff => {
2023-07-10 20:22:06 +00:00
return tariff.privilegies.map(p => p.type).includes("day") === (unit === "time") && !tariff.isDeleted;
2023-06-16 20:09:56 +00:00
});
2023-03-23 12:03:08 +00:00
2023-06-26 16:13:29 +00:00
const tariffElements = filteredTariffs.map((tariff, index) => {
const { price, priceWithDiscounts } = calcIndividualTariffPrices(tariff, discounts, customTariffs);
2023-06-26 16:13:29 +00:00
return (
<TariffCard
key={tariff._id}
icon={<NumberIcon
2023-07-05 05:47:46 +00:00
number={index + 1}
2023-06-26 16:13:29 +00:00
color={unit === "time" ? "#7E2AEA" : "#FB5607"}
backgroundColor={unit === "time" ? "#EEE4FC" : "#FEDFD0"}
/>}
buttonProps={{
text: "Выбрать",
onClick: () => handleTariffItemClick(tariff._id),
}}
2023-06-26 16:13:29 +00:00
headerText={tariff.name}
text={tariff.privilegies.map(p => `${p.name} - ${p.amount}`)}
price={<>
{price !== undefined && price !== priceWithDiscounts &&
<Typography variant="oldPrice">{currencyFormatter.format(price / 100)}</Typography>
}
{priceWithDiscounts !== undefined &&
<Typography variant="price">{currencyFormatter.format(priceWithDiscounts / 100)}</Typography>
}
</>}
/>
);
});
2023-06-30 15:35:31 +00:00
if (tariffElements.length < 6) tariffElements.push(<FreeTariffCard key="free_tariff_card" />);
else tariffElements.splice(5, 0, <FreeTariffCard key="free_tariff_card" />);
2023-06-26 16:13:29 +00:00
2023-06-16 20:09:56 +00:00
return (
<SectionWrapper
maxWidth="lg"
sx={{
mt: "20px",
mb: upMd ? "90px" : "63px",
display: "flex",
flexDirection: "column",
2023-03-23 12:03:08 +00:00
}}
2023-06-16 20:09:56 +00:00
>
{upMd && <ComplexNavText text1="Все тарифы — " text2={StepperText[unit]} />}
<Typography variant="h4" sx={{ marginBottom: "23px", mt: "20px" }}>
{StepperText[unit]}
</Typography>
<Tabs
TabIndicatorProps={{ sx: { display: "none" } }}
value={tabIndex}
onChange={(event, newValue) => setTabIndex(newValue)}
variant="scrollable"
scrollButtons={false}
>
<CustomTab value={0} label="Шаблонизатор" />
<CustomTab value={1} label="Опросник" />
<CustomTab value={2} label="Сокращатель ссылок" />
</Tabs>
<Box sx={{
mt: "40px",
mb: "30px",
2023-06-23 11:55:29 +00:00
display: "grid",
2023-06-16 20:09:56 +00:00
gap: "40px",
2023-07-04 12:02:23 +00:00
gridTemplateColumns: "repeat(auto-fit, minmax(300px, 360px))",
2023-06-16 20:09:56 +00:00
}}>
2023-06-26 16:13:29 +00:00
{tariffElements}
2023-06-16 20:09:56 +00:00
</Box>
</SectionWrapper>
);
}