frontPanel/src/pages/Tariffs/tariffsUtils/createTariffElements.tsx

124 lines
3.7 KiB
TypeScript
Raw Normal View History

import { Tariff, calcTariffPrice } from "@frontend/kitui";
import TariffCard from "./TariffCard";
import NumberIcon from "@icons/NumberIcon";
import { currencyFormatter } from "./currencyFormatter";
import FreeTariffCard from "./FreeTariffCard";
2024-08-27 23:41:02 +00:00
import { Box, Icon, Typography } from "@mui/material";
import { startCC } from "@/stores/cc";
2024-08-27 23:41:02 +00:00
import { FC, ReactNode } from "react";
export const createTariffElements = (
filteredTariffs: Tariff[],
addFreeTariff = false,
user: any,
discounts: any,
onclick: any,
2024-08-27 23:41:02 +00:00
sendRequest?: () => void,
cc?: boolean,
icon?: ReactNode
) => {
2025-01-10 11:01:50 +00:00
console.log("start work createTariffElements")
console.log("filteredTariffs ", filteredTariffs)
2025-05-22 20:10:15 +00:00
console.log("user ", user)
console.log("user.isUserNko, ", user.isUserNko)
const tariffElements = filteredTariffs
.filter((tariff) => tariff.privileges.length > 0)
.map((tariff, index) => {
const { priceBeforeDiscounts, priceAfterDiscounts } = calcTariffPrice(
tariff,
discounts,
2024-04-14 17:05:01 +00:00
user.wallet.spent,
2024-04-05 21:05:51 +00:00
[],
2025-05-22 20:10:15 +00:00
user.status === "nko",
2024-04-14 17:38:30 +00:00
user.userId,
);
return (
<TariffCard
key={tariff._id}
discount={
priceBeforeDiscounts - priceAfterDiscounts
? `${(
(priceBeforeDiscounts - priceAfterDiscounts) /
(Math.trunc(priceBeforeDiscounts) / 100)
).toFixed(0)}%`
: ""
}
icon={
2024-08-27 23:41:02 +00:00
icon ?
<Box
sx={{
backgroundColor: "#EEE4FC",
width: "36px",
height: "36px",
borderRadius: "6px",
display: "flex",
alignItems: "center",
justifyContent: "center",
flexShrink: 0,
}}>
{icon}
</Box>
:
<NumberIcon
number={index + 1}
color={"#7e2aea"}
backgroundColor={"#EEE4FC"}
/>
}
buttonProps={{
text: "Купить",
onClick: () => {
if (cc) startCC() //пометка что это запрос на создание квиза
onclick({
id: tariff._id,
price: Math.trunc(priceAfterDiscounts) / 100,
})
}
}}
2024-08-27 23:41:02 +00:00
sendRequest={sendRequest}
headerText={tariff.name}
text={tariff.description}
price={
<>
{priceBeforeDiscounts !== priceAfterDiscounts && (
<Typography
sx={{
textDecorationLine: "line-through",
color: "#FB5607",
fontSize: "15px",
lineHeight: "21px",
}}
>
2024-05-14 14:27:14 +00:00
{currencyFormatter.format(
Math.trunc(priceBeforeDiscounts) / 100,
)}
</Typography>
)}
<Typography
sx={{
fontWeight: 500,
fontSize: "20px",
lineHeight: "24px",
color: "#4D4D4D",
}}
>
2024-05-14 14:27:14 +00:00
{currencyFormatter.format(
Math.trunc(priceAfterDiscounts) / 100,
)}
</Typography>
</>
}
/>
);
});
if (addFreeTariff) {
if (tariffElements.length < 6)
tariffElements.push(<FreeTariffCard key="free_tariff_card" />);
else tariffElements.splice(5, 0, <FreeTariffCard key="free_tariff_card" />);
}
return tariffElements;
};