refactor, display total percent
This commit is contained in:
parent
cc65d8b257
commit
725a8861ca
@ -6,7 +6,7 @@ import { useState } from "react";
|
|||||||
import { GridSelectionModel } from "@mui/x-data-grid";
|
import { GridSelectionModel } from "@mui/x-data-grid";
|
||||||
import { testUser } from "@root/stores/mocks/user";
|
import { testUser } from "@root/stores/mocks/user";
|
||||||
import { useDiscountStore } from "@root/stores/discounts";
|
import { useDiscountStore } from "@root/stores/discounts";
|
||||||
import { calcCartData, createCartItem, findDiscountById, findDiscountFactorById, formatDiscountFactor } from "./calc";
|
import { calcCartData, createCartItem, findDiscountById, findDiscountFactor, formatDiscountFactor } from "./calc";
|
||||||
import { useTariffStore } from "@root/stores/tariffs";
|
import { useTariffStore } from "@root/stores/tariffs";
|
||||||
import { AnyDiscount, CartItemTotal } from "@root/model/cart";
|
import { AnyDiscount, CartItemTotal } from "@root/model/cart";
|
||||||
|
|
||||||
@ -28,8 +28,7 @@ export default function Cart({ selectedTariffs }: Props) {
|
|||||||
{cartItemTotal.envolvedDiscounts.map(discountId => (
|
{cartItemTotal.envolvedDiscounts.map(discountId => (
|
||||||
<DiscountTooltip
|
<DiscountTooltip
|
||||||
key={discountId}
|
key={discountId}
|
||||||
discounts={discounts}
|
discount={findDiscountById(discounts, discountId)}
|
||||||
discountId={discountId}
|
|
||||||
cartItemTotal={cartItemTotal}
|
cartItemTotal={cartItemTotal}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
@ -45,22 +44,26 @@ export default function Cart({ selectedTariffs }: Props) {
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
const envolvedCartDiscountsElement = (
|
const cartDiscounts = cartTotal?.envolvedCartDiscounts.map(discountId => findDiscountById(discounts, discountId));
|
||||||
|
const cartDiscountsResultFactor = cartDiscounts && 1 - cartDiscounts.reduce((acc, discount) => acc * findDiscountFactor(discount), 1);
|
||||||
|
|
||||||
|
const envolvedCartDiscountsElement = cartDiscounts && (
|
||||||
<Box sx={{
|
<Box sx={{
|
||||||
display: "inline-flex",
|
display: "inline-flex",
|
||||||
flexWrap: "wrap",
|
flexWrap: "wrap",
|
||||||
}}>
|
}}>
|
||||||
{cartTotal?.envolvedCartDiscounts.map((discountId, index, arr) => (
|
{cartDiscounts?.map((discount, index, arr) => (
|
||||||
<span key={discountId}>
|
<span key={discount._id}>
|
||||||
<DiscountTooltip
|
<DiscountTooltip
|
||||||
discounts={discounts}
|
discount={discount}
|
||||||
discountId={discountId}
|
|
||||||
/>
|
/>
|
||||||
{index < arr.length - 1 &&
|
{index < arr.length - 1 &&
|
||||||
<span>, </span>
|
<span>, </span>
|
||||||
}
|
}
|
||||||
</span>
|
</span>
|
||||||
))}
|
))}
|
||||||
|
|
||||||
|
{cartDiscountsResultFactor && `= ${formatDiscountFactor(cartDiscountsResultFactor)}`}
|
||||||
</Box>
|
</Box>
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -206,13 +209,11 @@ export default function Cart({ selectedTariffs }: Props) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function DiscountTooltip({ discounts, discountId, cartItemTotal }: {
|
function DiscountTooltip({ discount, cartItemTotal }: {
|
||||||
discounts: AnyDiscount[];
|
discount: AnyDiscount;
|
||||||
discountId: string;
|
|
||||||
cartItemTotal?: CartItemTotal;
|
cartItemTotal?: CartItemTotal;
|
||||||
}) {
|
}) {
|
||||||
const discount = findDiscountById(discounts, discountId);
|
const discountText = formatDiscountFactor(findDiscountFactor(discount, cartItemTotal?.tariff.privilege.privilegeId));
|
||||||
const discountText = formatDiscountFactor(findDiscountFactorById(discounts, discountId, cartItemTotal?.tariff.privilege.privilegeId));
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Tooltip title={
|
<Tooltip title={
|
||||||
|
@ -215,10 +215,7 @@ export function findDiscountById(discounts: AnyDiscount[], id: string) {
|
|||||||
return discount;
|
return discount;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function findDiscountFactorById(discounts: AnyDiscount[], id: string, privilegeId?: string) {
|
export function findDiscountFactor(discount: AnyDiscount, privilegeId?: string) {
|
||||||
const discount = discounts.find(discount => discount._id === id);
|
|
||||||
if (!discount) throw new Error("Discount not found by id");
|
|
||||||
|
|
||||||
switch (discount.conditionType) {
|
switch (discount.conditionType) {
|
||||||
case "cartPurchasesAmount":
|
case "cartPurchasesAmount":
|
||||||
return discount.factor;
|
return discount.factor;
|
||||||
|
Loading…
Reference in New Issue
Block a user