All files / src/hooks/merchant-api/cart useGetCart.tsx

100% Statements 9/9
100% Branches 2/2
100% Functions 4/4
100% Lines 9/9

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                                17x 1182x 27x           1182x     27x   27x 6x                                   27x                 1182x              
import { customInstance } from "@services/api";
import { useQuery } from "react-query";
import { QKEY_LIST_CART } from "@constants/queryKeys";
 
export type CartDataType = {
  id: number;
  title: string;
  amount: number;
  quantity: number;
  recurringIntervalName: string;
  recurringFrequency: number;
  imageUrl?: string;
  campaign: string;
  items?: any[];
};
 
export const useGetCart = (disableFetchCart?: boolean) => {
  const getCart = () => {
    return customInstance({
      method: "GET",
      url: `/cart`,
    });
  };
 
  const { data, isLoading, isError, refetch } = useQuery(
    QKEY_LIST_CART,
    async () => {
      const data = await getCart();
      // Match the transformation from useCartItems to ensure consistency across the app
      const customCartItems = data?.items?.map((item: any) => {
        return {
          ...item,
          id: item.id,
          title: item.productVariantName,
          amount: item.unitPrice / 100,
          productVariantPrice: item.productVariantPrice / 100,
          unitPrice: item.unitPrice / 100,
          quantity: item.quantity,
          recurringIntervalName: item.recurringIntervalName,
          recurringFrequency: item.recurringFrequency,
          imageUrl: item.imageURL,
          orderID: item.orderID,
          campaign:
            item.productType?.charAt(0).toUpperCase() +
            item.productType?.slice(1),
        };
      });
 
      return { ...data, items: customCartItems || [] };
    },
    {
      staleTime: 1000 * 10,
      cacheTime: 1000 * 60 * 3,
      enabled: !disableFetchCart,
    },
  );
 
  return {
    data,
    isLoading,
    isError,
    refetch,
  };
};