Merge branch 'dev' into 'main'
Dev See merge request frontend/marketplace!61
This commit is contained in:
commit
e20247cacb
@ -14,7 +14,7 @@
|
||||
"dependencies": {
|
||||
"@emotion/react": "^11.10.5",
|
||||
"@emotion/styled": "^11.10.5",
|
||||
"@frontend/kitui": "1.0.52",
|
||||
"@frontend/kitui": "1.0.53",
|
||||
"@mui/icons-material": "^5.10.14",
|
||||
"@mui/material": "^5.10.14",
|
||||
"@popperjs/core": "^2.11.8",
|
||||
|
@ -37,6 +37,7 @@ export default function TariffPage() {
|
||||
const discounts = useDiscountStore((state) => state.discounts);
|
||||
const purchasesAmount = useUserStore((state) => state.userAccount?.wallet.purchasesAmount) ?? 0;
|
||||
const cartTariffMap = useCartStore((state) => state.cartTariffMap);
|
||||
const isUserNko = useUserStore(state => state.userAccount?.status) === "nko";
|
||||
|
||||
const handleCustomBackNavigation = usePrevLocation(location);
|
||||
|
||||
@ -82,7 +83,8 @@ export default function TariffPage() {
|
||||
tariff,
|
||||
discounts,
|
||||
purchasesAmount,
|
||||
currentTariffs
|
||||
currentTariffs,
|
||||
isUserNko,
|
||||
);
|
||||
|
||||
return (
|
||||
|
@ -47,7 +47,7 @@ export const setCartTariffStatus = (tariffId: string, status: "loading" | "not f
|
||||
}
|
||||
);
|
||||
|
||||
export const addCartTariffs = (tariffs: Tariff[], discounts: Discount[], purchasesAmount: number) =>
|
||||
export const addCartTariffs = (tariffs: Tariff[], discounts: Discount[], purchasesAmount: number, isUserNko: boolean) =>
|
||||
useCartStore.setState(
|
||||
produce<CartStore>((state) => {
|
||||
tariffs.forEach((tariff) => {
|
||||
@ -60,7 +60,7 @@ export const addCartTariffs = (tariffs: Tariff[], discounts: Discount[], purchas
|
||||
const cartTariffs = Object.values(state.cartTariffMap).filter(
|
||||
(tariff): tariff is Tariff => typeof tariff === "object"
|
||||
);
|
||||
state.cart = calcCart(cartTariffs, discounts, purchasesAmount);
|
||||
state.cart = calcCart(cartTariffs, discounts, purchasesAmount, isUserNko);
|
||||
}),
|
||||
false,
|
||||
{
|
||||
@ -69,7 +69,7 @@ export const addCartTariffs = (tariffs: Tariff[], discounts: Discount[], purchas
|
||||
}
|
||||
);
|
||||
|
||||
export const removeMissingCartTariffs = (tariffIds: string[], discounts: Discount[], purchasesAmount: number) =>
|
||||
export const removeMissingCartTariffs = (tariffIds: string[], discounts: Discount[], purchasesAmount: number, isUserNko: boolean) =>
|
||||
useCartStore.setState(
|
||||
produce<CartStore>((state) => {
|
||||
for (const key in state.cartTariffMap) {
|
||||
@ -78,7 +78,7 @@ export const removeMissingCartTariffs = (tariffIds: string[], discounts: Discoun
|
||||
const cartTariffs = Object.values(state.cartTariffMap).filter(
|
||||
(tariff): tariff is Tariff => typeof tariff === "object"
|
||||
);
|
||||
state.cart = calcCart(cartTariffs, discounts, purchasesAmount);
|
||||
state.cart = calcCart(cartTariffs, discounts, purchasesAmount, isUserNko);
|
||||
}),
|
||||
false,
|
||||
{
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { CartData, Discount, PrivilegeCartData, Tariff, TariffCartData, applyCartDiscount, applyLoyaltyDiscount, applyPrivilegeDiscounts, applyServiceDiscounts } from "@frontend/kitui";
|
||||
|
||||
|
||||
export function calcCart(tariffs: Tariff[], discounts: Discount[], purchasesAmount: number): CartData {
|
||||
export function calcCart(tariffs: Tariff[], discounts: Discount[], purchasesAmount: number, isUserNko?: boolean): CartData {
|
||||
const cartData: CartData = {
|
||||
services: [],
|
||||
priceBeforeDiscounts: 0,
|
||||
@ -55,12 +55,34 @@ export function calcCart(tariffs: Tariff[], discounts: Discount[], purchasesAmou
|
||||
serviceData.price += tariffCartData.price;
|
||||
});
|
||||
|
||||
applyPrivilegeDiscounts(cartData, discounts);
|
||||
applyServiceDiscounts(cartData, discounts);
|
||||
applyCartDiscount(cartData, discounts);
|
||||
applyLoyaltyDiscount(cartData, discounts, purchasesAmount);
|
||||
const nkoDiscount = findNkoDiscount(discounts);
|
||||
if (isUserNko && nkoDiscount) {
|
||||
applyNkoDiscount(cartData, nkoDiscount);
|
||||
} else {
|
||||
applyPrivilegeDiscounts(cartData, discounts);
|
||||
applyServiceDiscounts(cartData, discounts);
|
||||
applyCartDiscount(cartData, discounts);
|
||||
applyLoyaltyDiscount(cartData, discounts, purchasesAmount);
|
||||
}
|
||||
|
||||
cartData.allAppliedDiscounts = Array.from(new Set(cartData.allAppliedDiscounts));
|
||||
|
||||
return cartData;
|
||||
}
|
||||
|
||||
function applyNkoDiscount(cartData: CartData, discount: Discount) {
|
||||
cartData.priceAfterDiscounts *= discount.Target.Factor;
|
||||
cartData.allAppliedDiscounts.push(discount);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
@ -5,7 +5,8 @@ export function calcIndividualTariffPrices(
|
||||
tariff: Tariff,
|
||||
discounts: Discount[],
|
||||
purchasesAmount: number,
|
||||
currentTariffs: Tariff[]
|
||||
currentTariffs: Tariff[],
|
||||
isUserNko?: boolean,
|
||||
): {
|
||||
priceBeforeDiscounts: number;
|
||||
priceAfterDiscounts: number;
|
||||
@ -14,7 +15,7 @@ export function calcIndividualTariffPrices(
|
||||
tariff.price || tariff.privileges.reduce((sum, privilege) => sum + privilege.amount * privilege.price, 0);
|
||||
let priceAfterDiscounts = priceBeforeDiscounts;
|
||||
|
||||
const cart = calcCart([...currentTariffs, tariff], discounts, purchasesAmount);
|
||||
const cart = calcCart([...currentTariffs, tariff], discounts, purchasesAmount, isUserNko);
|
||||
|
||||
cart.allAppliedDiscounts.forEach((discount) => {
|
||||
priceAfterDiscounts *= findDiscountFactor(discount);
|
||||
|
@ -1,82 +1,83 @@
|
||||
import { Tariff, devlog } from "@frontend/kitui";
|
||||
import { getTariffById } from "@root/api/tariff";
|
||||
import {
|
||||
addCartTariffs,
|
||||
removeMissingCartTariffs,
|
||||
setCartTariffStatus,
|
||||
useCartStore,
|
||||
} from "@root/stores/cart";
|
||||
import { useDiscountStore } from "@root/stores/discounts";
|
||||
import { useTariffStore } from "@root/stores/tariffs";
|
||||
import { removeTariffFromCart, useUserStore } from "@root/stores/user";
|
||||
import { useEffect } from "react";
|
||||
import {
|
||||
addCartTariffs,
|
||||
removeMissingCartTariffs,
|
||||
setCartTariffStatus,
|
||||
useCartStore,
|
||||
} from "@root/stores/cart";
|
||||
import { isAxiosError } from "axios";
|
||||
import { useDiscountStore } from "@root/stores/discounts";
|
||||
|
||||
|
||||
export function useCart() {
|
||||
const tariffs = useTariffStore((state) => state.tariffs);
|
||||
const cartTariffMap = useCartStore((state) => state.cartTariffMap);
|
||||
const cartTariffIds = useUserStore((state) => state.userAccount?.cart);
|
||||
const cart = useCartStore((state) => state.cart);
|
||||
const discounts = useDiscountStore((state) => state.discounts);
|
||||
const purchasesAmount =
|
||||
useUserStore((state) => state.userAccount?.wallet.purchasesAmount) ?? 0;
|
||||
const tariffs = useTariffStore((state) => state.tariffs);
|
||||
const cartTariffMap = useCartStore((state) => state.cartTariffMap);
|
||||
const cartTariffIds = useUserStore((state) => state.userAccount?.cart);
|
||||
const cart = useCartStore((state) => state.cart);
|
||||
const discounts = useDiscountStore((state) => state.discounts);
|
||||
const purchasesAmount =
|
||||
useUserStore((state) => state.userAccount?.wallet.purchasesAmount) ?? 0;
|
||||
const isUserNko = useUserStore(state => state.userAccount?.status) === "nko";
|
||||
|
||||
useEffect(() => {
|
||||
function addTariffsToCart() {
|
||||
const knownTariffs: Tariff[] = [];
|
||||
useEffect(() => {
|
||||
function addTariffsToCart() {
|
||||
const knownTariffs: Tariff[] = [];
|
||||
|
||||
console.log(cartTariffIds)
|
||||
cartTariffIds?.forEach(async (tariffId) => {
|
||||
if (typeof cartTariffMap[tariffId] === "object") return;
|
||||
console.log(cartTariffIds)
|
||||
cartTariffIds?.forEach(async (tariffId) => {
|
||||
if (typeof cartTariffMap[tariffId] === "object") return;
|
||||
|
||||
const tariff = tariffs.find((tariff) => tariff._id === tariffId);
|
||||
if (tariff) return knownTariffs.push(tariff);
|
||||
const tariff = tariffs.find((tariff) => tariff._id === tariffId);
|
||||
if (tariff) return knownTariffs.push(tariff);
|
||||
|
||||
if (!cartTariffMap[tariffId]) {
|
||||
setCartTariffStatus(tariffId, "loading");
|
||||
if (!cartTariffMap[tariffId]) {
|
||||
setCartTariffStatus(tariffId, "loading");
|
||||
|
||||
const [tariffByIdResponse, tariffByIdError, tariffByIdStatus] =
|
||||
await getTariffById(tariffId);
|
||||
const [tariffByIdResponse, tariffByIdError, tariffByIdStatus] =
|
||||
await getTariffById(tariffId);
|
||||
|
||||
if (tariffByIdError) {
|
||||
devlog(tariffByIdError);
|
||||
setCartTariffStatus(tariffId, "not found");
|
||||
if (tariffByIdError) {
|
||||
devlog(tariffByIdError);
|
||||
setCartTariffStatus(tariffId, "not found");
|
||||
|
||||
if (tariffByIdStatus === 404) {
|
||||
try {
|
||||
await removeTariffFromCart(tariffId);
|
||||
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);
|
||||
}
|
||||
}
|
||||
devlog(
|
||||
`Unexistant tariff with id ${tariffId} deleted from cart`
|
||||
);
|
||||
} catch (error) {
|
||||
devlog("Error deleting unexistant tariff from cart", error);
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (tariffByIdResponse) {
|
||||
addCartTariffs([tariffByIdResponse], discounts, purchasesAmount);
|
||||
}
|
||||
if (tariffByIdResponse) {
|
||||
addCartTariffs([tariffByIdResponse], discounts, purchasesAmount, isUserNko);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (knownTariffs.length > 0)
|
||||
addCartTariffs(knownTariffs, discounts, purchasesAmount, isUserNko);
|
||||
}
|
||||
});
|
||||
|
||||
if (knownTariffs.length > 0)
|
||||
addCartTariffs(knownTariffs, discounts, purchasesAmount);
|
||||
}
|
||||
addTariffsToCart();
|
||||
}, [cartTariffIds, cartTariffMap, discounts, isUserNko, purchasesAmount, tariffs]);
|
||||
|
||||
addTariffsToCart();
|
||||
}, [cartTariffIds, cartTariffMap, discounts, purchasesAmount, tariffs]);
|
||||
useEffect(
|
||||
function cleanUpCart() {
|
||||
if (cartTariffIds)
|
||||
removeMissingCartTariffs(cartTariffIds, discounts, purchasesAmount, isUserNko);
|
||||
},
|
||||
[cartTariffIds, discounts, isUserNko, purchasesAmount]
|
||||
);
|
||||
|
||||
useEffect(
|
||||
function cleanUpCart() {
|
||||
if (cartTariffIds)
|
||||
removeMissingCartTariffs(cartTariffIds, discounts, purchasesAmount);
|
||||
},
|
||||
[cartTariffIds, discounts, purchasesAmount]
|
||||
);
|
||||
|
||||
return cart;
|
||||
return cart;
|
||||
}
|
||||
|
@ -1532,10 +1532,10 @@
|
||||
minimatch "^3.1.2"
|
||||
strip-json-comments "^3.1.1"
|
||||
|
||||
"@frontend/kitui@1.0.52":
|
||||
version "1.0.52"
|
||||
resolved "https://penahub.gitlab.yandexcloud.net/api/v4/projects/21/packages/npm/@frontend/kitui/-/@frontend/kitui-1.0.52.tgz#3b1c28f889da80ab325ab2b511108632fa925f1c"
|
||||
integrity sha1-Oxwo+InagKsyWrK1ERCGMvqSXxw=
|
||||
"@frontend/kitui@1.0.53":
|
||||
version "1.0.53"
|
||||
resolved "https://penahub.gitlab.yandexcloud.net/api/v4/projects/21/packages/npm/@frontend/kitui/-/@frontend/kitui-1.0.53.tgz#a663052d300b9e3c588346c646f276c9aec7de5d"
|
||||
integrity sha1-pmMFLTALnjxYg0bGRvJ2ya7H3l0=
|
||||
dependencies:
|
||||
immer "^10.0.2"
|
||||
reconnecting-eventsource "^1.6.2"
|
||||
|
Loading…
Reference in New Issue
Block a user