change cart and tariffs calculation logic
use swr for cart tariffs datafetching remove console clutter remove cart store some fixes
This commit is contained in:
parent
c730993fef
commit
0990dc081f
24027
package-lock.json
generated
24027
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -15,7 +15,7 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@emotion/react": "^11.10.5",
|
"@emotion/react": "^11.10.5",
|
||||||
"@emotion/styled": "^11.10.5",
|
"@emotion/styled": "^11.10.5",
|
||||||
"@frontend/kitui": "1.0.62",
|
"@frontend/kitui": "^1.0.66",
|
||||||
"@mui/icons-material": "^5.10.14",
|
"@mui/icons-material": "^5.10.14",
|
||||||
"@mui/material": "^5.10.14",
|
"@mui/material": "^5.10.14",
|
||||||
"@popperjs/core": "^2.11.8",
|
"@popperjs/core": "^2.11.8",
|
||||||
@ -36,6 +36,7 @@
|
|||||||
"react-router-dom": "^6.15.0",
|
"react-router-dom": "^6.15.0",
|
||||||
"react-slick": "^0.29.0",
|
"react-slick": "^0.29.0",
|
||||||
"slick-carousel": "^1.8.1",
|
"slick-carousel": "^1.8.1",
|
||||||
|
"swr": "^2.2.5",
|
||||||
"use-debounce": "^10.0.0",
|
"use-debounce": "^10.0.0",
|
||||||
"web-vitals": "^2.1.0",
|
"web-vitals": "^2.1.0",
|
||||||
"yup": "^1.1.1",
|
"yup": "^1.1.1",
|
||||||
|
@ -50,7 +50,6 @@ export async function getHistory(): Promise<[GetHistoryResponse | null, string?]
|
|||||||
|
|
||||||
|
|
||||||
historyResponse.records = checked || []
|
historyResponse.records = checked || []
|
||||||
console.log("historyResponse ", historyResponse)
|
|
||||||
return [historyResponse]
|
return [historyResponse]
|
||||||
} catch (nativeError) {
|
} catch (nativeError) {
|
||||||
const [error] = parseAxiosError(nativeError)
|
const [error] = parseAxiosError(nativeError)
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
import { Tariff, makeRequest } from "@frontend/kitui"
|
import { Tariff, makeRequest } from "@frontend/kitui";
|
||||||
import { CreateTariffBody } from "@root/model/customTariffs"
|
import { CreateTariffBody } from "@root/model/customTariffs";
|
||||||
import { parseAxiosError } from "@root/utils/parse-error"
|
import { parseAxiosError } from "@root/utils/parse-error";
|
||||||
|
|
||||||
import type { ServiceKeyToPrivilegesMap } from "@root/model/privilege"
|
import type { ServiceKeyToPrivilegesMap } from "@root/model/privilege";
|
||||||
import type { GetTariffsResponse } from "@root/model/tariff"
|
import type { GetTariffsResponse } from "@root/model/tariff";
|
||||||
|
import { removeTariffFromCart } from "@root/stores/user";
|
||||||
|
|
||||||
const apiUrl = process.env.REACT_APP_DOMAIN + "/strator"
|
const apiUrl = process.env.REACT_APP_DOMAIN + "/strator";
|
||||||
|
|
||||||
export async function getTariffs(
|
export async function getTariffs(
|
||||||
apiPage: number,
|
apiPage: number,
|
||||||
@ -18,13 +19,13 @@ export async function getTariffs(
|
|||||||
method: "get",
|
method: "get",
|
||||||
useToken: true,
|
useToken: true,
|
||||||
signal,
|
signal,
|
||||||
})
|
});
|
||||||
|
|
||||||
return [tariffsResponse]
|
return [tariffsResponse];
|
||||||
} catch (nativeError) {
|
} catch (nativeError) {
|
||||||
const [error] = parseAxiosError(nativeError)
|
const [error] = parseAxiosError(nativeError);
|
||||||
|
|
||||||
return [null, `Не удалось получить список тарифов. ${error}`]
|
return [null, `Не удалось получить список тарифов. ${error}`];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,13 +36,13 @@ export async function createTariff(tariff: CreateTariffBody): Promise<[Tariff |
|
|||||||
method: "post",
|
method: "post",
|
||||||
useToken: true,
|
useToken: true,
|
||||||
body: tariff,
|
body: tariff,
|
||||||
})
|
});
|
||||||
|
|
||||||
return [createTariffResponse]
|
return [createTariffResponse];
|
||||||
} catch (nativeError) {
|
} catch (nativeError) {
|
||||||
const [error] = parseAxiosError(nativeError)
|
const [error] = parseAxiosError(nativeError);
|
||||||
|
|
||||||
return [null, `Не удалось создать тариф. ${error}`]
|
return [null, `Не удалось создать тариф. ${error}`];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,13 +52,13 @@ export async function getTariffById(tariffId: string): Promise<[Tariff | null, s
|
|||||||
url: `${apiUrl}/tariff/${tariffId}`,
|
url: `${apiUrl}/tariff/${tariffId}`,
|
||||||
method: "get",
|
method: "get",
|
||||||
useToken: true,
|
useToken: true,
|
||||||
})
|
});
|
||||||
|
|
||||||
return [getTariffByIdResponse]
|
return [getTariffByIdResponse];
|
||||||
} catch (nativeError) {
|
} catch (nativeError) {
|
||||||
const [error, status] = parseAxiosError(nativeError)
|
const [error, status] = parseAxiosError(nativeError);
|
||||||
|
|
||||||
return [null, `Не удалось получить тарифы. ${error}`, status]
|
return [null, `Не удалось получить тарифы. ${error}`, status];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,17 +71,47 @@ export async function getCustomTariffs(
|
|||||||
signal,
|
signal,
|
||||||
method: "get",
|
method: "get",
|
||||||
useToken: true,
|
useToken: true,
|
||||||
})
|
});
|
||||||
|
|
||||||
const tempCustomTariffsResponse = {
|
const tempCustomTariffsResponse = {
|
||||||
...customTariffsResponse,
|
...customTariffsResponse,
|
||||||
squiz: customTariffsResponse.squiz
|
squiz: customTariffsResponse.squiz
|
||||||
};
|
};
|
||||||
|
|
||||||
return [tempCustomTariffsResponse]
|
return [tempCustomTariffsResponse];
|
||||||
} catch (nativeError) {
|
} catch (nativeError) {
|
||||||
const [error] = parseAxiosError(nativeError)
|
const [error] = parseAxiosError(nativeError);
|
||||||
|
|
||||||
return [null, `Не удалось получить мои тарифы. ${error}`]
|
return [null, `Не удалось получить мои тарифы. ${error}`];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getTariffArray(tariffIds: string[] | undefined) {
|
||||||
|
if (!tariffIds) return null;
|
||||||
|
|
||||||
|
const responses = await Promise.allSettled(tariffIds.map(tariffId =>
|
||||||
|
makeRequest<never, Tariff>({
|
||||||
|
url: `${apiUrl}/tariff/${tariffId}`,
|
||||||
|
method: "get",
|
||||||
|
useToken: true,
|
||||||
|
})
|
||||||
|
));
|
||||||
|
|
||||||
|
const tariffs: Tariff[] = [];
|
||||||
|
|
||||||
|
responses.forEach((response, index) => {
|
||||||
|
switch (response.status) {
|
||||||
|
case "fulfilled": {
|
||||||
|
tariffs.push(response.value);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "rejected": {
|
||||||
|
const [, status] = parseAxiosError(response.reason);
|
||||||
|
if (status === 404) removeTariffFromCart(tariffIds[index]);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return tariffs;
|
||||||
|
}
|
||||||
|
@ -6,18 +6,17 @@ import { removeTariffFromCart } from "@root/stores/user"
|
|||||||
import { enqueueSnackbar } from "notistack"
|
import { enqueueSnackbar } from "notistack"
|
||||||
import {
|
import {
|
||||||
CloseButton,
|
CloseButton,
|
||||||
TariffCartData,
|
|
||||||
getMessageFromFetchError,
|
getMessageFromFetchError,
|
||||||
} from "@frontend/kitui"
|
} from "@frontend/kitui"
|
||||||
|
|
||||||
import type { MouseEvent } from "react"
|
import type { MouseEvent } from "react"
|
||||||
|
import { TariffCartData } from "@root/utils/calcCart/utils";
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
tariffCartData: TariffCartData;
|
tariffCartData: TariffCartData;
|
||||||
outsideFactor: number;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function CustomTariffAccordion({ tariffCartData, outsideFactor }: Props) {
|
export default function CustomTariffAccordion({ tariffCartData }: Props) {
|
||||||
const theme = useTheme()
|
const theme = useTheme()
|
||||||
const upMd = useMediaQuery(theme.breakpoints.up("md"))
|
const upMd = useMediaQuery(theme.breakpoints.up("md"))
|
||||||
const upSm = useMediaQuery(theme.breakpoints.up("sm"))
|
const upSm = useMediaQuery(theme.breakpoints.up("sm"))
|
||||||
@ -36,7 +35,6 @@ export default function CustomTariffAccordion({ tariffCartData, outsideFactor }:
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('DRAWER', outsideFactor)
|
|
||||||
return (
|
return (
|
||||||
<Box
|
<Box
|
||||||
sx={{
|
sx={{
|
||||||
@ -103,7 +101,7 @@ console.log('DRAWER', outsideFactor)
|
|||||||
fontWeight: 500,
|
fontWeight: 500,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{currencyFormatter.format(tariffCartData.price * outsideFactor / 100)}
|
{currencyFormatter.format(tariffCartData.price / 100)}
|
||||||
</Typography>
|
</Typography>
|
||||||
</Box>
|
</Box>
|
||||||
<CloseButton
|
<CloseButton
|
||||||
@ -150,7 +148,7 @@ console.log('DRAWER', outsideFactor)
|
|||||||
fontWeight: 500,
|
fontWeight: 500,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{currencyFormatter.format(privilege.price * outsideFactor / 100)}
|
{currencyFormatter.format(privilege.price / 100)}
|
||||||
</Typography>
|
</Typography>
|
||||||
</Box>
|
</Box>
|
||||||
</Box>
|
</Box>
|
||||||
|
@ -5,7 +5,7 @@ import ClearIcon from "@mui/icons-material/Clear";
|
|||||||
import { currencyFormatter } from "@root/utils/currencyFormatter";
|
import { currencyFormatter } from "@root/utils/currencyFormatter";
|
||||||
import { useUserStore, removeTariffFromCart, setCart } from "@root/stores/user";
|
import { useUserStore, removeTariffFromCart, setCart } from "@root/stores/user";
|
||||||
import { enqueueSnackbar } from "notistack";
|
import { enqueueSnackbar } from "notistack";
|
||||||
import { ServiceCartData, getMessageFromFetchError } from "@frontend/kitui";
|
import { getMessageFromFetchError } from "@frontend/kitui";
|
||||||
import ExpandIcon from "@components/icons/ExpandIcon";
|
import ExpandIcon from "@components/icons/ExpandIcon";
|
||||||
import { deleteCart } from "@root/api/cart";
|
import { deleteCart } from "@root/api/cart";
|
||||||
|
|
||||||
@ -13,6 +13,7 @@ import { ReactComponent as CrossIcon } from "@root/assets/Icons/cross.svg";
|
|||||||
|
|
||||||
import type { MouseEvent } from "react";
|
import type { MouseEvent } from "react";
|
||||||
import CustomTariffAccordion from "@root/components/CustomTariffAccordion";
|
import CustomTariffAccordion from "@root/components/CustomTariffAccordion";
|
||||||
|
import { ServiceCartData } from "@root/utils/calcCart/utils";
|
||||||
|
|
||||||
const name: Record<string, string> = {
|
const name: Record<string, string> = {
|
||||||
templategen: "Шаблонизатор",
|
templategen: "Шаблонизатор",
|
||||||
@ -23,10 +24,9 @@ const name: Record<string, string> = {
|
|||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
serviceData: ServiceCartData;
|
serviceData: ServiceCartData;
|
||||||
outsideFactor: number;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function CustomWrapperDrawer({ serviceData, outsideFactor }: Props) {
|
export default function CustomWrapperDrawer({ serviceData }: Props) {
|
||||||
const [isExpanded, setIsExpanded] = useState<boolean>(false);
|
const [isExpanded, setIsExpanded] = useState<boolean>(false);
|
||||||
const [isLoading, setIsLoading] = useState<boolean>(false);
|
const [isLoading, setIsLoading] = useState<boolean>(false);
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
@ -138,7 +138,7 @@ export default function CustomWrapperDrawer({ serviceData, outsideFactor }: Prop
|
|||||||
fontWeight: 500,
|
fontWeight: 500,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{currencyFormatter.format(serviceData.price * outsideFactor / 100)}
|
{currencyFormatter.format(serviceData.price / 100)}
|
||||||
</Typography>
|
</Typography>
|
||||||
</Box>
|
</Box>
|
||||||
</Box>
|
</Box>
|
||||||
@ -169,9 +169,8 @@ export default function CustomWrapperDrawer({ serviceData, outsideFactor }: Prop
|
|||||||
serviceData.tariffs.map((tariff) => {
|
serviceData.tariffs.map((tariff) => {
|
||||||
const privilege = tariff.privileges[0];
|
const privilege = tariff.privileges[0];
|
||||||
|
|
||||||
console.log('CD', tariff, outsideFactor)
|
|
||||||
return tariff.privileges.length > 1 ? (
|
return tariff.privileges.length > 1 ? (
|
||||||
<CustomTariffAccordion outsideFactor={outsideFactor} key={tariff.id} tariffCartData={tariff} />
|
<CustomTariffAccordion key={tariff.id} tariffCartData={tariff} />
|
||||||
) : (
|
) : (
|
||||||
<Box
|
<Box
|
||||||
key={tariff.id + privilege.privilegeId}
|
key={tariff.id + privilege.privilegeId}
|
||||||
@ -211,7 +210,7 @@ console.log('CD', tariff, outsideFactor)
|
|||||||
fontWeight: 500,
|
fontWeight: 500,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{currencyFormatter.format((tariff.price || privilege.price) * outsideFactor / 100)}
|
{currencyFormatter.format((tariff.price || privilege.price) / 100)}
|
||||||
</Typography>
|
</Typography>
|
||||||
<SvgIcon
|
<SvgIcon
|
||||||
sx={{
|
sx={{
|
||||||
|
@ -6,7 +6,6 @@ import { NotificationsModal } from "./NotificationsModal";
|
|||||||
import { Loader } from "./Loader";
|
import { Loader } from "./Loader";
|
||||||
import { useCart } from "@root/utils/hooks/useCart";
|
import { useCart } from "@root/utils/hooks/useCart";
|
||||||
import { currencyFormatter } from "@root/utils/currencyFormatter";
|
import { currencyFormatter } from "@root/utils/currencyFormatter";
|
||||||
import { closeCartDrawer, openCartDrawer, useCartStore } from "@root/stores/cart";
|
|
||||||
import { setUserAccount, useUserStore } from "@root/stores/user";
|
import { setUserAccount, useUserStore } from "@root/stores/user";
|
||||||
import { useTicketStore } from "@root/stores/tickets";
|
import { useTicketStore } from "@root/stores/tickets";
|
||||||
|
|
||||||
@ -29,7 +28,7 @@ function Drawers() {
|
|||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const upMd = useMediaQuery(theme.breakpoints.up("md"));
|
const upMd = useMediaQuery(theme.breakpoints.up("md"));
|
||||||
const isTablet = useMediaQuery(theme.breakpoints.down(1000));
|
const isTablet = useMediaQuery(theme.breakpoints.down(1000));
|
||||||
const isDrawerOpen = useCartStore((state) => state.isDrawerOpen);
|
const [isDrawerOpen, setIsDrawerOpen] = useState<boolean>(false);
|
||||||
const cart = useCart();
|
const cart = useCart();
|
||||||
const userAccount = useUserStore((state) => state.userAccount);
|
const userAccount = useUserStore((state) => state.userAccount);
|
||||||
const tickets = useTicketStore((state) => state.tickets);
|
const tickets = useTicketStore((state) => state.tickets);
|
||||||
@ -52,7 +51,7 @@ function Drawers() {
|
|||||||
|
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
|
|
||||||
closeCartDrawer()
|
setIsDrawerOpen(false);
|
||||||
navigate("payment")
|
navigate("payment")
|
||||||
if (!payCartError.includes("insufficient funds: ")) enqueueSnackbar(payCartError);
|
if (!payCartError.includes("insufficient funds: ")) enqueueSnackbar(payCartError);
|
||||||
return
|
return
|
||||||
@ -63,7 +62,7 @@ function Drawers() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
closeCartDrawer();
|
setIsDrawerOpen(false);;
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleReplenishWallet() {
|
function handleReplenishWallet() {
|
||||||
@ -135,7 +134,7 @@ function Drawers() {
|
|||||||
}))}
|
}))}
|
||||||
/>
|
/>
|
||||||
<IconButton
|
<IconButton
|
||||||
onClick={openCartDrawer}
|
onClick={() => setIsDrawerOpen(true)}
|
||||||
component="div"
|
component="div"
|
||||||
sx={{
|
sx={{
|
||||||
cursor: "pointer",
|
cursor: "pointer",
|
||||||
@ -173,7 +172,7 @@ function Drawers() {
|
|||||||
<CartIcon />
|
<CartIcon />
|
||||||
</Badge>
|
</Badge>
|
||||||
</IconButton>
|
</IconButton>
|
||||||
<Drawer anchor={"right"} open={isDrawerOpen} onClose={closeCartDrawer} sx={{ background: "rgba(0, 0, 0, 0.55)" }}>
|
<Drawer anchor={"right"} open={isDrawerOpen} onClose={() => setIsDrawerOpen(false)} sx={{ background: "rgba(0, 0, 0, 0.55)" }}>
|
||||||
<SectionWrapper
|
<SectionWrapper
|
||||||
maxWidth="lg"
|
maxWidth="lg"
|
||||||
sx={{
|
sx={{
|
||||||
@ -205,15 +204,14 @@ function Drawers() {
|
|||||||
>
|
>
|
||||||
Корзина
|
Корзина
|
||||||
</Typography>
|
</Typography>
|
||||||
<IconButton onClick={closeCartDrawer} sx={{ p: 0 }}>
|
<IconButton onClick={() => setIsDrawerOpen(false)} sx={{ p: 0 }}>
|
||||||
<CrossIcon />
|
<CrossIcon />
|
||||||
</IconButton>
|
</IconButton>
|
||||||
</Box>
|
</Box>
|
||||||
<Box sx={{ pl: "20px", pr: "20px" }}>
|
<Box sx={{ pl: "20px", pr: "20px" }}>
|
||||||
{cart.services.map((serviceData) => {
|
{cart.services.map((serviceData) => {
|
||||||
console.log('aaaaaaaaaaAAAAAAAAAAAAAA', (cart.appliedCartPurchasesDiscount?.Target?.Factor || 1)*(cart.appliedLoyaltyDiscount?.Target?.Factor || 1), (cart.appliedCartPurchasesDiscount?.Target?.Factor || 1),(cart.appliedLoyaltyDiscount?.Target?.Factor || 1))
|
|
||||||
return (
|
return (
|
||||||
<CustomWrapperDrawer key={serviceData.serviceKey} outsideFactor={(cart.appliedCartPurchasesDiscount?.Target?.Factor || 1)*(cart.appliedLoyaltyDiscount?.Target?.Factor || 1)} serviceData={serviceData} />
|
<CustomWrapperDrawer key={serviceData.serviceKey} serviceData={serviceData} />
|
||||||
)})}
|
)})}
|
||||||
<Box
|
<Box
|
||||||
sx={{
|
sx={{
|
||||||
|
@ -19,8 +19,6 @@ interface Props {
|
|||||||
export default function TotalPrice({ priceAfterDiscounts, priceBeforeDiscounts, isConstructor = false }: Props) {
|
export default function TotalPrice({ priceAfterDiscounts, priceBeforeDiscounts, isConstructor = false }: Props) {
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const upMd = useMediaQuery(theme.breakpoints.up("md"));
|
const upMd = useMediaQuery(theme.breakpoints.up("md"));
|
||||||
const isMobile = useMediaQuery(theme.breakpoints.down(550));
|
|
||||||
const cart = useCart();
|
|
||||||
const [notEnoughMoneyAmount, setNotEnoughMoneyAmount] = useState<number>(0);
|
const [notEnoughMoneyAmount, setNotEnoughMoneyAmount] = useState<number>(0);
|
||||||
const [loading, setLoading] = useState<boolean>(false);
|
const [loading, setLoading] = useState<boolean>(false);
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
@ -169,7 +167,7 @@ export default function TotalPrice({ priceAfterDiscounts, priceBeforeDiscounts,
|
|||||||
)}
|
)}
|
||||||
<Button
|
<Button
|
||||||
variant="pena-contained-dark"
|
variant="pena-contained-dark"
|
||||||
disabled = {cart.priceAfterDiscounts === 0}
|
disabled = {priceAfterDiscounts === 0}
|
||||||
onClick={() => (notEnoughMoneyAmount === 0 ? !loading && handlePayClick() : handleReplenishWallet())}
|
onClick={() => (notEnoughMoneyAmount === 0 ? !loading && handlePayClick() : handleReplenishWallet())}
|
||||||
>
|
>
|
||||||
{loading ? <Loader size={24} /> : notEnoughMoneyAmount === 0 ? "Оплатить" : "Пополнить"}
|
{loading ? <Loader size={24} /> : notEnoughMoneyAmount === 0 ? "Оплатить" : "Пополнить"}
|
||||||
|
@ -5,8 +5,3 @@ export interface GetTariffsResponse {
|
|||||||
totalPages: number;
|
totalPages: number;
|
||||||
tariffs: Tariff[];
|
tariffs: Tariff[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface FullTariff extends Tariff {
|
|
||||||
description?: string;
|
|
||||||
order?: number;
|
|
||||||
}
|
|
@ -1,28 +1,26 @@
|
|||||||
import { Box, IconButton, Typography, Badge, useMediaQuery, useTheme } from "@mui/material"
|
import { Box, IconButton, Typography, Badge, useMediaQuery, useTheme } from "@mui/material";
|
||||||
import SectionWrapper from "@components/SectionWrapper"
|
import SectionWrapper from "@components/SectionWrapper";
|
||||||
import ArrowBackIcon from "@mui/icons-material/ArrowBack"
|
import ArrowBackIcon from "@mui/icons-material/ArrowBack";
|
||||||
import TotalPrice from "@components/TotalPrice"
|
import TotalPrice from "@components/TotalPrice";
|
||||||
import CustomWrapper from "./CustomWrapper"
|
import CustomWrapper from "./CustomWrapper";
|
||||||
import { useCart } from "@root/utils/hooks/useCart"
|
import { useCart } from "@root/utils/hooks/useCart";
|
||||||
import { useLocation } from "react-router-dom"
|
import { useLocation } from "react-router-dom";
|
||||||
import { usePrevLocation } from "@root/utils/hooks/handleCustomBackNavigation"
|
import { usePrevLocation } from "@root/utils/hooks/handleCustomBackNavigation";
|
||||||
import { handleComponentError } from "@root/utils/handleComponentError"
|
import { handleComponentError } from "@root/utils/handleComponentError";
|
||||||
import { withErrorBoundary } from "react-error-boundary"
|
import { withErrorBoundary } from "react-error-boundary";
|
||||||
|
|
||||||
function Cart() {
|
function Cart() {
|
||||||
const theme = useTheme()
|
const theme = useTheme();
|
||||||
const upMd = useMediaQuery(theme.breakpoints.up("md"))
|
const upMd = useMediaQuery(theme.breakpoints.up("md"));
|
||||||
const isMobile = useMediaQuery(theme.breakpoints.down(550))
|
const isMobile = useMediaQuery(theme.breakpoints.down(550));
|
||||||
const isTablet = useMediaQuery(theme.breakpoints.down(1000))
|
const isTablet = useMediaQuery(theme.breakpoints.down(1000));
|
||||||
const cart = useCart()
|
const cart = useCart();
|
||||||
const location = useLocation()
|
const location = useLocation();
|
||||||
|
|
||||||
const totalPriceBeforeDiscounts = cart.priceBeforeDiscounts
|
const totalPriceBeforeDiscounts = cart.priceBeforeDiscounts;
|
||||||
const totalPriceAfterDiscounts = cart.priceAfterDiscounts
|
const totalPriceAfterDiscounts = cart.priceAfterDiscounts;
|
||||||
|
|
||||||
console.log('CART', totalPriceAfterDiscounts, totalPriceBeforeDiscounts, cart)
|
|
||||||
const handleCustomBackNavigation = usePrevLocation(location)
|
|
||||||
|
|
||||||
|
const handleCustomBackNavigation = usePrevLocation(location);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<SectionWrapper
|
<SectionWrapper
|
||||||
@ -65,7 +63,6 @@ console.log('CART', totalPriceAfterDiscounts, totalPriceBeforeDiscounts, cart)
|
|||||||
<CustomWrapper
|
<CustomWrapper
|
||||||
key={serviceData.serviceKey}
|
key={serviceData.serviceKey}
|
||||||
serviceData={serviceData}
|
serviceData={serviceData}
|
||||||
outsideFactor={(cart.appliedCartPurchasesDiscount?.Target?.Factor || 1)*(cart.appliedLoyaltyDiscount?.Target?.Factor || 1)}
|
|
||||||
first={index === 0}
|
first={index === 0}
|
||||||
last={index === cart.services.length - 1}
|
last={index === cart.services.length - 1}
|
||||||
/>
|
/>
|
||||||
@ -73,10 +70,10 @@ console.log('CART', totalPriceAfterDiscounts, totalPriceBeforeDiscounts, cart)
|
|||||||
</Box>
|
</Box>
|
||||||
<TotalPrice priceBeforeDiscounts={totalPriceBeforeDiscounts} priceAfterDiscounts={totalPriceAfterDiscounts} />
|
<TotalPrice priceBeforeDiscounts={totalPriceBeforeDiscounts} priceAfterDiscounts={totalPriceAfterDiscounts} />
|
||||||
</SectionWrapper>
|
</SectionWrapper>
|
||||||
)
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default withErrorBoundary(Cart, {
|
export default withErrorBoundary(Cart, {
|
||||||
fallback: <Typography mt="8px" textAlign="center">Ошибка при отображении корзины</Typography>,
|
fallback: <Typography mt="8px" textAlign="center">Ошибка при отображении корзины</Typography>,
|
||||||
onError: handleComponentError,
|
onError: handleComponentError,
|
||||||
})
|
});
|
||||||
|
@ -5,10 +5,11 @@ import ClearIcon from "@mui/icons-material/Clear"
|
|||||||
import { currencyFormatter } from "@root/utils/currencyFormatter"
|
import { currencyFormatter } from "@root/utils/currencyFormatter"
|
||||||
import { removeTariffFromCart } from "@root/stores/user"
|
import { removeTariffFromCart } from "@root/stores/user"
|
||||||
import { enqueueSnackbar } from "notistack"
|
import { enqueueSnackbar } from "notistack"
|
||||||
import { CloseButton, ServiceCartData, getMessageFromFetchError } from "@frontend/kitui"
|
import { CloseButton, getMessageFromFetchError } from "@frontend/kitui"
|
||||||
|
|
||||||
import type { MouseEvent } from "react"
|
import type { MouseEvent } from "react"
|
||||||
import CustomTariffAccordion from "@root/components/CustomTariffAccordion"
|
import CustomTariffAccordion from "@root/components/CustomTariffAccordion"
|
||||||
|
import { ServiceCartData } from "@root/utils/calcCart/utils";
|
||||||
|
|
||||||
const name: Record<string, string> = {
|
const name: Record<string, string> = {
|
||||||
templategen: "Шаблонизатор",
|
templategen: "Шаблонизатор",
|
||||||
@ -19,12 +20,11 @@ const name: Record<string, string> = {
|
|||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
serviceData: ServiceCartData;
|
serviceData: ServiceCartData;
|
||||||
outsideFactor: number;
|
|
||||||
last?: boolean;
|
last?: boolean;
|
||||||
first?: boolean;
|
first?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function CustomWrapper({ serviceData, last, first, outsideFactor }: Props) {
|
export default function CustomWrapper({ serviceData, last, first }: Props) {
|
||||||
const theme = useTheme()
|
const theme = useTheme()
|
||||||
const upMd = useMediaQuery(theme.breakpoints.up("md"))
|
const upMd = useMediaQuery(theme.breakpoints.up("md"))
|
||||||
const upSm = useMediaQuery(theme.breakpoints.up("sm"))
|
const upSm = useMediaQuery(theme.breakpoints.up("sm"))
|
||||||
@ -55,7 +55,6 @@ export default function CustomWrapper({ serviceData, last, first, outsideFactor
|
|||||||
enqueueSnackbar("Тарифы удалены")
|
enqueueSnackbar("Тарифы удалены")
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('SDDD', serviceData)
|
|
||||||
return (
|
return (
|
||||||
<Box
|
<Box
|
||||||
sx={{
|
sx={{
|
||||||
@ -128,7 +127,7 @@ console.log('SDDD', serviceData)
|
|||||||
fontWeight: 500,
|
fontWeight: 500,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{currencyFormatter.format(serviceData.price * outsideFactor / 100)}
|
{currencyFormatter.format(serviceData.price / 100)}
|
||||||
</Typography>
|
</Typography>
|
||||||
</Box>
|
</Box>
|
||||||
<CloseButton style={{ height: "22 px", width: "22px" }} onClick={deleteService} />
|
<CloseButton style={{ height: "22 px", width: "22px" }} onClick={deleteService} />
|
||||||
@ -139,7 +138,7 @@ console.log('SDDD', serviceData)
|
|||||||
|
|
||||||
|
|
||||||
return tariff.privileges.length > 1 ? (
|
return tariff.privileges.length > 1 ? (
|
||||||
<CustomTariffAccordion outsideFactor={outsideFactor} key={tariff.id} tariffCartData={tariff} />
|
<CustomTariffAccordion key={tariff.id} tariffCartData={tariff} />
|
||||||
) : (
|
) : (
|
||||||
<Box
|
<Box
|
||||||
key={tariff.id + privilege.privilegeId}
|
key={tariff.id + privilege.privilegeId}
|
||||||
@ -178,7 +177,7 @@ console.log('SDDD', serviceData)
|
|||||||
fontWeight: 500,
|
fontWeight: 500,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{currencyFormatter.format(tariff.price * outsideFactor / 100)}
|
{currencyFormatter.format(tariff.price / 100)}
|
||||||
</Typography>
|
</Typography>
|
||||||
</Box>
|
</Box>
|
||||||
<Box
|
<Box
|
||||||
|
@ -55,7 +55,6 @@ export default function CustomTariffCard({ serviceKey, privileges }: Props) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('COMP', priceAfterDiscounts, priceBeforeDiscounts)
|
|
||||||
return (
|
return (
|
||||||
<Box
|
<Box
|
||||||
sx={{
|
sx={{
|
||||||
|
@ -1,29 +1,28 @@
|
|||||||
import { Box, IconButton, Typography, useMediaQuery, useTheme } from "@mui/material"
|
import { Box, IconButton, Typography, useMediaQuery, useTheme } from "@mui/material";
|
||||||
import { Link } from "react-router-dom"
|
import { Link } from "react-router-dom";
|
||||||
import SectionWrapper from "@components/SectionWrapper"
|
import SectionWrapper from "@components/SectionWrapper";
|
||||||
import { useCustomTariffsStore } from "@root/stores/customTariffs"
|
import { useCustomTariffsStore } from "@root/stores/customTariffs";
|
||||||
import ComplexHeader from "@root/components/ComplexHeader"
|
import ComplexHeader from "@root/components/ComplexHeader";
|
||||||
import CustomTariffCard from "./CustomTariffCard"
|
import CustomTariffCard from "./CustomTariffCard";
|
||||||
import ArrowBackIcon from "@mui/icons-material/ArrowBack"
|
import ArrowBackIcon from "@mui/icons-material/ArrowBack";
|
||||||
import TotalPrice from "@root/components/TotalPrice"
|
import TotalPrice from "@root/components/TotalPrice";
|
||||||
import { serviceNameByKey } from "@root/utils/serviceKeys"
|
import { serviceNameByKey } from "@root/utils/serviceKeys";
|
||||||
import { useHistoryTracker } from "@root/utils/hooks/useHistoryTracker"
|
import { useHistoryTracker } from "@root/utils/hooks/useHistoryTracker";
|
||||||
import { withErrorBoundary } from "react-error-boundary"
|
import { withErrorBoundary } from "react-error-boundary";
|
||||||
import { handleComponentError } from "@root/utils/handleComponentError"
|
import { handleComponentError } from "@root/utils/handleComponentError";
|
||||||
|
import { useCart } from "@root/utils/hooks/useCart";
|
||||||
|
|
||||||
function TariffConstructor() {
|
function TariffConstructor() {
|
||||||
const theme = useTheme()
|
const theme = useTheme();
|
||||||
const upMd = useMediaQuery(theme.breakpoints.up("md"))
|
const upMd = useMediaQuery(theme.breakpoints.up("md"));
|
||||||
const isTablet = useMediaQuery(theme.breakpoints.down(1000))
|
const isTablet = useMediaQuery(theme.breakpoints.down(1000));
|
||||||
|
const cart = useCart();
|
||||||
|
const customTariffs = useCustomTariffsStore((state) => state.privilegeByService);
|
||||||
|
|
||||||
const customTariffs = useCustomTariffsStore((state) => state.privilegeByService)
|
const basePrice = cart.priceBeforeDiscounts;
|
||||||
const summaryPriceBeforeDiscountsMap = useCustomTariffsStore((state) => state.summaryPriceBeforeDiscountsMap)
|
const discountedPrice = cart.priceAfterDiscounts;
|
||||||
const summaryPriceAfterDiscountsMap = useCustomTariffsStore((state) => state.summaryPriceAfterDiscountsMap)
|
|
||||||
|
|
||||||
const basePrice = Object.values(summaryPriceBeforeDiscountsMap).reduce((a, e) => a + e, 0)
|
const handleCustomBackNavigation = useHistoryTracker();
|
||||||
const discountedPrice = Object.values(summaryPriceAfterDiscountsMap).reduce((a, e) => a + e, 0)
|
|
||||||
|
|
||||||
const handleCustomBackNavigation = useHistoryTracker()
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<SectionWrapper
|
<SectionWrapper
|
||||||
@ -43,9 +42,8 @@ function TariffConstructor() {
|
|||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{Object.entries(customTariffs).filter(([serviceKey]) => serviceKey === "squiz").map(([serviceKey, privileges], index) => {
|
{Object.entries(customTariffs).filter(([serviceKey]) => serviceKey === "squiz").map(([serviceKey, privileges], index) => {
|
||||||
console.log("serviceKey ",serviceKey)
|
return (
|
||||||
console.log(Object.entries(customTariffs))
|
<Box key={serviceKey}>
|
||||||
return <Box key={index}>
|
|
||||||
<Box
|
<Box
|
||||||
sx={{
|
sx={{
|
||||||
mb: "40px",
|
mb: "40px",
|
||||||
@ -70,6 +68,7 @@ function TariffConstructor() {
|
|||||||
</Box>
|
</Box>
|
||||||
<CustomTariffCard serviceKey={serviceKey} privileges={privileges} />
|
<CustomTariffCard serviceKey={serviceKey} privileges={privileges} />
|
||||||
</Box>
|
</Box>
|
||||||
|
);
|
||||||
})}
|
})}
|
||||||
</Box>
|
</Box>
|
||||||
<Link
|
<Link
|
||||||
@ -86,10 +85,10 @@ function TariffConstructor() {
|
|||||||
</Link>
|
</Link>
|
||||||
<TotalPrice priceBeforeDiscounts={basePrice} priceAfterDiscounts={discountedPrice} isConstructor={true} />
|
<TotalPrice priceBeforeDiscounts={basePrice} priceAfterDiscounts={discountedPrice} isConstructor={true} />
|
||||||
</SectionWrapper>
|
</SectionWrapper>
|
||||||
)
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default withErrorBoundary(TariffConstructor, {
|
export default withErrorBoundary(TariffConstructor, {
|
||||||
fallback: <Typography mt="8px" textAlign="center">Ошибка при отображении моих тарифов</Typography>,
|
fallback: <Typography mt="8px" textAlign="center">Ошибка при отображении моих тарифов</Typography>,
|
||||||
onError: handleComponentError,
|
onError: handleComponentError,
|
||||||
})
|
});
|
||||||
|
@ -1,15 +1,14 @@
|
|||||||
import { Privilege, PrivilegeValueType, useThrottle } from "@frontend/kitui"
|
import { Privilege, useThrottle } from "@frontend/kitui";
|
||||||
import { Box, SliderProps, Typography, useMediaQuery, useTheme } from "@mui/material"
|
import { Box, Typography, useMediaQuery, useTheme } from "@mui/material";
|
||||||
import { CustomSlider } from "@root/components/CustomSlider"
|
import { CustomSlider } from "@root/components/CustomSlider";
|
||||||
import NumberInputWithUnitAdornment from "@root/components/NumberInputWithUnitAdornment"
|
import NumberInputWithUnitAdornment from "@root/components/NumberInputWithUnitAdornment";
|
||||||
import CalendarIcon from "@root/components/icons/CalendarIcon"
|
import CalendarIcon from "@root/components/icons/CalendarIcon";
|
||||||
import PieChartIcon from "@root/components/icons/PieChartIcon"
|
import PieChartIcon from "@root/components/icons/PieChartIcon";
|
||||||
import { useCartStore } from "@root/stores/cart"
|
import { setCustomTariffsUserValue, useCustomTariffsStore } from "@root/stores/customTariffs";
|
||||||
import { setCustomTariffsUserValue, useCustomTariffsStore } from "@root/stores/customTariffs"
|
import { useUserStore } from "@root/stores/user";
|
||||||
import { useDiscountStore } from "@root/stores/discounts"
|
import { getDeclension } from "@root/utils/declension";
|
||||||
import { useUserStore } from "@root/stores/user"
|
import { useCartTariffs } from "@root/utils/hooks/useCartTariffs";
|
||||||
import { getDeclension } from "@root/utils/declension"
|
import { useEffect, useState } from "react";
|
||||||
import { useEffect, useState } from "react"
|
|
||||||
import { useDebouncedCallback } from 'use-debounce';
|
import { useDebouncedCallback } from 'use-debounce';
|
||||||
|
|
||||||
const sliderSettingsByType = {
|
const sliderSettingsByType = {
|
||||||
@ -17,69 +16,66 @@ const sliderSettingsByType = {
|
|||||||
шаблон: { max: 5000, min: 0 },
|
шаблон: { max: 5000, min: 0 },
|
||||||
МБ: { max: 5000, min: 0 },
|
МБ: { max: 5000, min: 0 },
|
||||||
заявка: { max: 5000, min: 0 }
|
заявка: { max: 5000, min: 0 }
|
||||||
}
|
};
|
||||||
type PrivilegeName = "день" | "шаблон" | "МБ" | "заявка"
|
|
||||||
|
type PrivilegeName = "день" | "шаблон" | "МБ" | "заявка";
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
privilege: Privilege;
|
privilege: Privilege;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function TariffPrivilegeSlider({ privilege }: Props) {
|
export default function TariffPrivilegeSlider({ privilege }: Props) {
|
||||||
const theme = useTheme()
|
const theme = useTheme();
|
||||||
const upMd = useMediaQuery(theme.breakpoints.up("md"))
|
const upMd = useMediaQuery(theme.breakpoints.up("md"));
|
||||||
const userValue = useCustomTariffsStore((state) => state.userValuesMap[privilege.serviceKey]?.[privilege._id]) ?? sliderSettingsByType[privilege.value]?.min
|
const userValue = useCustomTariffsStore((state) => state.userValuesMap[privilege.serviceKey]?.[privilege._id]) ?? sliderSettingsByType[privilege.value]?.min;
|
||||||
const discounts = useDiscountStore((state) => state.discounts)
|
const purchasesAmount = useUserStore((state) => state.userAccount?.wallet.spent) ?? sliderSettingsByType[privilege.value]?.min;
|
||||||
const currentCartTotal = useCartStore((state) => state.cart.priceAfterDiscounts)
|
const cartTariffs = useCartTariffs();
|
||||||
const purchasesAmount = useUserStore((state) => state.userAccount?.wallet.spent) ?? sliderSettingsByType[privilege.value]?.min
|
const [value, setValue] = useState<number>(userValue);
|
||||||
const isUserNko = useUserStore(state => state.userAccount?.status) === "nko"
|
const throttledValue = useThrottle(value, 200);
|
||||||
const [value, setValue] = useState<number>(userValue)
|
|
||||||
const throttledValue = useThrottle(value, 200)
|
|
||||||
|
|
||||||
useEffect(
|
useEffect(
|
||||||
function setStoreValue() {
|
function setStoreValue() {
|
||||||
setCustomTariffsUserValue(
|
setCustomTariffsUserValue(
|
||||||
|
cartTariffs ?? [],
|
||||||
privilege.serviceKey,
|
privilege.serviceKey,
|
||||||
privilege._id,
|
privilege._id,
|
||||||
throttledValue,
|
throttledValue,
|
||||||
discounts,
|
|
||||||
currentCartTotal,
|
|
||||||
purchasesAmount,
|
purchasesAmount,
|
||||||
isUserNko,
|
);
|
||||||
)
|
|
||||||
},
|
},
|
||||||
[currentCartTotal, discounts, purchasesAmount, privilege._id, privilege.serviceKey, throttledValue, isUserNko]
|
[cartTariffs, purchasesAmount, privilege._id, privilege.serviceKey, throttledValue]
|
||||||
)
|
);
|
||||||
|
|
||||||
function handleSliderChange(measurement: PrivilegeName) {
|
function handleSliderChange(measurement: PrivilegeName) {
|
||||||
return (value: number | number[]) => {
|
return (value: number | number[]) => {
|
||||||
|
|
||||||
if (Number(value) < Number(sliderSettingsByType[measurement]?.min)) {
|
if (Number(value) < Number(sliderSettingsByType[measurement]?.min)) {
|
||||||
setValue(0)
|
setValue(0);
|
||||||
return
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Array.isArray(value)) throw new Error("Slider uses multiple values instead of one")
|
if (Array.isArray(value)) throw new Error("Slider uses multiple values instead of one");
|
||||||
|
|
||||||
setValue(value)
|
setValue(value);
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const quantityText = `${value} ${getDeclension(value, privilege.value)}`
|
const quantityText = `${value} ${getDeclension(value, privilege.value)}`;
|
||||||
|
|
||||||
|
|
||||||
const setNotSmallNumber = useDebouncedCallback(() => {
|
const setNotSmallNumber = useDebouncedCallback(() => {
|
||||||
if (value === sliderSettingsByType[privilege.value]?.min) return
|
if (value === sliderSettingsByType[privilege.value]?.min) return;
|
||||||
if (Number(value) < Number(sliderSettingsByType[privilege.value]?.min)) {
|
if (Number(value) < Number(sliderSettingsByType[privilege.value]?.min)) {
|
||||||
setValue(sliderSettingsByType[privilege.value]?.min)
|
setValue(sliderSettingsByType[privilege.value]?.min);
|
||||||
}
|
}
|
||||||
if (privilege.value === "день" && Number(value) < 30 && Number(value) !== 0) {
|
if (privilege.value === "день" && Number(value) < 30 && Number(value) !== 0) {
|
||||||
setValue(30)
|
setValue(30);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (privilege.value !== "день" && Number(value) < 100 && Number(value) !== 0) {
|
if (privilege.value !== "день" && Number(value) < 100 && Number(value) !== 0) {
|
||||||
setValue(100)
|
setValue(100);
|
||||||
}
|
}
|
||||||
}, 600)
|
}, 600);
|
||||||
|
|
||||||
const quantityElement = (
|
const quantityElement = (
|
||||||
<Box
|
<Box
|
||||||
@ -110,20 +106,20 @@ export default function TariffPrivilegeSlider({ privilege }: Props) {
|
|||||||
privilege={privilege}
|
privilege={privilege}
|
||||||
adornmentText={getDeclension(0, privilege.value)}
|
adornmentText={getDeclension(0, privilege.value)}
|
||||||
onChange={(value) => {
|
onChange={(value) => {
|
||||||
setValue(value)
|
setValue(value);
|
||||||
setNotSmallNumber()
|
setNotSmallNumber();
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</Box>
|
</Box>
|
||||||
</Box>
|
</Box>
|
||||||
)
|
);
|
||||||
|
|
||||||
const icon =
|
const icon =
|
||||||
privilege.type === "day" ? (
|
privilege.type === "day" ? (
|
||||||
<CalendarIcon color={theme.palette.orange.main} bgcolor="#FEDFD0" />
|
<CalendarIcon color={theme.palette.orange.main} bgcolor="#FEDFD0" />
|
||||||
) : (
|
) : (
|
||||||
<PieChartIcon color={theme.palette.orange.main} bgcolor="#FEDFD0" />
|
<PieChartIcon color={theme.palette.orange.main} bgcolor="#FEDFD0" />
|
||||||
)
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box>
|
<Box>
|
||||||
@ -169,5 +165,5 @@ export default function TariffPrivilegeSlider({ privilege }: Props) {
|
|||||||
{!upMd && quantityElement}
|
{!upMd && quantityElement}
|
||||||
</Box>
|
</Box>
|
||||||
</Box>
|
</Box>
|
||||||
)
|
);
|
||||||
}
|
}
|
||||||
|
@ -1,28 +1,23 @@
|
|||||||
import { useEffect, useState } from "react";
|
|
||||||
import { useLocation } from "react-router-dom";
|
|
||||||
import { Box, IconButton, Typography, useMediaQuery, useTheme } from "@mui/material";
|
|
||||||
import SectionWrapper from "@components/SectionWrapper";
|
import SectionWrapper from "@components/SectionWrapper";
|
||||||
import { useTariffStore } from "@root/stores/tariffs";
|
|
||||||
import { enqueueSnackbar } from "notistack";
|
|
||||||
import { Select } from "@root/components/Select";
|
|
||||||
import { Tabs } from "@root/components/Tabs";
|
|
||||||
import TariffCard from "./TariffCard";
|
|
||||||
import NumberIcon from "@root/components/NumberIcon";
|
|
||||||
import { currencyFormatter } from "@root/utils/currencyFormatter";
|
|
||||||
import { calcIndividualTariffPrices } from "@root/utils/calcTariffPrices";
|
|
||||||
import { Tariff, getMessageFromFetchError } from "@frontend/kitui";
|
import { Tariff, getMessageFromFetchError } from "@frontend/kitui";
|
||||||
import FreeTariffCard from "./FreeTariffCard";
|
|
||||||
import { addTariffToCart, useUserStore } from "@root/stores/user";
|
|
||||||
import { useDiscountStore } from "@root/stores/discounts";
|
|
||||||
import { Slider } from "./slider";
|
|
||||||
import { useCartStore } from "@root/stores/cart";
|
|
||||||
import ArrowBackIcon from "@mui/icons-material/ArrowBack";
|
import ArrowBackIcon from "@mui/icons-material/ArrowBack";
|
||||||
import { usePrevLocation } from "@root/utils/hooks/handleCustomBackNavigation";
|
import { Box, IconButton, Typography, useMediaQuery, useTheme } from "@mui/material";
|
||||||
import { withErrorBoundary } from "react-error-boundary";
|
import NumberIcon from "@root/components/NumberIcon";
|
||||||
import { handleComponentError } from "@root/utils/handleComponentError";
|
import { useDiscountStore } from "@root/stores/discounts";
|
||||||
import { useHistoryStore } from "@root/stores/history";
|
import { useHistoryStore } from "@root/stores/history";
|
||||||
import { useRecentlyPurchasedTariffs } from "@utils/hooks/useRecentlyPurchasedTariffs";
|
import { useTariffStore } from "@root/stores/tariffs";
|
||||||
import { FullTariff } from "@root/model/tariff"
|
import { addTariffToCart, useUserStore } from "@root/stores/user";
|
||||||
|
import { calcIndividualTariffPrices } from "@root/utils/calcTariffPrices";
|
||||||
|
import { currencyFormatter } from "@root/utils/currencyFormatter";
|
||||||
|
import { handleComponentError } from "@root/utils/handleComponentError";
|
||||||
|
import { usePrevLocation } from "@root/utils/hooks/handleCustomBackNavigation";
|
||||||
|
import { useCartTariffs } from "@root/utils/hooks/useCartTariffs";
|
||||||
|
import { enqueueSnackbar } from "notistack";
|
||||||
|
import { useState } from "react";
|
||||||
|
import { withErrorBoundary } from "react-error-boundary";
|
||||||
|
import { useLocation } from "react-router-dom";
|
||||||
|
import FreeTariffCard from "./FreeTariffCard";
|
||||||
|
import TariffCard from "./TariffCard";
|
||||||
|
|
||||||
const subPages = ["Опросник", /*"Шаблонизатор",*/ /*"Сокращатель ссылок"*/];
|
const subPages = ["Опросник", /*"Шаблонизатор",*/ /*"Сокращатель ссылок"*/];
|
||||||
|
|
||||||
@ -43,15 +38,13 @@ function TariffPage() {
|
|||||||
const [selectedItem, setSelectedItem] = useState<number>(0);
|
const [selectedItem, setSelectedItem] = useState<number>(0);
|
||||||
const discounts = useDiscountStore((state) => state.discounts);
|
const discounts = useDiscountStore((state) => state.discounts);
|
||||||
const purchasesAmount = useUserStore((state) => state.userAccount?.wallet.spent) ?? 0;
|
const purchasesAmount = useUserStore((state) => state.userAccount?.wallet.spent) ?? 0;
|
||||||
const cartTariffMap = useCartStore((state) => state.cartTariffMap);
|
|
||||||
const isUserNko = useUserStore((state) => state.userAccount?.status) === "nko";
|
const isUserNko = useUserStore((state) => state.userAccount?.status) === "nko";
|
||||||
const historyData = useHistoryStore((state) => state.history);
|
const historyData = useHistoryStore((state) => state.history);
|
||||||
|
const currentTariffs = useCartTariffs();
|
||||||
|
|
||||||
const { recentlyPurchased } = useRecentlyPurchasedTariffs();
|
|
||||||
const handleCustomBackNavigation = usePrevLocation(location);
|
const handleCustomBackNavigation = usePrevLocation(location);
|
||||||
|
|
||||||
const unit: string = String(location.pathname).slice(9);
|
const unit: string = String(location.pathname).slice(9);
|
||||||
const currentTariffs = Object.values(cartTariffMap).filter((tariff): tariff is FullTariff => typeof tariff === "object");
|
|
||||||
|
|
||||||
function handleTariffItemClick(tariffId: string) {
|
function handleTariffItemClick(tariffId: string) {
|
||||||
addTariffToCart(tariffId)
|
addTariffToCart(tariffId)
|
||||||
@ -64,7 +57,6 @@ function TariffPage() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("tariffstariffstariffstariffs", tariffs)
|
|
||||||
const filteredTariffs = tariffs.filter((tariff) => {
|
const filteredTariffs = tariffs.filter((tariff) => {
|
||||||
if (tariff.privileges[0] === undefined) return false
|
if (tariff.privileges[0] === undefined) return false
|
||||||
if (
|
if (
|
||||||
@ -92,7 +84,7 @@ function TariffPage() {
|
|||||||
return historyTariffIds.includes(tariff._id);
|
return historyTariffIds.includes(tariff._id);
|
||||||
});
|
});
|
||||||
|
|
||||||
const createTariffElements = (filteredTariffs: FullTariff[], addFreeTariff = false) => {
|
const createTariffElements = (filteredTariffs: Tariff[], addFreeTariff = false) => {
|
||||||
console.log(filteredTariffs)
|
console.log(filteredTariffs)
|
||||||
const tariffElements = filteredTariffs
|
const tariffElements = filteredTariffs
|
||||||
.filter((tariff) => tariff.privileges.length > 0)
|
.filter((tariff) => tariff.privileges.length > 0)
|
||||||
@ -101,14 +93,9 @@ function TariffPage() {
|
|||||||
tariff,
|
tariff,
|
||||||
discounts,
|
discounts,
|
||||||
purchasesAmount,
|
purchasesAmount,
|
||||||
currentTariffs,
|
currentTariffs ?? [],
|
||||||
isUserNko
|
isUserNko
|
||||||
);
|
);
|
||||||
console.log('TECCCCCCCC',priceBeforeDiscounts, priceAfterDiscounts,tariff,
|
|
||||||
discounts,
|
|
||||||
purchasesAmount,
|
|
||||||
currentTariffs,
|
|
||||||
isUserNko)
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<TariffCard
|
<TariffCard
|
||||||
|
@ -1,91 +0,0 @@
|
|||||||
import { CartData, Discount, Tariff } from "@frontend/kitui"
|
|
||||||
import { calcCart } from "@root/utils/calcCart/calcCart"
|
|
||||||
import { produce } from "immer"
|
|
||||||
import { create } from "zustand"
|
|
||||||
import { devtools } from "zustand/middleware"
|
|
||||||
|
|
||||||
interface CartStore {
|
|
||||||
cartTariffMap: Record<string, Tariff | "loading" | "not found">;
|
|
||||||
cart: CartData;
|
|
||||||
isDrawerOpen: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const useCartStore = create<CartStore>()(
|
|
||||||
devtools(
|
|
||||||
(get, set) => ({
|
|
||||||
cartTariffMap: {},
|
|
||||||
cart: {
|
|
||||||
services: [],
|
|
||||||
priceBeforeDiscounts: 0,
|
|
||||||
priceAfterDiscounts: 0,
|
|
||||||
itemCount: 0,
|
|
||||||
allAppliedDiscounts: [],
|
|
||||||
appliedCartPurchasesDiscount: null,
|
|
||||||
appliedLoyaltyDiscount: null,
|
|
||||||
},
|
|
||||||
isDrawerOpen: false,
|
|
||||||
}),
|
|
||||||
{
|
|
||||||
name: "Cart",
|
|
||||||
enabled: process.env.NODE_ENV === "development",
|
|
||||||
trace: true,
|
|
||||||
actionsBlacklist: "rejected",
|
|
||||||
}
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
export const setCartTariffStatus = (tariffId: string, status: "loading" | "not found") =>
|
|
||||||
useCartStore.setState(
|
|
||||||
produce<CartStore>((state) => {
|
|
||||||
state.cartTariffMap[tariffId] = status
|
|
||||||
}),
|
|
||||||
false,
|
|
||||||
{
|
|
||||||
type: "setCartTariffStatus",
|
|
||||||
tariffId,
|
|
||||||
status,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
export const addCartTariffs = (tariffs: Tariff[], discounts: Discount[], purchasesAmount: number, isUserNko: boolean) =>
|
|
||||||
useCartStore.setState(
|
|
||||||
produce<CartStore>((state) => {
|
|
||||||
tariffs.forEach((tariff) => {
|
|
||||||
// if (tariff.isCustom && tariff.privileges[0].serviceKey !== "custom") {
|
|
||||||
// tariff.privileges[0].serviceKey = "custom"
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
state.cartTariffMap[tariff._id] = tariff
|
|
||||||
})
|
|
||||||
const cartTariffs = Object.values(state.cartTariffMap).filter(
|
|
||||||
(tariff): tariff is Tariff => typeof tariff === "object"
|
|
||||||
)
|
|
||||||
state.cart = calcCart(cartTariffs, discounts, purchasesAmount, isUserNko)
|
|
||||||
}),
|
|
||||||
false,
|
|
||||||
{
|
|
||||||
type: tariffs.length > 0 ? "addCartTariffs" : "rejected",
|
|
||||||
tariffs,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
export const removeMissingCartTariffs = (tariffIds: string[], discounts: Discount[], purchasesAmount: number, isUserNko: boolean) =>
|
|
||||||
useCartStore.setState(
|
|
||||||
produce<CartStore>((state) => {
|
|
||||||
for (const key in state.cartTariffMap) {
|
|
||||||
if (!tariffIds.includes(key)) delete state.cartTariffMap[key]
|
|
||||||
}
|
|
||||||
const cartTariffs = Object.values(state.cartTariffMap).filter(
|
|
||||||
(tariff): tariff is Tariff => typeof tariff === "object"
|
|
||||||
)
|
|
||||||
state.cart = calcCart(cartTariffs, discounts, purchasesAmount, isUserNko)
|
|
||||||
}),
|
|
||||||
false,
|
|
||||||
{
|
|
||||||
type: "rejected",
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
export const openCartDrawer = () => useCartStore.setState({ isDrawerOpen: true })
|
|
||||||
|
|
||||||
export const closeCartDrawer = () => useCartStore.setState({ isDrawerOpen: false })
|
|
@ -1,13 +1,13 @@
|
|||||||
import { createTariff } from "@root/api/tariff"
|
import { PrivilegeWithAmount, Tariff } from "@frontend/kitui";
|
||||||
import { CustomTariffUserValuesMap, ServiceKeyToPriceMap } from "@root/model/customTariffs"
|
import { createTariff } from "@root/api/tariff";
|
||||||
import { ServiceKeyToPrivilegesMap } from "@root/model/privilege"
|
import { CustomTariffUserValuesMap, ServiceKeyToPriceMap } from "@root/model/customTariffs";
|
||||||
import { produce } from "immer"
|
import { ServiceKeyToPrivilegesMap } from "@root/model/privilege";
|
||||||
import { create } from "zustand"
|
import { calcCustomTariffPrice } from "@root/utils/calcCart/calcCustomTariffPrice";
|
||||||
import { devtools, persist } from "zustand/middleware"
|
import { produce } from "immer";
|
||||||
import { Discount, PrivilegeWithAmount/* , findCartDiscount */, findDiscountFactor/* , findLoyaltyDiscount *//* , findPrivilegeDiscount *//* , findServiceDiscount */} from "@frontend/kitui"
|
import { create } from "zustand";
|
||||||
import { findNkoDiscount, calcCart } from "@root/utils/calcCart/calcCart"
|
import { devtools, persist } from "zustand/middleware";
|
||||||
import { useCartStore } from "./cart"
|
import { useDiscountStore } from "./discounts";
|
||||||
import { FullTariff } from "@root/model/tariff"
|
import { useUserStore } from "./user";
|
||||||
|
|
||||||
|
|
||||||
interface CustomTariffsStore {
|
interface CustomTariffsStore {
|
||||||
@ -22,52 +22,7 @@ const initialState: CustomTariffsStore = {
|
|||||||
userValuesMap: {},
|
userValuesMap: {},
|
||||||
summaryPriceBeforeDiscountsMap: {},
|
summaryPriceBeforeDiscountsMap: {},
|
||||||
summaryPriceAfterDiscountsMap: {},
|
summaryPriceAfterDiscountsMap: {},
|
||||||
}
|
};
|
||||||
|
|
||||||
function findLoyaltyDiscount(
|
|
||||||
purchasesAmount: number,
|
|
||||||
discounts: Discount[],
|
|
||||||
): Discount | null {
|
|
||||||
const applicableDiscounts = discounts.filter(discount => {
|
|
||||||
return (
|
|
||||||
discount.Layer === 4 &&
|
|
||||||
discount.Condition.UserType !== "nko" &&
|
|
||||||
purchasesAmount >= Number(discount.Condition.PurchasesAmount)
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!applicableDiscounts.length) return null;
|
|
||||||
|
|
||||||
const maxValueDiscount = applicableDiscounts.reduce((prev, current) => {
|
|
||||||
return Number(current.Condition.PurchasesAmount) > Number(prev.Condition.PurchasesAmount) ? current : prev;
|
|
||||||
});
|
|
||||||
|
|
||||||
return maxValueDiscount;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
function findServiceDiscount(
|
|
||||||
serviceKey: string,
|
|
||||||
currentPrice: number,
|
|
||||||
discounts: Discount[],
|
|
||||||
): Discount | null {
|
|
||||||
const applicableDiscounts = discounts.filter(discount => {
|
|
||||||
return (
|
|
||||||
discount.Layer === 2 &&
|
|
||||||
discount.Condition.Group === serviceKey &&
|
|
||||||
currentPrice >= Number(discount.Condition.PriceFrom)
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!applicableDiscounts.length) return null;
|
|
||||||
|
|
||||||
const maxValueDiscount = applicableDiscounts.reduce((prev, current) => {
|
|
||||||
return Number(current.Condition.PriceFrom) > Number(prev.Condition.PriceFrom) ? current : prev;
|
|
||||||
});
|
|
||||||
|
|
||||||
return maxValueDiscount;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
export const useCustomTariffsStore = create<CustomTariffsStore>()(
|
export const useCustomTariffsStore = create<CustomTariffsStore>()(
|
||||||
persist(
|
persist(
|
||||||
@ -87,165 +42,66 @@ export const useCustomTariffsStore = create<CustomTariffsStore>()(
|
|||||||
migrate: () => initialState,
|
migrate: () => initialState,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
)
|
|
||||||
|
|
||||||
function findPrivilegeDiscount(
|
|
||||||
privilegeId: string,
|
|
||||||
privilegePrice: number,
|
|
||||||
discounts: Discount[],
|
|
||||||
): Discount | null {
|
|
||||||
const applicableDiscounts = discounts.filter(discount => {
|
|
||||||
const conditionMinPrice = parseFloat(discount.Condition.Term);
|
|
||||||
if (!isFinite(conditionMinPrice)) throw new Error(`Couldn't parse Discount.Condition.Term: ${discount.Condition.Term}`);
|
|
||||||
|
|
||||||
return (
|
|
||||||
discount.Layer === 1 &&
|
|
||||||
privilegeId === discount.Condition.Product &&
|
|
||||||
privilegePrice >= conditionMinPrice
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!applicableDiscounts.length) return null;
|
|
||||||
|
|
||||||
const maxValueDiscount = applicableDiscounts.reduce((prev, current) =>
|
|
||||||
parseFloat(current.Condition.Term) > parseFloat(prev.Condition.Term) ? current : prev
|
|
||||||
);
|
);
|
||||||
|
|
||||||
return maxValueDiscount;
|
export const setCustomTariffs = (customTariffs: ServiceKeyToPrivilegesMap) => useCustomTariffsStore.setState({ privilegeByService: customTariffs });
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
export const setCustomTariffs = (customTariffs: ServiceKeyToPrivilegesMap) => useCustomTariffsStore.setState({ privilegeByService: customTariffs })
|
|
||||||
function findCartDiscount(
|
|
||||||
cartPurchasesAmount: number,
|
|
||||||
discounts: Discount[],
|
|
||||||
): Discount | null {
|
|
||||||
const applicableDiscounts = discounts.filter(discount => {
|
|
||||||
return (
|
|
||||||
discount.Layer === 3 &&
|
|
||||||
cartPurchasesAmount >= discount.Condition.CartPurchasesAmount
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!applicableDiscounts.length) return null;
|
|
||||||
|
|
||||||
const maxValueDiscount = applicableDiscounts.reduce((prev, current) => {
|
|
||||||
|
|
||||||
return Number(current.Condition.CartPurchasesAmount) > Number(prev.Condition.CartPurchasesAmount) ? current : prev;
|
|
||||||
});
|
|
||||||
|
|
||||||
return maxValueDiscount;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const setCustomTariffsUserValue = (
|
export const setCustomTariffsUserValue = (
|
||||||
|
cartTariffs: Tariff[],
|
||||||
serviceKey: string,
|
serviceKey: string,
|
||||||
privilegeId: string,
|
privilegeId: string,
|
||||||
value: number,
|
value: number,
|
||||||
discounts: Discount[],
|
|
||||||
currentCartTotal: number,
|
|
||||||
purchasesAmount: number,
|
purchasesAmount: number,
|
||||||
isUserNko: boolean,
|
|
||||||
) => useCustomTariffsStore.setState(
|
) => useCustomTariffsStore.setState(
|
||||||
produce<CustomTariffsStore>(state => {
|
produce<CustomTariffsStore>(state => {
|
||||||
state.userValuesMap[serviceKey] ??= {}
|
state.userValuesMap[serviceKey] ??= {};
|
||||||
state.userValuesMap[serviceKey][privilegeId] = value
|
state.userValuesMap[serviceKey][privilegeId] = value;
|
||||||
|
const isUserNko = useUserStore.getState().userAccount?.status === "nko";
|
||||||
|
const discounts = useDiscountStore.getState().discounts;
|
||||||
|
|
||||||
|
const { priceBeforeDiscounts, priceAfterDiscounts } = calcCustomTariffPrice(
|
||||||
|
state.userValuesMap[serviceKey],
|
||||||
|
state.privilegeByService[serviceKey],
|
||||||
|
cartTariffs,
|
||||||
|
discounts,
|
||||||
|
purchasesAmount,
|
||||||
|
isUserNko
|
||||||
|
);
|
||||||
|
|
||||||
const privilegeWithoutAmount = state.privilegeByService[serviceKey].find(p => p._id === privilegeId)
|
state.summaryPriceBeforeDiscountsMap[serviceKey] = priceBeforeDiscounts;
|
||||||
if (!privilegeWithoutAmount) throw new Error(`Privilege not found: ${privilegeId}`)
|
state.summaryPriceAfterDiscountsMap[serviceKey] = priceAfterDiscounts;
|
||||||
let priceWithoutDiscounts = 0
|
|
||||||
let priceAfterDiscounts = 0
|
|
||||||
const privileges = new Array<PrivilegeWithAmount>()
|
|
||||||
|
|
||||||
Object.keys(state.userValuesMap[serviceKey]).forEach(e => {
|
|
||||||
const pwa = state.privilegeByService[serviceKey].find(p => p._id === e)
|
|
||||||
if (!pwa) return
|
|
||||||
if (state.userValuesMap[serviceKey][e] > 0)
|
|
||||||
privileges.push({
|
|
||||||
...pwa,
|
|
||||||
amount: state.userValuesMap[serviceKey][e]
|
|
||||||
})
|
})
|
||||||
})
|
);
|
||||||
|
|
||||||
let tariff: FullTariff = {
|
|
||||||
_id: "",
|
|
||||||
name: "",
|
|
||||||
price: 0,
|
|
||||||
description: "",
|
|
||||||
isCustom: true,
|
|
||||||
isDeleted: false,
|
|
||||||
privileges: privileges,
|
|
||||||
}
|
|
||||||
const cart = calcCart([...Object.values(useCartStore.getState().cartTariffMap as Record<string, FullTariff>), tariff], discounts, purchasesAmount, isUserNko)
|
|
||||||
|
|
||||||
const nkoDiscount = findNkoDiscount(discounts)
|
|
||||||
|
|
||||||
if (isUserNko && nkoDiscount) {
|
|
||||||
state.privilegeByService[serviceKey].forEach(privilege => {
|
|
||||||
const amount = state.userValuesMap[serviceKey]?.[privilege._id] ?? 0
|
|
||||||
priceWithoutDiscounts += privilege.price * amount
|
|
||||||
})
|
|
||||||
|
|
||||||
priceAfterDiscounts = priceWithoutDiscounts * findDiscountFactor(nkoDiscount)
|
|
||||||
} else {
|
|
||||||
state.privilegeByService[serviceKey].forEach(privilege => {
|
|
||||||
const amount = state.userValuesMap[serviceKey]?.[privilege._id] ?? 0
|
|
||||||
console.log(amount, serviceKey, privilege._id)
|
|
||||||
priceWithoutDiscounts += privilege.price * amount
|
|
||||||
|
|
||||||
const discount = cart.allAppliedDiscounts?.find(e => e.Condition.Product === privilege.privilegeId)
|
|
||||||
console.log(discount, privilege.price*amount, findDiscountFactor(discount))
|
|
||||||
//const discount = findPrivilegeDiscount(privilege.privilegeId, amount, discounts)
|
|
||||||
priceAfterDiscounts += privilege.price * amount * findDiscountFactor(discount)
|
|
||||||
})
|
|
||||||
console.log(priceAfterDiscounts)
|
|
||||||
|
|
||||||
const serviceDiscount = cart.allAppliedDiscounts?.find(e => e.Condition.Group === serviceKey)
|
|
||||||
priceAfterDiscounts *= findDiscountFactor(serviceDiscount)
|
|
||||||
console.log(serviceDiscount, serviceKey, priceAfterDiscounts, discounts)
|
|
||||||
|
|
||||||
const cartDiscount = findCartDiscount(currentCartTotal+priceAfterDiscounts, discounts)
|
|
||||||
priceAfterDiscounts *= findDiscountFactor(cartDiscount)
|
|
||||||
|
|
||||||
const loyaltyDiscount = findLoyaltyDiscount(purchasesAmount, discounts)
|
|
||||||
priceAfterDiscounts *= findDiscountFactor(loyaltyDiscount)
|
|
||||||
}
|
|
||||||
console.log('cTC', cart)
|
|
||||||
|
|
||||||
state.summaryPriceBeforeDiscountsMap[serviceKey] = priceWithoutDiscounts
|
|
||||||
state.summaryPriceAfterDiscountsMap[serviceKey] = priceAfterDiscounts
|
|
||||||
})
|
|
||||||
)
|
|
||||||
|
|
||||||
export const createAndSendTariff = (serviceKey: string) => {
|
export const createAndSendTariff = (serviceKey: string) => {
|
||||||
const state = useCustomTariffsStore.getState()
|
const state = useCustomTariffsStore.getState();
|
||||||
|
|
||||||
const privileges: PrivilegeWithAmount[] = []
|
const privileges: PrivilegeWithAmount[] = [];
|
||||||
|
|
||||||
Object.entries(state.userValuesMap[serviceKey]).forEach(([privilegeId, userValue]) => {
|
Object.entries(state.userValuesMap[serviceKey]).forEach(([privilegeId, userValue]) => {
|
||||||
if (userValue === 0) return
|
if (userValue === 0) return;
|
||||||
|
|
||||||
const privilegeWithoutAmount = state.privilegeByService[serviceKey].find(p => p._id === privilegeId)
|
const privilegeWithoutAmount = state.privilegeByService[serviceKey].find(p => p._id === privilegeId);
|
||||||
if (!privilegeWithoutAmount) throw new Error(`Privilege not found: ${privilegeId}`)
|
if (!privilegeWithoutAmount) throw new Error(`Privilege not found: ${privilegeId}`);
|
||||||
|
|
||||||
const privilege: PrivilegeWithAmount = {
|
const privilege: PrivilegeWithAmount = {
|
||||||
...privilegeWithoutAmount,
|
...privilegeWithoutAmount,
|
||||||
privilegeId: privilegeWithoutAmount.privilegeId,
|
privilegeId: privilegeWithoutAmount.privilegeId,
|
||||||
amount: userValue,
|
amount: userValue,
|
||||||
}
|
};
|
||||||
|
|
||||||
privileges.push(privilege)
|
privileges.push(privilege);
|
||||||
})
|
});
|
||||||
|
|
||||||
const name = [...privileges.map(p => p.name), new Date().toISOString()].join(", ")
|
const name = [...privileges.map(p => p.name), new Date().toISOString()].join(", ");
|
||||||
|
|
||||||
const tariff = {
|
const tariff = {
|
||||||
name,
|
name,
|
||||||
isCustom: true,
|
isCustom: true,
|
||||||
privileges,
|
privileges,
|
||||||
}
|
};
|
||||||
|
|
||||||
return createTariff(tariff)
|
return createTariff(tariff);
|
||||||
}
|
};
|
||||||
|
|
||||||
export const clearCustomTariffs = () => useCustomTariffsStore.setState({ ...initialState })
|
export const clearCustomTariffs = () => useCustomTariffsStore.setState({ ...initialState });
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
import { CartData, Discount } from "@frontend/kitui"
|
import { CartData, Discount, Tariff } from "@frontend/kitui"
|
||||||
import { calcCart } from "./calcCart"
|
import { calcCart } from "./calcCart"
|
||||||
import { FullTariff } from "@root/model/tariff"
|
|
||||||
|
|
||||||
|
|
||||||
describe("Cart calculations", () => {
|
describe("Cart calculations", () => {
|
||||||
@ -825,7 +824,7 @@ describe("Cart calculations", () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
const templategenTariff1: FullTariff = {
|
const templategenTariff1: Tariff = {
|
||||||
_id: "t1",
|
_id: "t1",
|
||||||
name: "templategenTariff1",
|
name: "templategenTariff1",
|
||||||
price: 0,
|
price: 0,
|
||||||
@ -849,7 +848,7 @@ const templategenTariff1: FullTariff = {
|
|||||||
updatedAt: ""
|
updatedAt: ""
|
||||||
}
|
}
|
||||||
|
|
||||||
const templategenTariff2: FullTariff = {
|
const templategenTariff2: Tariff = {
|
||||||
_id: "t5",
|
_id: "t5",
|
||||||
name: "templategenTariff2",
|
name: "templategenTariff2",
|
||||||
price: 0,
|
price: 0,
|
||||||
@ -873,7 +872,7 @@ const templategenTariff2: FullTariff = {
|
|||||||
updatedAt: ""
|
updatedAt: ""
|
||||||
}
|
}
|
||||||
|
|
||||||
const squizTariff: FullTariff = {
|
const squizTariff: Tariff = {
|
||||||
_id: "t2",
|
_id: "t2",
|
||||||
name: "squizTariff",
|
name: "squizTariff",
|
||||||
price: 0,
|
price: 0,
|
||||||
@ -897,7 +896,7 @@ const squizTariff: FullTariff = {
|
|||||||
updatedAt: ""
|
updatedAt: ""
|
||||||
}
|
}
|
||||||
|
|
||||||
const reducerTariff: FullTariff = {
|
const reducerTariff: Tariff = {
|
||||||
_id: "t3",
|
_id: "t3",
|
||||||
name: "reducerTariff",
|
name: "reducerTariff",
|
||||||
description: "test",
|
description: "test",
|
||||||
|
@ -1,140 +1,31 @@
|
|||||||
import { CartData, Discount, PrivilegeCartData, Tariff, TariffCartData, findPrivilegeDiscount, findDiscountFactor, applyLoyaltyDiscount} from "@frontend/kitui";
|
import { Discount, Tariff, findDiscountFactor } from "@frontend/kitui";
|
||||||
|
import { CartData, PrivilegeCartData, TariffCartData, findCartDiscount, findLoyaltyDiscount, findNkoDiscount, findPrivilegeDiscount, findServiceDiscount } from "./utils";
|
||||||
|
|
||||||
function applyPrivilegeDiscounts(
|
export function calcCart(tariffs: Tariff[], discounts: Discount[], purchasesAmount: number, isUserNko?: boolean, userId: string = ""): CartData {
|
||||||
cartData: CartData,
|
|
||||||
discounts: Discount[],
|
|
||||||
) {
|
|
||||||
const privMap = new Map()
|
|
||||||
|
|
||||||
cartData.services.forEach(service => service.tariffs.forEach(tariff => tariff.privileges.forEach(p => {
|
|
||||||
privMap.set(p.privilegeId, p.amount + (privMap.get(p.privilegeId)||0))
|
|
||||||
})))
|
|
||||||
cartData.services.forEach(service => {
|
|
||||||
|
|
||||||
service.tariffs.forEach(tariff => {
|
|
||||||
|
|
||||||
tariff.privileges.forEach(privilege => {
|
|
||||||
const privilegeDiscount = findPrivilegeDiscount(privilege.privilegeId, privMap.get(privilege.privilegeId)||0, discounts);
|
|
||||||
if (!privilegeDiscount) return;
|
|
||||||
|
|
||||||
const discountAmount = privilege.price * (1 - findDiscountFactor(privilegeDiscount));
|
|
||||||
privilege.price -= discountAmount;
|
|
||||||
cartData.allAppliedDiscounts.push(privilegeDiscount);
|
|
||||||
privilege.appliedPrivilegeDiscount = privilegeDiscount;
|
|
||||||
|
|
||||||
tariff.price -= discountAmount;
|
|
||||||
service.price -= discountAmount;
|
|
||||||
cartData.priceAfterDiscounts -= discountAmount;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
function findServiceDiscount(
|
|
||||||
serviceKey: string,
|
|
||||||
currentPrice: number,
|
|
||||||
discounts: Discount[],
|
|
||||||
): Discount | null {
|
|
||||||
const applicableDiscounts = discounts.filter(discount => {
|
|
||||||
return (
|
|
||||||
discount.Layer === 2 &&
|
|
||||||
discount.Condition.Group === serviceKey &&
|
|
||||||
currentPrice >= Number(discount.Condition.PriceFrom)
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!applicableDiscounts.length) return null;
|
|
||||||
|
|
||||||
const maxValueDiscount = applicableDiscounts.reduce((prev, current) => {
|
|
||||||
return Number(current.Condition.PriceFrom) > Number(prev.Condition.PriceFrom) ? current : prev;
|
|
||||||
});
|
|
||||||
|
|
||||||
return maxValueDiscount;
|
|
||||||
}
|
|
||||||
|
|
||||||
function findCartDiscount(
|
|
||||||
cartPurchasesAmount: number,
|
|
||||||
discounts: Discount[],
|
|
||||||
): Discount | null {
|
|
||||||
const applicableDiscounts = discounts.filter(discount => {
|
|
||||||
return (
|
|
||||||
discount.Layer === 3 &&
|
|
||||||
cartPurchasesAmount >= Number(discount.Condition.CartPurchasesAmount)
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!applicableDiscounts.length) return null;
|
|
||||||
|
|
||||||
const maxValueDiscount = applicableDiscounts.reduce((prev, current) => {
|
|
||||||
return Number(current.Condition.CartPurchasesAmount) > Number(prev.Condition.CartPurchasesAmount) ? current : prev;
|
|
||||||
});
|
|
||||||
|
|
||||||
return maxValueDiscount;
|
|
||||||
}
|
|
||||||
|
|
||||||
function applyCartDiscount(
|
|
||||||
cartData: CartData,
|
|
||||||
discounts: Discount[],
|
|
||||||
) {
|
|
||||||
const cartDiscount = findCartDiscount(cartData.priceAfterDiscounts, discounts);
|
|
||||||
if (!cartDiscount) return;
|
|
||||||
|
|
||||||
cartData.priceAfterDiscounts *= findDiscountFactor(cartDiscount);
|
|
||||||
cartData.allAppliedDiscounts.push(cartDiscount);
|
|
||||||
cartData.appliedCartPurchasesDiscount = cartDiscount;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
function applyServiceDiscounts(
|
|
||||||
cartData: CartData,
|
|
||||||
discounts: Discount[],
|
|
||||||
) {
|
|
||||||
const privMap = new Map()
|
|
||||||
cartData.services.forEach(service => {
|
|
||||||
|
|
||||||
service.tariffs.forEach(tariff => tariff.privileges.forEach(p => {
|
|
||||||
privMap.set(p.serviceKey, p.price + (privMap.get(p.serviceKey)||0))
|
|
||||||
}))
|
|
||||||
})
|
|
||||||
|
|
||||||
cartData.services.forEach(service => {
|
|
||||||
service.tariffs.map(tariff => {
|
|
||||||
tariff.privileges.forEach(privilege => {
|
|
||||||
const privilegeDiscount = findServiceDiscount(privilege.serviceKey, privMap.get(privilege.serviceKey), discounts);
|
|
||||||
if (!privilegeDiscount) return;
|
|
||||||
|
|
||||||
const discountAmount = privilege.price * (1 - findDiscountFactor(privilegeDiscount));
|
|
||||||
privilege.price -= discountAmount;
|
|
||||||
cartData.allAppliedDiscounts.push(privilegeDiscount);
|
|
||||||
service.appliedServiceDiscount = privilegeDiscount;
|
|
||||||
|
|
||||||
tariff.price -= discountAmount;
|
|
||||||
service.price -= discountAmount;
|
|
||||||
cartData.priceAfterDiscounts -= discountAmount;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export function calcCart(tariffs: Tariff[], discounts: Discount[], purchasesAmount: number, isUserNko?: boolean): CartData {
|
|
||||||
const cartData: CartData = {
|
const cartData: CartData = {
|
||||||
services: [],
|
services: [],
|
||||||
priceBeforeDiscounts: 0,
|
priceBeforeDiscounts: 0,
|
||||||
priceAfterDiscounts: 0,
|
priceAfterDiscounts: 0,
|
||||||
itemCount: 0,
|
|
||||||
appliedCartPurchasesDiscount: null,
|
appliedCartPurchasesDiscount: null,
|
||||||
appliedLoyaltyDiscount: null,
|
appliedLoyaltyDiscount: null,
|
||||||
allAppliedDiscounts: [],
|
allAppliedDiscounts: [],
|
||||||
}
|
appliedDiscountsByPrivilegeId: new Map(),
|
||||||
|
};
|
||||||
|
const privilegeAmountById = new Map<string, number>();
|
||||||
|
const servicePriceByKey = new Map<string, number>();
|
||||||
|
const allAppliedDiscounts = new Set<Discount>();
|
||||||
|
|
||||||
|
// Формируем корзину
|
||||||
|
|
||||||
tariffs.forEach(tariff => {
|
tariffs.forEach(tariff => {
|
||||||
if (tariff.privileges === undefined) return
|
if (tariff.privileges === undefined) return;
|
||||||
if (
|
if (
|
||||||
(tariff.price || 0) > 0
|
(tariff.price || 0) > 0
|
||||||
&& tariff.privileges.length !== 1
|
&& tariff.privileges.length !== 1
|
||||||
) throw new Error("Price is defined for tariff with several")
|
) throw new Error("Price is defined for tariff with several privileges");
|
||||||
|
|
||||||
let serviceData =cartData.services.find(service => (service.serviceKey === "custom" && tariff.isCustom))
|
let serviceData = cartData.services.find(service => (service.serviceKey === "custom" && tariff.isCustom));
|
||||||
if (!serviceData && !tariff.isCustom) serviceData = cartData.services.find(service => service.serviceKey === tariff.privileges[0]?.serviceKey)
|
if (!serviceData && !tariff.isCustom) serviceData = cartData.services.find(service => service.serviceKey === tariff.privileges[0]?.serviceKey);
|
||||||
|
|
||||||
if (!serviceData) {
|
if (!serviceData) {
|
||||||
serviceData = {
|
serviceData = {
|
||||||
@ -142,8 +33,8 @@ export function calcCart(tariffs: Tariff[], discounts: Discount[], purchasesAmou
|
|||||||
tariffs: [],
|
tariffs: [],
|
||||||
price: 0,
|
price: 0,
|
||||||
appliedServiceDiscount: null,
|
appliedServiceDiscount: null,
|
||||||
}
|
};
|
||||||
cartData.services.push(serviceData)
|
cartData.services.push(serviceData);
|
||||||
}
|
}
|
||||||
|
|
||||||
const tariffCartData: TariffCartData = {
|
const tariffCartData: TariffCartData = {
|
||||||
@ -152,13 +43,12 @@ export function calcCart(tariffs: Tariff[], discounts: Discount[], purchasesAmou
|
|||||||
privileges: [],
|
privileges: [],
|
||||||
id: tariff._id,
|
id: tariff._id,
|
||||||
name: tariff.name,
|
name: tariff.name,
|
||||||
}
|
};
|
||||||
serviceData.tariffs.push(tariffCartData)
|
serviceData.tariffs.push(tariffCartData);
|
||||||
|
|
||||||
tariff.privileges.forEach(privilege => {
|
tariff.privileges.forEach(privilege => {
|
||||||
let privilegePrice = privilege.amount * privilege.price
|
let privilegePrice = privilege.amount * privilege.price;
|
||||||
if (!tariff.price) tariffCartData.price += privilegePrice
|
if (tariff.price) privilegePrice = tariff.price;
|
||||||
else privilegePrice = tariff.price
|
|
||||||
|
|
||||||
const privilegeCartData: PrivilegeCartData = {
|
const privilegeCartData: PrivilegeCartData = {
|
||||||
serviceKey: privilege.serviceKey,
|
serviceKey: privilege.serviceKey,
|
||||||
@ -166,47 +56,158 @@ export function calcCart(tariffs: Tariff[], discounts: Discount[], purchasesAmou
|
|||||||
description: privilege.description,
|
description: privilege.description,
|
||||||
price: privilegePrice,
|
price: privilegePrice,
|
||||||
amount: privilege.amount,
|
amount: privilege.amount,
|
||||||
appliedPrivilegeDiscount: null,
|
};
|
||||||
}
|
|
||||||
|
|
||||||
tariffCartData.privileges.push(privilegeCartData)
|
privilegeAmountById.set(
|
||||||
cartData.priceAfterDiscounts += privilegePrice
|
privilege.privilegeId,
|
||||||
cartData.itemCount++
|
privilege.amount + (privilegeAmountById.get(privilege.privilegeId) || 0)
|
||||||
})
|
);
|
||||||
|
servicePriceByKey.set(
|
||||||
|
privilege.serviceKey,
|
||||||
|
privilegePrice + (servicePriceByKey.get(privilege.serviceKey) || 0)
|
||||||
|
);
|
||||||
|
cartData.appliedDiscountsByPrivilegeId.set(privilege.privilegeId, new Set());
|
||||||
|
|
||||||
cartData.priceBeforeDiscounts += tariffCartData.price
|
tariffCartData.privileges.push(privilegeCartData);
|
||||||
serviceData.price += tariffCartData.price
|
});
|
||||||
})
|
});
|
||||||
|
|
||||||
const nkoDiscount = findNkoDiscount(discounts)
|
cartData.priceBeforeDiscounts = Array.from(servicePriceByKey.values()).reduce((a, b) => a + b, 0);
|
||||||
|
|
||||||
|
const nkoDiscount = findNkoDiscount(discounts);
|
||||||
if (isUserNko && nkoDiscount) {
|
if (isUserNko && nkoDiscount) {
|
||||||
applyNkoDiscount(cartData, nkoDiscount)
|
cartData.priceAfterDiscounts *= nkoDiscount.Target.Factor;
|
||||||
} else {
|
allAppliedDiscounts.add(nkoDiscount);
|
||||||
applyPrivilegeDiscounts(cartData, discounts)
|
|
||||||
applyServiceDiscounts(cartData, discounts)
|
cartData.services.forEach(service => {
|
||||||
applyCartDiscount(cartData, discounts)
|
service.tariffs.forEach(tariff => {
|
||||||
applyLoyaltyDiscount(cartData, discounts, purchasesAmount)
|
tariff.privileges.forEach(privilege => {
|
||||||
|
const privilegeAppliedDiscounts = cartData.appliedDiscountsByPrivilegeId.get(privilege.privilegeId);
|
||||||
|
if (!privilegeAppliedDiscounts) throw new Error(`Privilege id ${privilege.privilegeId} not found in appliedDiscountsByPrivilegeId`);
|
||||||
|
|
||||||
|
privilegeAppliedDiscounts.add(nkoDiscount);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
applyDiscountsToCart(cartData);
|
||||||
|
|
||||||
|
cartData.allAppliedDiscounts = Array.from(allAppliedDiscounts);
|
||||||
|
|
||||||
|
return cartData;
|
||||||
}
|
}
|
||||||
|
|
||||||
cartData.allAppliedDiscounts = Array.from(new Set(cartData.allAppliedDiscounts))
|
// Ищем и собираем скидки в appliedDiscountsByPrivilegeId
|
||||||
|
|
||||||
console.log('CD', cartData)
|
cartData.services.forEach(service => {
|
||||||
return cartData
|
service.tariffs.forEach(tariff => {
|
||||||
|
tariff.privileges.forEach(privilege => {
|
||||||
|
const privilegeDiscount = findPrivilegeDiscount(privilege.privilegeId, privilegeAmountById.get(privilege.privilegeId) || 0, discounts, userId);
|
||||||
|
if (!privilegeDiscount) return;
|
||||||
|
|
||||||
|
allAppliedDiscounts.add(privilegeDiscount);
|
||||||
|
|
||||||
|
const privilegeAppliedDiscounts = cartData.appliedDiscountsByPrivilegeId.get(privilege.privilegeId);
|
||||||
|
if (!privilegeAppliedDiscounts) throw new Error(`Privilege id ${privilege.privilegeId} not found in appliedDiscountsByPrivilegeId`);
|
||||||
|
|
||||||
|
privilegeAppliedDiscounts.add(privilegeDiscount);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
cartData.services.forEach(service => {
|
||||||
|
service.tariffs.map(tariff => {
|
||||||
|
tariff.privileges.forEach(privilege => {
|
||||||
|
const servicePrice = servicePriceByKey.get(privilege.serviceKey);
|
||||||
|
if (!servicePrice) return;
|
||||||
|
|
||||||
|
const serviceDiscount = findServiceDiscount(privilege.serviceKey, servicePrice, discounts, userId);
|
||||||
|
if (!serviceDiscount) return;
|
||||||
|
|
||||||
|
allAppliedDiscounts.add(serviceDiscount);
|
||||||
|
service.appliedServiceDiscount = serviceDiscount;
|
||||||
|
|
||||||
|
const privilegeAppliedDiscounts = cartData.appliedDiscountsByPrivilegeId.get(privilege.privilegeId);
|
||||||
|
if (!privilegeAppliedDiscounts) throw new Error(`Privilege id ${privilege.privilegeId} not found in appliedDiscountsByPrivilegeId`);
|
||||||
|
|
||||||
|
privilegeAppliedDiscounts.add(serviceDiscount);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
const intermediateCartData = structuredClone(cartData);
|
||||||
|
applyDiscountsToCart(intermediateCartData);
|
||||||
|
|
||||||
|
const intermediateCartPriceAfterDiscounts = intermediateCartData.priceAfterDiscounts;
|
||||||
|
|
||||||
|
const cartDiscount = findCartDiscount(intermediateCartPriceAfterDiscounts, discounts);
|
||||||
|
if (cartDiscount) {
|
||||||
|
cartData.services.forEach(service => {
|
||||||
|
service.tariffs.forEach(tariff => {
|
||||||
|
tariff.privileges.forEach(privilege => {
|
||||||
|
const privilegeAppliedDiscounts = cartData.appliedDiscountsByPrivilegeId.get(privilege.privilegeId);
|
||||||
|
if (!privilegeAppliedDiscounts) throw new Error(`Privilege id ${privilege.privilegeId} not found in appliedDiscountsByPrivilegeId`);
|
||||||
|
|
||||||
|
if (!Array.from(privilegeAppliedDiscounts)[0]?.Condition.User) privilegeAppliedDiscounts.add(cartDiscount);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
allAppliedDiscounts.add(cartDiscount);
|
||||||
|
cartData.appliedCartPurchasesDiscount = cartDiscount;
|
||||||
}
|
}
|
||||||
|
|
||||||
function applyNkoDiscount(cartData: CartData, discount: Discount) {
|
const loyalDiscount = findLoyaltyDiscount(purchasesAmount, discounts);
|
||||||
cartData.priceAfterDiscounts *= discount.Target.Factor
|
if (loyalDiscount) {
|
||||||
cartData.allAppliedDiscounts.push(discount)
|
cartData.services.forEach(service => {
|
||||||
|
service.tariffs.forEach(tariff => {
|
||||||
|
tariff.privileges.forEach(privilege => {
|
||||||
|
const privilegeAppliedDiscounts = cartData.appliedDiscountsByPrivilegeId.get(privilege.privilegeId);
|
||||||
|
if (!privilegeAppliedDiscounts) throw new Error(`Privilege id ${privilege.privilegeId} not found in appliedDiscountsByPrivilegeId`);
|
||||||
|
|
||||||
|
if (!Array.from(privilegeAppliedDiscounts)[0]?.Condition.User) privilegeAppliedDiscounts.add(loyalDiscount);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
allAppliedDiscounts.add(loyalDiscount);
|
||||||
|
cartData.appliedLoyaltyDiscount = loyalDiscount;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function findNkoDiscount(discounts: Discount[]): Discount | null {
|
// Применяем скидки
|
||||||
const applicableDiscounts = discounts.filter(discount => discount.Condition.UserType === "nko")
|
|
||||||
|
|
||||||
if (!applicableDiscounts.length) return null
|
applyDiscountsToCart(cartData);
|
||||||
|
|
||||||
const maxValueDiscount = applicableDiscounts.reduce((prev, current) => {
|
cartData.allAppliedDiscounts = Array.from(allAppliedDiscounts);
|
||||||
return current.Condition.CartPurchasesAmount > prev.Condition.CartPurchasesAmount ? current : prev
|
|
||||||
})
|
|
||||||
|
|
||||||
return maxValueDiscount
|
return Object.freeze(cartData);
|
||||||
|
}
|
||||||
|
|
||||||
|
function applyDiscountsToCart(cartData: CartData) {
|
||||||
|
cartData.services.forEach(service => {
|
||||||
|
let servicePrice = 0;
|
||||||
|
|
||||||
|
service.tariffs.forEach(tariff => {
|
||||||
|
let privilegePriceSum = 0;
|
||||||
|
let tariffDiscountFactor = 1;
|
||||||
|
|
||||||
|
tariff.privileges.forEach(privilege => {
|
||||||
|
const discounts = cartData.appliedDiscountsByPrivilegeId.get(privilege.privilegeId) ?? [];
|
||||||
|
|
||||||
|
const discountsFactor = Array.from(discounts).reduce((factor, discount) => factor * findDiscountFactor(discount), 1);
|
||||||
|
privilege.price *= discountsFactor;
|
||||||
|
tariffDiscountFactor *= discountsFactor;
|
||||||
|
|
||||||
|
privilegePriceSum += privilege.price;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (tariff.price) tariff.price *= tariffDiscountFactor;
|
||||||
|
else tariff.price = privilegePriceSum;
|
||||||
|
|
||||||
|
servicePrice += tariff.price;
|
||||||
|
});
|
||||||
|
|
||||||
|
service.price = servicePrice;
|
||||||
|
cartData.priceAfterDiscounts += servicePrice;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
49
src/utils/calcCart/calcCustomTariffPrice.ts
Normal file
49
src/utils/calcCart/calcCustomTariffPrice.ts
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
import { Discount, Privilege, PrivilegeWithAmount, Tariff } from "@frontend/kitui";
|
||||||
|
import { CustomTariffUserValues } from "@root/model/customTariffs";
|
||||||
|
import { calcCart } from "./calcCart";
|
||||||
|
|
||||||
|
|
||||||
|
export function calcCustomTariffPrice(
|
||||||
|
customTariffUserValues: CustomTariffUserValues,
|
||||||
|
servicePrivileges: Privilege[],
|
||||||
|
cartTariffs: Tariff[],
|
||||||
|
discounts: Discount[],
|
||||||
|
purchasesAmount: number,
|
||||||
|
isUserNko: boolean,
|
||||||
|
) {
|
||||||
|
const privileges = new Array<PrivilegeWithAmount>();
|
||||||
|
|
||||||
|
const priceBeforeDiscounts = servicePrivileges.reduce((price, privilege) => {
|
||||||
|
const amount = customTariffUserValues?.[privilege._id] ?? 0;
|
||||||
|
return price + privilege.price * amount;
|
||||||
|
}, 0);
|
||||||
|
|
||||||
|
Object.keys(customTariffUserValues).forEach(privilegeId => {
|
||||||
|
const pwa = servicePrivileges.find(p => p._id === privilegeId);
|
||||||
|
if (!pwa) return;
|
||||||
|
if (customTariffUserValues[privilegeId] > 0) privileges.push({
|
||||||
|
...pwa,
|
||||||
|
amount: customTariffUserValues[privilegeId]
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
const customTariff: Tariff = {
|
||||||
|
_id: crypto.randomUUID(),
|
||||||
|
name: "",
|
||||||
|
price: 0,
|
||||||
|
description: "",
|
||||||
|
isCustom: true,
|
||||||
|
isDeleted: false,
|
||||||
|
privileges: privileges,
|
||||||
|
};
|
||||||
|
|
||||||
|
const cart = calcCart([...cartTariffs, customTariff], discounts, purchasesAmount, isUserNko);
|
||||||
|
|
||||||
|
const customService = cart.services.flatMap(service => service.tariffs).find(tariff => tariff.id === customTariff._id);
|
||||||
|
if (!customService) throw new Error("Custom service not found in cart");
|
||||||
|
|
||||||
|
return {
|
||||||
|
priceBeforeDiscounts,
|
||||||
|
priceAfterDiscounts: customService.price,
|
||||||
|
};
|
||||||
|
}
|
182
src/utils/calcCart/utils.ts
Normal file
182
src/utils/calcCart/utils.ts
Normal file
@ -0,0 +1,182 @@
|
|||||||
|
import { Discount, findDiscountFactor } from "@frontend/kitui";
|
||||||
|
|
||||||
|
|
||||||
|
export type CartData = {
|
||||||
|
services: ServiceCartData[];
|
||||||
|
priceBeforeDiscounts: number;
|
||||||
|
priceAfterDiscounts: number;
|
||||||
|
appliedCartPurchasesDiscount: Discount | null;
|
||||||
|
appliedLoyaltyDiscount: Discount | null;
|
||||||
|
allAppliedDiscounts: Discount[];
|
||||||
|
appliedDiscountsByPrivilegeId: Map<string, Set<Discount>>;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type ServiceCartData = {
|
||||||
|
serviceKey: string;
|
||||||
|
tariffs: TariffCartData[];
|
||||||
|
price: number;
|
||||||
|
appliedServiceDiscount: Discount | null;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type TariffCartData = {
|
||||||
|
name: string;
|
||||||
|
id: string;
|
||||||
|
price: number;
|
||||||
|
isCustom: boolean;
|
||||||
|
privileges: PrivilegeCartData[];
|
||||||
|
};
|
||||||
|
|
||||||
|
export type PrivilegeCartData = {
|
||||||
|
serviceKey: string;
|
||||||
|
privilegeId: string;
|
||||||
|
description: string;
|
||||||
|
price: number;
|
||||||
|
amount: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
export function findPrivilegeDiscount(
|
||||||
|
privilegeId: string,
|
||||||
|
privilegePrice: number,
|
||||||
|
discounts: Discount[],
|
||||||
|
userId: string,
|
||||||
|
): Discount | null {
|
||||||
|
const applicableDiscounts = discounts.filter(discount => {
|
||||||
|
if (discount.Condition.User !== "" && discount.Condition.User === userId) return true;
|
||||||
|
|
||||||
|
const conditionMinPrice = parseFloat(discount.Condition.Term);
|
||||||
|
if (!isFinite(conditionMinPrice)) throw new Error(`Couldn't parse Discount.Condition.Term: ${discount.Condition.Term}`);
|
||||||
|
|
||||||
|
return (
|
||||||
|
discount.Layer === 1 &&
|
||||||
|
privilegeId === discount.Condition.Product &&
|
||||||
|
privilegePrice >= conditionMinPrice
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!applicableDiscounts.length) return null;
|
||||||
|
|
||||||
|
let maxValueDiscount: Discount = applicableDiscounts[0];
|
||||||
|
for (const discount of applicableDiscounts) {
|
||||||
|
if (discount.Condition.User !== "" && discount.Condition.User === userId) {
|
||||||
|
maxValueDiscount = discount;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Number(discount.Condition.Term) > Number(maxValueDiscount.Condition.Term)) {
|
||||||
|
maxValueDiscount = discount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return maxValueDiscount;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function findServiceDiscount(
|
||||||
|
serviceKey: string,
|
||||||
|
currentPrice: number,
|
||||||
|
discounts: Discount[],
|
||||||
|
userId: string,
|
||||||
|
): Discount | null {
|
||||||
|
const applicableDiscounts = discounts.filter(discount => {
|
||||||
|
if (discount.Condition.User !== "" && discount.Condition.User === userId) return true;
|
||||||
|
|
||||||
|
return (
|
||||||
|
discount.Layer === 2 &&
|
||||||
|
discount.Condition.Group === serviceKey &&
|
||||||
|
currentPrice >= Number(discount.Condition.PriceFrom)
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!applicableDiscounts.length) return null;
|
||||||
|
|
||||||
|
let maxValueDiscount: Discount = applicableDiscounts[0];
|
||||||
|
for (const discount of applicableDiscounts) {
|
||||||
|
if (discount.Condition.User !== "" && discount.Condition.User === userId) {
|
||||||
|
maxValueDiscount = discount;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Number(discount.Condition.PriceFrom) > Number(maxValueDiscount.Condition.PriceFrom)) {
|
||||||
|
maxValueDiscount = discount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return maxValueDiscount;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function applyCartDiscount(
|
||||||
|
cartData: CartData,
|
||||||
|
discounts: Discount[],
|
||||||
|
) {
|
||||||
|
const cartDiscount = findCartDiscount(cartData.priceAfterDiscounts, discounts);
|
||||||
|
if (!cartDiscount) return;
|
||||||
|
|
||||||
|
cartData.priceAfterDiscounts *= findDiscountFactor(cartDiscount);
|
||||||
|
cartData.allAppliedDiscounts.push(cartDiscount);
|
||||||
|
cartData.appliedCartPurchasesDiscount = cartDiscount;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function findCartDiscount(
|
||||||
|
cartPurchasesAmount: number,
|
||||||
|
discounts: Discount[],
|
||||||
|
): Discount | null {
|
||||||
|
const applicableDiscounts = discounts.filter(discount => {
|
||||||
|
return (
|
||||||
|
discount.Layer === 3 &&
|
||||||
|
cartPurchasesAmount >= Number(discount.Condition.CartPurchasesAmount)
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!applicableDiscounts.length) return null;
|
||||||
|
|
||||||
|
const maxValueDiscount = applicableDiscounts.reduce((prev, current) => {
|
||||||
|
return Number(current.Condition.CartPurchasesAmount) > Number(prev.Condition.CartPurchasesAmount) ? current : prev;
|
||||||
|
});
|
||||||
|
|
||||||
|
return maxValueDiscount;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function applyLoyaltyDiscount(
|
||||||
|
cartData: CartData,
|
||||||
|
discounts: Discount[],
|
||||||
|
purchasesAmount: number,
|
||||||
|
) {
|
||||||
|
const loyalDiscount = findLoyaltyDiscount(purchasesAmount, discounts);
|
||||||
|
if (!loyalDiscount) return;
|
||||||
|
|
||||||
|
cartData.priceAfterDiscounts *= findDiscountFactor(loyalDiscount);
|
||||||
|
cartData.allAppliedDiscounts.push(loyalDiscount);
|
||||||
|
cartData.appliedLoyaltyDiscount = loyalDiscount;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function findLoyaltyDiscount(
|
||||||
|
purchasesAmount: number,
|
||||||
|
discounts: Discount[],
|
||||||
|
): Discount | null {
|
||||||
|
const applicableDiscounts = discounts.filter(discount => {
|
||||||
|
return (
|
||||||
|
discount.Layer === 4 &&
|
||||||
|
discount.Condition.UserType !== "nko" &&
|
||||||
|
purchasesAmount >= discount.Condition.PurchasesAmount
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!applicableDiscounts.length) return null;
|
||||||
|
|
||||||
|
const maxValueDiscount = applicableDiscounts.reduce((prev, current) => {
|
||||||
|
return current.Condition.PurchasesAmount > prev.Condition.PurchasesAmount ? current : prev;
|
||||||
|
});
|
||||||
|
|
||||||
|
return maxValueDiscount;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function findNkoDiscount(discounts: Discount[]): Discount | null {
|
||||||
|
const applicableDiscounts = discounts.filter(discount => discount.Condition.UserType === "nko");
|
||||||
|
|
||||||
|
if (!applicableDiscounts.length) return null;
|
||||||
|
|
||||||
|
const maxValueDiscount = applicableDiscounts.reduce((prev, current) => {
|
||||||
|
return current.Condition.CartPurchasesAmount > prev.Condition.CartPurchasesAmount ? current : prev;
|
||||||
|
});
|
||||||
|
|
||||||
|
return maxValueDiscount;
|
||||||
|
}
|
@ -1,8 +1,8 @@
|
|||||||
import { Discount, Tariff, findDiscountFactor } from "@frontend/kitui"
|
import { Discount, Tariff, findDiscountFactor } from "@frontend/kitui";
|
||||||
import { calcCart } from "./calcCart/calcCart"
|
import { calcCart } from "./calcCart/calcCart";
|
||||||
|
|
||||||
export function calcIndividualTariffPrices(
|
export function calcIndividualTariffPrices(
|
||||||
tariff: Tariff,
|
targetTariff: Tariff,
|
||||||
discounts: Discount[],
|
discounts: Discount[],
|
||||||
purchasesAmount: number,
|
purchasesAmount: number,
|
||||||
currentTariffs: Tariff[],
|
currentTariffs: Tariff[],
|
||||||
@ -11,31 +11,23 @@ export function calcIndividualTariffPrices(
|
|||||||
priceBeforeDiscounts: number;
|
priceBeforeDiscounts: number;
|
||||||
priceAfterDiscounts: number;
|
priceAfterDiscounts: number;
|
||||||
} {
|
} {
|
||||||
const priceBeforeDiscounts =
|
const priceBeforeDiscounts = targetTariff.price || targetTariff.privileges.reduce(
|
||||||
tariff.price || tariff.privileges.reduce((sum, privilege) => sum + privilege.amount * privilege.price, 0)
|
(sum, privilege) => sum + privilege.amount * privilege.price,
|
||||||
let priceAfterDiscounts = 0
|
0
|
||||||
|
);
|
||||||
|
|
||||||
const cart = calcCart([...currentTariffs, tariff], discounts, purchasesAmount, isUserNko)
|
const cart = calcCart([...currentTariffs, targetTariff], discounts, purchasesAmount, isUserNko);
|
||||||
console.log('CITP', cart)
|
|
||||||
if (cart.allAppliedDiscounts[0]?.Target.Overhelm) return {priceBeforeDiscounts: priceBeforeDiscounts, priceAfterDiscounts: priceBeforeDiscounts * cart.allAppliedDiscounts[0].Target.Factor }
|
|
||||||
cart.services.forEach(s => {
|
|
||||||
if (s.serviceKey === tariff.privileges[0].serviceKey) {
|
|
||||||
let processed = false
|
|
||||||
s.tariffs.forEach(t => {
|
|
||||||
if (t.id === tariff._id && !processed) {
|
|
||||||
processed = true
|
|
||||||
t.privileges.forEach(p => priceAfterDiscounts+=p.price)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
priceAfterDiscounts*=findDiscountFactor(cart.appliedLoyaltyDiscount)
|
|
||||||
priceAfterDiscounts*=findDiscountFactor(cart.appliedCartPurchasesDiscount)
|
|
||||||
|
|
||||||
if (priceAfterDiscounts === 0) priceAfterDiscounts = cart.priceAfterDiscounts
|
const tariffCartData = cart.services.flatMap(service => service.tariffs).find(tariff => tariff.id === targetTariff._id);
|
||||||
// cart.allAppliedDiscounts.forEach((discount) => {
|
if (!tariffCartData) throw new Error(`Target tariff ${targetTariff._id} not found in cart`);
|
||||||
// priceAfterDiscounts *= findDiscountFactor(discount)
|
|
||||||
// })
|
if (cart.allAppliedDiscounts[0]?.Target.Overhelm) return {
|
||||||
//priceAfterDiscounts = cart.priceAfterDiscounts
|
priceBeforeDiscounts: priceBeforeDiscounts,
|
||||||
return { priceBeforeDiscounts, priceAfterDiscounts }
|
priceAfterDiscounts: priceBeforeDiscounts * cart.allAppliedDiscounts[0].Target.Factor
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
priceBeforeDiscounts,
|
||||||
|
priceAfterDiscounts: tariffCartData.price,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ interface ComponentError {
|
|||||||
timestamp: number;
|
timestamp: number;
|
||||||
message: string;
|
message: string;
|
||||||
callStack: string | undefined;
|
callStack: string | undefined;
|
||||||
componentStack: string;
|
componentStack: string | null | undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function handleComponentError(error: Error, info: ErrorInfo) {
|
export function handleComponentError(error: Error, info: ErrorInfo) {
|
||||||
|
@ -1,83 +1,20 @@
|
|||||||
import { Tariff, devlog } from "@frontend/kitui"
|
import { useDiscountStore } from "@root/stores/discounts";
|
||||||
import { getTariffById } from "@root/api/tariff"
|
import { useUserStore } from "@root/stores/user";
|
||||||
import {
|
import { useMemo } from "react";
|
||||||
addCartTariffs,
|
import { calcCart } from "../calcCart/calcCart";
|
||||||
removeMissingCartTariffs,
|
import { CartData } from "../calcCart/utils";
|
||||||
setCartTariffStatus,
|
import { useCartTariffs } from "./useCartTariffs";
|
||||||
useCartStore,
|
|
||||||
} from "@root/stores/cart"
|
|
||||||
import { calcCart } from "@root/utils/calcCart/calcCart"
|
|
||||||
import { useDiscountStore } from "@root/stores/discounts"
|
|
||||||
import { useTariffStore } from "@root/stores/tariffs"
|
|
||||||
import { removeTariffFromCart, useUserStore } from "@root/stores/user"
|
|
||||||
import { useEffect } from "react"
|
|
||||||
|
|
||||||
|
|
||||||
export function useCart() {
|
export function useCart(): CartData {
|
||||||
const tariffs = useTariffStore((state) => state.tariffs)
|
const cartTariffs = useCartTariffs();
|
||||||
const cartTariffMap = useCartStore((state) => state.cartTariffMap)
|
const discounts = useDiscountStore((state) => state.discounts);
|
||||||
const cartTariffIds = useUserStore((state) => state.userAccount?.cart)
|
const purchasesAmount = useUserStore((state) => state.userAccount?.wallet.spent) ?? 0;
|
||||||
const cart = useCartStore((state) => state.cart)
|
const isUserNko = useUserStore(state => state.userAccount?.status) === "nko";
|
||||||
const discounts = useDiscountStore((state) => state.discounts)
|
|
||||||
const purchasesAmount =
|
|
||||||
useUserStore((state) => state.userAccount?.wallet.spent) ?? 0
|
|
||||||
const isUserNko = useUserStore(state => state.userAccount?.status) === "nko"
|
|
||||||
|
|
||||||
useEffect(() => {
|
const cart = useMemo(() => {
|
||||||
function addTariffsToCart() {
|
return calcCart(cartTariffs ?? [], discounts, purchasesAmount, isUserNko);
|
||||||
const knownTariffs: Tariff[] = []
|
}, [cartTariffs, discounts, purchasesAmount, isUserNko]);
|
||||||
|
|
||||||
cartTariffIds?.forEach(async (tariffId) => {
|
return cart;
|
||||||
if (typeof cartTariffMap[tariffId] === "object") return
|
|
||||||
|
|
||||||
const tariff = tariffs.find((tariff) => tariff._id === tariffId)
|
|
||||||
if (tariff) return knownTariffs.push(tariff)
|
|
||||||
|
|
||||||
if (!cartTariffMap[tariffId]) {
|
|
||||||
setCartTariffStatus(tariffId, "loading")
|
|
||||||
|
|
||||||
const [tariffByIdResponse, tariffByIdError, tariffByIdStatus] =
|
|
||||||
await getTariffById(tariffId)
|
|
||||||
|
|
||||||
if (tariffByIdError) {
|
|
||||||
devlog(tariffByIdError)
|
|
||||||
setCartTariffStatus(tariffId, "not found")
|
|
||||||
|
|
||||||
if (tariffByIdStatus === 404) {
|
|
||||||
try {
|
|
||||||
await removeTariffFromCart(tariffId)
|
|
||||||
|
|
||||||
devlog(
|
|
||||||
`Unexistant tariff with id ${tariffId} deleted from cart`
|
|
||||||
)
|
|
||||||
} catch (error) {
|
|
||||||
devlog("Error deleting unexistant tariff from cart", error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tariffByIdResponse) {
|
|
||||||
addCartTariffs([tariffByIdResponse], discounts, purchasesAmount, isUserNko)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
if (knownTariffs.length > 0)
|
|
||||||
addCartTariffs(knownTariffs, discounts, purchasesAmount, isUserNko)
|
|
||||||
}
|
|
||||||
|
|
||||||
addTariffsToCart()
|
|
||||||
}, [cartTariffIds, cartTariffMap, discounts, isUserNko, purchasesAmount, tariffs])
|
|
||||||
|
|
||||||
useEffect(
|
|
||||||
function cleanUpCart() {
|
|
||||||
if (cartTariffIds)
|
|
||||||
removeMissingCartTariffs(cartTariffIds, discounts, purchasesAmount, isUserNko)
|
|
||||||
},
|
|
||||||
[cartTariffIds, discounts, isUserNko, purchasesAmount]
|
|
||||||
)
|
|
||||||
const currentTariffs = Object.values(cartTariffMap).filter((tariff): tariff is Tariff => typeof tariff === "object");
|
|
||||||
return calcCart(currentTariffs, discounts,purchasesAmount,isUserNko)
|
|
||||||
}
|
}
|
||||||
|
17
src/utils/hooks/useCartTariffs.ts
Normal file
17
src/utils/hooks/useCartTariffs.ts
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
import { getTariffArray } from "@root/api/tariff";
|
||||||
|
import { useUserStore } from "@root/stores/user";
|
||||||
|
import useSWR from "swr";
|
||||||
|
|
||||||
|
|
||||||
|
export function useCartTariffs() {
|
||||||
|
const cartTariffIds = useUserStore((state) => state.userAccount?.cart);
|
||||||
|
const { data } = useSWR(
|
||||||
|
["cartTariffs", cartTariffIds],
|
||||||
|
key => getTariffArray(key[1]),
|
||||||
|
{
|
||||||
|
keepPreviousData: true,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return data;
|
||||||
|
}
|
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"extends": "./tsconfig.extend.json",
|
"extends": "./tsconfig.extend.json",
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"target": "es5",
|
"target": "ESNext",
|
||||||
"lib": ["es5", "dom", "dom.iterable", "esnext"],
|
"lib": ["es5", "dom", "dom.iterable", "esnext"],
|
||||||
"types": ["node"],
|
"types": ["node"],
|
||||||
"allowJs": true,
|
"allowJs": true,
|
||||||
|
Loading…
Reference in New Issue
Block a user