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 | 12x 665x 665x 665x 665x 665x 665x 665x 665x 665x 665x 665x 665x 665x 665x 665x 104x 104x | import { useEffect, useRef } from "react";
import useCheckFormType from "../../hooks/useCheckFormType";
import { useGetFundraiserVariants } from "@sections/PayBuilder/provider/useManageApi";
import { useLocation, useNavigate, useParams } from "react-router-dom";
import { isEmpty } from "lodash";
import { parsePriceToInteger } from "@utils/helpers";
import { useCart } from "@sections/PayBuilder/provider/CartContext";
import { useAddToCart } from "@hooks/merchant-api/cart";
import { usePayBuilderForm } from "@sections/PayBuilder/provider/PayBuilderFormProvider";
type Props = {
isPreview?: boolean;
};
export const useCreateDefaultFundraiserCart = ({ isPreview }: Props) => {
const { id } = useParams();
const location = useLocation();
const navigate = useNavigate();
const { data: variants } = useGetFundraiserVariants(id || "");
const { cartItems, addToCart, clearCart } = useCart();
const { addToCartHandler, product } = useAddToCart({
disableFetchCart: true,
newCartItems: cartItems,
showModal: false,
});
const { isFundraiser } = useCheckFormType();
const { methods } = usePayBuilderForm();
const values = methods.watch();
const suggestedAmount = values?.Donations?.suggestedAmount;
const optionalAmounts = values?.Donations?.optionalAmounts;
const enableSuggestedAmounts = values.Donations?.allowCustomAmount;
const amountToUs =
!isEmpty(optionalAmounts) &&
!isEmpty(suggestedAmount) &&
enableSuggestedAmounts
? suggestedAmount
: !isEmpty(optionalAmounts) && enableSuggestedAmounts
? optionalAmounts?.[0]
: "1.00";
const initializedId = useRef<string | undefined>(undefined);
useEffect(() => {
Eif (
[
isEmpty(variants),
isEmpty(variants?.data),
!id,
isPreview,
!isFundraiser,
!product?.id,
].some(Boolean)
)
return;
if (initializedId.current === id) return;
initializedId.current = id;
clearCart();
const variant = variants?.data[0];
const initPrice = amountToUs || "1.00";
const cartItem = {
id: String(variant.id),
productVariantName: variant.name,
productVariantID: variant.id,
productVariantPrice: parsePriceToInteger(initPrice, false),
quantity: 1,
unitPrice: 1,
productVariantImageURL: variant.imageURL || "",
recurringIntervalName: variant.defaultRecurringIntervalName || "once",
in_stock: 1,
price: parsePriceToInteger(initPrice, false),
};
addToCart(cartItem, 1, undefined, true);
navigate(".", {
replace: true,
state: {
...location.state,
initPrice,
},
});
(async () => {
await addToCartHandler(undefined, [cartItem], undefined, false);
})();
}, [variants, isPreview, id, product?.id]);
};
|