All files / src/sections/PayBuilder/Checkout/hooks useOptionItemHandler.tsx

65% Statements 13/20
28.57% Branches 4/14
75% Functions 3/4
65% Lines 13/20

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                  12x 36x 36x 36x 36x         36x           4x 4x                                             36x       36x           4x 4x     36x        
// useCartHandler.ts
import { useCart } from "@sections/PayBuilder/provider/CartContext";
import { useParams } from "react-router-dom";
import { useAddToCart } from "@hooks/merchant-api/cart";
import { useDebouncedCallback } from "use-debounce";
import { isEmpty } from "lodash";
import { parsePriceToInteger } from "@utils/helpers";
import { useGetFundraiserVariants } from "@sections/PayBuilder/provider/useManageApi";
 
export const useOptionItemHandler = () => {
  const { cartItems, addToCart, clearCart } = useCart();
  const { id: productId } = useParams();
  const { data } = useGetFundraiserVariants(productId || "");
  const { addToCartHandler } = useAddToCart({
    newCartItems: cartItems,
    showModal: false,
  });
 
  const handleAddToCart = async (
    item: any,
    recurrenceType?: string,
    quantity?: number,
    max?: number,
  ) => {
    Eif (isEmpty(data) || isEmpty(data.data)) {
      return;
    }
    clearCart();
    const variant = data.data[0];
    const formattedRecurrence =
      recurrenceType === "one_time" ? "once" : recurrenceType;
    const addedQuantity = quantity || 1;
    const cartItem = {
      id: String(variant.id),
      productVariantName: variant.name,
      productVariantID: variant.id,
      productVariantPrice: parsePriceToInteger(item, false),
      quantity: addedQuantity,
      unitPrice: item,
      productVariantImageURL: variant.imageURL || "",
      recurringIntervalName: formattedRecurrence,
      in_stock: max || 1,
      price: parsePriceToInteger(item, false),
    };
 
    addToCart(cartItem, addedQuantity, undefined, true);
  };
 
  const debouncedAddToCartHandler = useDebouncedCallback(async () => {
    await addToCartHandler();
  }, 500);
 
  const debouncedAddDonationToCart = async (
    item: any,
    recurrenceType?: string,
    quantity?: number,
    max?: number,
  ) => {
    await handleAddToCart(item, recurrenceType, quantity, max);
    debouncedAddToCartHandler();
  };
 
  return {
    debouncedAddDonationToCart,
  };
};