All files / src/sections/PayBuilder/items useAddMembershipToCart.tsx

9.09% Statements 1/11
0% Branches 0/10
0% Functions 0/3
9.09% Lines 1/11

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                14x                                                                              
import { parsePriceToInteger } from "@utils/helpers";
import { useCart } from "../provider/CartContext";
import { useAddToCart } from "@hooks/merchant-api/cart";
import { PUBLIC_PRODUCT_ITEM_MODAL } from "modals/modal_names";
import NiceModal from "@ebay/nice-modal-react";
import { Product } from "../components/products/types";
import { Dispatch, SetStateAction } from "react";
 
export const useAddMembershipToCart = ({
  showCheckoutBottomSheet,
}: {
  showCheckoutBottomSheet?: Dispatch<SetStateAction<boolean>>;
}) => {
  const { clearCart, addToCart } = useCart();
  const { addToCartHandler, isLoading } = useAddToCart({
    disableFetchCart: false,
    showModal: false,
  });
  const onAddMembershipToCart = async (product: Product) => {
    clearCart();
    const item = {
      id: String(product.id) || "",
      productVariantName: product.title,
      productVariantID: Number(product.variantID) ?? Number(product.id),
      productVariantPrice: parsePriceToInteger(product.amount || "", false),
      quantity: 1,
      unitPrice: product.amount || "",
      productVariantImageURL: product.thumbnail || "",
      recurringIntervalName: product.paymentType,
      recurringInterval: product?.paymentType,
      in_stock: null, // BE: for membership they excpect inventory to be null
      signupFee: product.signupFee,
    };
 
    addToCart(item, 1, undefined, true);
 
    await addToCartHandler(() => {
      NiceModal.hide(PUBLIC_PRODUCT_ITEM_MODAL);
      showCheckoutBottomSheet?.(true);
    }, [item]);
  };
 
  return {
    onAddMembershipToCart,
    isLoading,
  };
};