Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | 68x 68x 68x 68x 68x 12x 68x 68x 48x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 12x 68x 68x | import { CARD_FEES_LABEL, CURRENCY } from "@constants/constants";
import { Stack } from "@mui/material";
import { CheckCircleIcon } from "@phosphor-icons/react";
import { FEE_PAYER_OPTIONS } from "@sections/PayBuilder/Checkout/consts";
import { useCardProcessingFees } from "@sections/PayBuilder/Checkout/hooks/useCardProcessingFees";
import useCheckFormType from "@sections/PayBuilder/components/hooks/useCheckFormType";
import { ICartItem, useCart } from "@sections/PayBuilder/provider/CartContext";
import { usePayBuilderForm } from "@sections/PayBuilder/provider/PayBuilderFormProvider";
import { useIsPreviewMode } from "@sections/PayBuilder/provider/useIsPreviewMode";
import GiveText from "@shared/Text/GiveText";
import { useAppTheme } from "@theme/v2/Provider";
import { parseAmount } from "@utils/index";
import { format, addYears, addMonths } from "date-fns";
function getUpcomingCharge(items: ICartItem[]): number {
let filteredItems: ICartItem[] = [];
/**
* In order to calculate the upcoming charge, we need to find the closest payment occurance and calculate
* sum of the items' with respective occurance.
*/
Iif (items.some((item) => item.recurringIntervalName === "monthly")) {
filteredItems = items.filter(
(item) => item.recurringIntervalName === "monthly",
);
} else Iif (items.some((item) => item.recurringIntervalName === "quarterly")) {
filteredItems = items.filter(
(item) => item.recurringIntervalName === "quarterly",
);
} else Iif (items.some((item) => item.recurringIntervalName === "yearly")) {
filteredItems = items.filter(
(item) => item.recurringIntervalName === "yearly",
);
}
// Calculate total upcoming charge
return filteredItems.reduce(
(total, item) => total + Number(item.productVariantPrice) * item.quantity,
0,
);
}
const CartTotalSection = ({
cartItems,
fees,
entries,
}: {
cartItems: ICartItem[];
fees: null | number;
entries?: string;
}) => {
const { subTotal, totalAmount, displayFees } = useCart();
const hasUpocomingCharge = cartItems.some(
(item) => item.recurringIntervalName !== "once",
);
const { isPreviewMode, isPeekModalOpened } = useIsPreviewMode();
const { palette } = useAppTheme();
const { isFundraiser, isMembership, isSweepstake } = useCheckFormType();
const recurringIntervalName = cartItems?.[0]?.recurringIntervalName;
const isYears = recurringIntervalName === "yearly";
const { methods: leftSidepanelMethods } = usePayBuilderForm();
const isCustomerPayingFees =
leftSidepanelMethods.watch().Checkout.feePayer !==
FEE_PAYER_OPTIONS.MERCHANT;
const subTotalItems = getSubTotalItems(cartItems);
const numberOfMonths = (() => {
switch (recurringIntervalName) {
case "monthly":
return 1;
case "quarterly":
return 3;
default:
return 0;
}
})();
const upcomingCharge_1 = isMembership
? `Paid subscription starts on ${format(new Date(), "MM/dd/yyyy")}`
: `Upcoming charge ${getUpcomingCharge(cartItems)} ${CURRENCY}`;
const subscriptionStartDate = isYears
? addYears(new Date(), 1)
: addMonths(new Date(), numberOfMonths);
const upcomingCharge_2 = isMembership
? `Automatically renews on ${format(subscriptionStartDate, "MM/dd/yyyy")}`
: `Paid subscription starts on ${format(new Date(), "MM/dd/yyyy")}`;
const upcomingCharge = [upcomingCharge_1, upcomingCharge_2, `Cancel anytime`];
const isPreview = isPreviewMode || isPeekModalOpened;
const showFees =
Boolean(isPreview && isCustomerPayingFees) || Boolean(fees && displayFees);
return (
<Stack
spacing={2}
mt={2}
sx={{
borderRadius: "12px",
padding: "16px",
backgroundColor: palette.primitive?.transparent["darken-5"],
}}
>
{!isSweepstake && !isFundraiser && (
<Stack direction="row" justifyContent="space-between">
<GiveText variant="bodyS" color="primary">
Subtotal{" "}
{!isMembership &&
`(${subTotalItems} ${subTotalItems === 1 ? "item" : "items"})`}
</GiveText>
<GiveText variant="bodyS" color="primary">
{subTotal} {CURRENCY}
</GiveText>
</Stack>
)}
{showFees && (
<Stack direction="row" justifyContent="space-between">
<GiveText variant="bodyS" color="primary">
{CARD_FEES_LABEL}
</GiveText>
<GiveText variant="bodyS" color="primary">
{parseAmount(isPreview ? 0 : fees)} {CURRENCY}
</GiveText>
</Stack>
)}
<Stack
sx={{
...(isSweepstake || (isFundraiser && !showFees)
? {}
: {
borderTop: `1px solid ${palette.primitive?.transparent["darken-10"]}`,
paddingTop: "16px",
}),
}}
>
<Stack direction="row" justifyContent="space-between">
<GiveText variant="bodyL">
{hasUpocomingCharge ? "Due Today" : "Total"}
</GiveText>
<GiveText variant="bodyL">
<GiveText variant="bodyL">{`${totalAmount} ${CURRENCY}`}</GiveText>
</GiveText>
</Stack>
{hasUpocomingCharge && (
<Stack mt={2} gap="12px">
{upcomingCharge.map((item, index) => {
return (
<Stack
key={index}
direction="row"
spacing={1}
alignItems="center"
>
<CheckCircleIcon size={18} color={palette.text.secondary} />
<GiveText variant="bodyXS" color="secondary">
{item}
</GiveText>
</Stack>
);
})}
</Stack>
)}
</Stack>
</Stack>
);
};
export default CartTotalSection;
//for Subtotal items we have to calculate the sum of all item * quntity
const getSubTotalItems = (items: ICartItem[]) => {
Iif (!Array.isArray(items)) return 0;
return items.reduce((total, item) => total + (item.quantity || 0), 0);
};
|