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 { testUser } from "@root/stores/mocks/user";
|
||||
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 { AnyDiscount, CartItemTotal } from "@root/model/cart";
|
||||
|
||||
@ -28,8 +28,7 @@ export default function Cart({ selectedTariffs }: Props) {
|
||||
{cartItemTotal.envolvedDiscounts.map(discountId => (
|
||||
<DiscountTooltip
|
||||
key={discountId}
|
||||
discounts={discounts}
|
||||
discountId={discountId}
|
||||
discount={findDiscountById(discounts, discountId)}
|
||||
cartItemTotal={cartItemTotal}
|
||||
/>
|
||||
))}
|
||||
@ -44,23 +43,27 @@ export default function Cart({ selectedTariffs }: Props) {
|
||||
price: cartItemTotal.totalPrice,
|
||||
};
|
||||
});
|
||||
|
||||
const cartDiscounts = cartTotal?.envolvedCartDiscounts.map(discountId => findDiscountById(discounts, discountId));
|
||||
const cartDiscountsResultFactor = cartDiscounts && 1 - cartDiscounts.reduce((acc, discount) => acc * findDiscountFactor(discount), 1);
|
||||
|
||||
const envolvedCartDiscountsElement = (
|
||||
const envolvedCartDiscountsElement = cartDiscounts && (
|
||||
<Box sx={{
|
||||
display: "inline-flex",
|
||||
flexWrap: "wrap",
|
||||
}}>
|
||||
{cartTotal?.envolvedCartDiscounts.map((discountId, index, arr) => (
|
||||
<span key={discountId}>
|
||||
{cartDiscounts?.map((discount, index, arr) => (
|
||||
<span key={discount._id}>
|
||||
<DiscountTooltip
|
||||
discounts={discounts}
|
||||
discountId={discountId}
|
||||
discount={discount}
|
||||
/>
|
||||
{index < arr.length - 1 &&
|
||||
<span>, </span>
|
||||
}
|
||||
</span>
|
||||
))}
|
||||
|
||||
{cartDiscountsResultFactor && `= ${formatDiscountFactor(cartDiscountsResultFactor)}`}
|
||||
</Box>
|
||||
);
|
||||
|
||||
@ -206,13 +209,11 @@ export default function Cart({ selectedTariffs }: Props) {
|
||||
);
|
||||
}
|
||||
|
||||
function DiscountTooltip({ discounts, discountId, cartItemTotal }: {
|
||||
discounts: AnyDiscount[];
|
||||
discountId: string;
|
||||
function DiscountTooltip({ discount, cartItemTotal }: {
|
||||
discount: AnyDiscount;
|
||||
cartItemTotal?: CartItemTotal;
|
||||
}) {
|
||||
const discount = findDiscountById(discounts, discountId);
|
||||
const discountText = formatDiscountFactor(findDiscountFactorById(discounts, discountId, cartItemTotal?.tariff.privilege.privilegeId));
|
||||
const discountText = formatDiscountFactor(findDiscountFactor(discount, cartItemTotal?.tariff.privilege.privilegeId));
|
||||
|
||||
return (
|
||||
<Tooltip title={
|
||||
|
@ -215,10 +215,7 @@ export function findDiscountById(discounts: AnyDiscount[], id: string) {
|
||||
return discount;
|
||||
}
|
||||
|
||||
export function findDiscountFactorById(discounts: AnyDiscount[], id: string, privilegeId?: string) {
|
||||
const discount = discounts.find(discount => discount._id === id);
|
||||
if (!discount) throw new Error("Discount not found by id");
|
||||
|
||||
export function findDiscountFactor(discount: AnyDiscount, privilegeId?: string) {
|
||||
switch (discount.conditionType) {
|
||||
case "cartPurchasesAmount":
|
||||
return discount.factor;
|
||||
|
Loading…
Reference in New Issue
Block a user