All files / lib/cart cart-store.tsx

88.88% Statements 40/45
86.95% Branches 20/23
93.33% Functions 14/15
88.88% Lines 40/45

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 1603x                                         3x                                                   3x     2x       2x 1x     1x 1x   1x       1x 1x       1x                             21x   21x 8x 6x     2x     21x 16x 11x     5x     21x 3x     21x 1x     21x 4x     21x       21x 1x     21x 1x     21x 1x     21x 21x                             21x       21x   21x       21x    
"use client";
 
import {
  createContext,
  useCallback,
  useContext,
  useEffect,
  useMemo,
  useReducer,
  type ReactNode,
} from "react";
 
import {
  cartReducer,
  getCartItemCount,
  getCartSubtotal,
  initialCartState,
} from "@/lib/cart/cart-reducer";
import type { CartLineItem, CartState } from "@/lib/cart/cart-types";
import type { Product } from "@/types/product";
 
const storageKey = "headless-commerce-cart";
 
/**
 * Public cart context contract exposed through `useCart`.
 */
export interface CartContextValue extends CartState {
  readonly addItem: (product: Product, quantity?: number) => void;
  readonly clearCart: () => void;
  readonly closeCart: () => void;
  readonly decrementItem: (slug: string) => void;
  readonly incrementItem: (slug: string) => void;
  readonly itemCount: number;
  readonly openCart: () => void;
  readonly removeItem: (slug: string) => void;
  readonly subtotalCents: number;
}
 
/**
 * Cart provider options used by the application shell and component tests.
 */
export interface CartProviderProps {
  readonly children: ReactNode;
  readonly initialState?: CartState;
  readonly persist?: boolean;
}
 
const CartContext = createContext<CartContextValue | null>(null);
 
function isRecord(value: unknown): value is Record<string, unknown> {
  return typeof value === "object" && value !== null;
}
 
function parseStoredItems(value: string | null): CartLineItem[] {
  if (!value) {
    return [];
  }
 
  try {
    const parsed: unknown = JSON.parse(value);
 
    Iif (!Array.isArray(parsed)) {
      return [];
    }
 
    return parsed.filter((item): item is CartLineItem => {
      Iif (!isRecord(item) || !isRecord(item.product)) {
        return false;
      }
 
      return typeof item.product.slug === "string" && typeof item.quantity === "number";
    });
  } catch {
    return [];
  }
}
 
/**
 * Client-side cart provider with reducer state and localStorage persistence.
 */
export function CartProvider({
  children,
  initialState,
  persist = true,
}: CartProviderProps): ReactNode {
  const [state, dispatch] = useReducer(cartReducer, initialState ?? initialCartState);
 
  useEffect(() => {
    if (!persist || initialState) {
      return;
    }
 
    dispatch({ items: parseStoredItems(window.localStorage.getItem(storageKey)), type: "hydrate" });
  }, [initialState, persist]);
 
  useEffect(() => {
    if (!persist) {
      return;
    }
 
    window.localStorage.setItem(storageKey, JSON.stringify(state.items));
  }, [persist, state.items]);
 
  const addItem = useCallback((product: Product, quantity?: number) => {
    dispatch({ product, quantity, type: "add" });
  }, []);
 
  const clearCart = useCallback(() => {
    dispatch({ type: "clear" });
  }, []);
 
  const closeCart = useCallback(() => {
    dispatch({ type: "close" });
  }, []);
 
  const decrementItem = useCallback((slug: string) => {
    dispatch({ slug, type: "decrement" });
  }, []);
 
  const incrementItem = useCallback((slug: string) => {
    dispatch({ slug, type: "increment" });
  }, []);
 
  const openCart = useCallback(() => {
    dispatch({ type: "open" });
  }, []);
 
  const removeItem = useCallback((slug: string) => {
    dispatch({ slug, type: "remove" });
  }, []);
 
  const value = useMemo<CartContextValue>(
    () => ({
      ...state,
      addItem,
      clearCart,
      closeCart,
      decrementItem,
      incrementItem,
      itemCount: getCartItemCount(state.items),
      openCart,
      removeItem,
      subtotalCents: getCartSubtotal(state.items),
    }),
    [addItem, clearCart, closeCart, decrementItem, incrementItem, openCart, removeItem, state],
  );
 
  return <CartContext.Provider value={value}>{children}</CartContext.Provider>;
}
 
export function useCart(): CartContextValue {
  const context = useContext(CartContext);
 
  Iif (!context) {
    throw new Error("useCart must be used inside CartProvider.");
  }
 
  return context;
}