All files / src/contexts CheckoutContext.tsx

99.12% Statements 338/341
89.74% Branches 35/39
100% Functions 6/6
99.12% Lines 338/341

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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 3421x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 22x 22x 22x 22x 22x 22x 6x 22x 22x 5x 5x 5x 5x 5x 5x 22x 22x 22x 1x 22x 22x 1x 1x 1x 1x 1x 1x 22x 22x 22x 3x 22x 22x 2x 22x 22x 2x 22x 22x 1x 22x 22x 1x 22x 22x   22x 22x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 6x 34x 34x 34x 5x 34x 34x 34x 1x 34x 34x 34x 1x 34x 34x 34x 3x 34x 34x 34x 2x 34x 34x 34x 2x 34x 34x 34x 1x 34x 34x 34x 1x 34x 34x 34x 34x 33x 33x 27x 33x 4x 33x 2x 33x   33x   33x 34x 34x 34x 34x 34x 34x 34x 34x 34x 26x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 1x 1x 1x 1x 1x 1x 1x 1x 1x 36x 36x 36x 36x 2x 2x 34x 34x 34x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
'use client';
 
/**
 * CheckoutContext
 *
 * Centralized state management for checkout flow.
 * Eliminates prop drilling through the checkout component hierarchy.
 */
 
import {
  createContext,
  useContext,
  useReducer,
  useMemo,
  useCallback,
  ReactNode,
} from 'react';
 
// ============================================================================
// TYPES
// ============================================================================
 
export interface CheckoutAddress {
  id?: number;
  firstName: string;
  lastName: string;
  street: string;
  city: string;
  state: string;
  zipCode: string;
  country: string;
  phone?: string;
}
 
export type CheckoutStep = 'shipping' | 'payment' | 'review' | 'confirmation';
 
export interface CheckoutState {
  step: CheckoutStep;
  shippingAddress: CheckoutAddress | null;
  billingAddress: CheckoutAddress | null;
  sameAsShipping: boolean;
  shippingMethod: string | null;
  paymentMethod: string | null;
  isProcessing: boolean;
  error: string | null;
}
 
type CheckoutAction =
  | { type: 'SET_STEP'; payload: CheckoutStep }
  | { type: 'SET_SHIPPING_ADDRESS'; payload: CheckoutAddress }
  | { type: 'SET_BILLING_ADDRESS'; payload: CheckoutAddress }
  | { type: 'SET_SAME_AS_SHIPPING'; payload: boolean }
  | { type: 'SET_SHIPPING_METHOD'; payload: string }
  | { type: 'SET_PAYMENT_METHOD'; payload: string }
  | { type: 'SET_PROCESSING'; payload: boolean }
  | { type: 'SET_ERROR'; payload: string | null }
  | { type: 'RESET' };
 
// ============================================================================
// INITIAL STATE
// ============================================================================
 
const initialState: CheckoutState = {
  step: 'shipping',
  shippingAddress: null,
  billingAddress: null,
  sameAsShipping: true,
  shippingMethod: null,
  paymentMethod: null,
  isProcessing: false,
  error: null,
};
 
// ============================================================================
// REDUCER
// ============================================================================
 
function checkoutReducer(
  state: CheckoutState,
  action: CheckoutAction
): CheckoutState {
  switch (action.type) {
    case 'SET_STEP':
      return { ...state, step: action.payload, error: null };
 
    case 'SET_SHIPPING_ADDRESS':
      return {
        ...state,
        shippingAddress: action.payload,
        billingAddress: state.sameAsShipping
          ? action.payload
          : state.billingAddress,
      };
 
    case 'SET_BILLING_ADDRESS':
      return { ...state, billingAddress: action.payload };
 
    case 'SET_SAME_AS_SHIPPING':
      return {
        ...state,
        sameAsShipping: action.payload,
        billingAddress: action.payload
          ? state.shippingAddress
          : state.billingAddress,
      };
 
    case 'SET_SHIPPING_METHOD':
      return { ...state, shippingMethod: action.payload };
 
    case 'SET_PAYMENT_METHOD':
      return { ...state, paymentMethod: action.payload };
 
    case 'SET_PROCESSING':
      return { ...state, isProcessing: action.payload };
 
    case 'SET_ERROR':
      return { ...state, error: action.payload, isProcessing: false };
 
    case 'RESET':
      return initialState;
 
    default:
      return state;
  }
}
 
// ============================================================================
// CONTEXT
// ============================================================================
 
export interface CheckoutContextValue extends CheckoutState {
  // Actions
  setStep: (step: CheckoutStep) => void;
  setShippingAddress: (address: CheckoutAddress) => void;
  setBillingAddress: (address: CheckoutAddress) => void;
  setSameAsShipping: (same: boolean) => void;
  setShippingMethod: (method: string) => void;
  setPaymentMethod: (method: string) => void;
  setProcessing: (processing: boolean) => void;
  setError: (error: string | null) => void;
  reset: () => void;
 
  // Computed values
  canProceed: boolean;
  isComplete: boolean;
}
 
const CheckoutContext = createContext<CheckoutContextValue | null>(null);
 
// ============================================================================
// PROVIDER
// ============================================================================
 
interface CheckoutProviderProps {
  children: ReactNode;
  initialStep?: CheckoutStep;
}
 
export function CheckoutProvider({
  children,
  initialStep = 'shipping',
}: CheckoutProviderProps) {
  const [state, dispatch] = useReducer(checkoutReducer, {
    ...initialState,
    step: initialStep,
  });
 
  // Memoized action creators
  const setStep = useCallback((step: CheckoutStep) => {
    dispatch({ type: 'SET_STEP', payload: step });
  }, []);
 
  const setShippingAddress = useCallback((address: CheckoutAddress) => {
    dispatch({ type: 'SET_SHIPPING_ADDRESS', payload: address });
  }, []);
 
  const setBillingAddress = useCallback((address: CheckoutAddress) => {
    dispatch({ type: 'SET_BILLING_ADDRESS', payload: address });
  }, []);
 
  const setSameAsShipping = useCallback((same: boolean) => {
    dispatch({ type: 'SET_SAME_AS_SHIPPING', payload: same });
  }, []);
 
  const setShippingMethod = useCallback((method: string) => {
    dispatch({ type: 'SET_SHIPPING_METHOD', payload: method });
  }, []);
 
  const setPaymentMethod = useCallback((method: string) => {
    dispatch({ type: 'SET_PAYMENT_METHOD', payload: method });
  }, []);
 
  const setProcessing = useCallback((processing: boolean) => {
    dispatch({ type: 'SET_PROCESSING', payload: processing });
  }, []);
 
  const setError = useCallback((error: string | null) => {
    dispatch({ type: 'SET_ERROR', payload: error });
  }, []);
 
  const reset = useCallback(() => {
    dispatch({ type: 'RESET' });
  }, []);
 
  // Computed values
  const canProceed = useMemo(() => {
    switch (state.step) {
      case 'shipping':
        return !!state.shippingAddress && !!state.shippingMethod;
      case 'payment':
        return !!state.paymentMethod;
      case 'review':
        return !state.isProcessing;
      case 'confirmation':
        return true;
      default:
        return false;
    }
  }, [
    state.step,
    state.shippingAddress,
    state.shippingMethod,
    state.paymentMethod,
    state.isProcessing,
  ]);
 
  const isComplete = useMemo(() => {
    return state.step === 'confirmation';
  }, [state.step]);
 
  // Memoized context value
  const value = useMemo<CheckoutContextValue>(
    () => ({
      ...state,
      setStep,
      setShippingAddress,
      setBillingAddress,
      setSameAsShipping,
      setShippingMethod,
      setPaymentMethod,
      setProcessing,
      setError,
      reset,
      canProceed,
      isComplete,
    }),
    [
      state,
      setStep,
      setShippingAddress,
      setBillingAddress,
      setSameAsShipping,
      setShippingMethod,
      setPaymentMethod,
      setProcessing,
      setError,
      reset,
      canProceed,
      isComplete,
    ]
  );
 
  return (
    <CheckoutContext.Provider value={value}>
      {children}
    </CheckoutContext.Provider>
  );
}
 
// ============================================================================
// HOOK
// ============================================================================
 
/**
 * Hook to access checkout context
 * @throws Error if used outside CheckoutProvider
 */
export function useCheckout(): CheckoutContextValue {
  const context = useContext(CheckoutContext);
 
  if (!context) {
    throw new Error('useCheckout must be used within a CheckoutProvider');
  }
 
  return context;
}
 
/**
 * Hook to access specific checkout step state
 * Provides optimized subscription to avoid unnecessary re-renders
 */
export function useCheckoutStep(): {
  step: CheckoutStep;
  setStep: (step: CheckoutStep) => void;
  canProceed: boolean;
} {
  const { step, setStep, canProceed } = useCheckout();
  return { step, setStep, canProceed };
}
 
/**
 * Hook to access shipping-related checkout state
 */
export function useCheckoutShipping() {
  const {
    shippingAddress,
    shippingMethod,
    setShippingAddress,
    setShippingMethod,
  } = useCheckout();
 
  return {
    shippingAddress,
    shippingMethod,
    setShippingAddress,
    setShippingMethod,
  };
}
 
/**
 * Hook to access billing-related checkout state
 */
export function useCheckoutBilling() {
  const {
    billingAddress,
    sameAsShipping,
    setBillingAddress,
    setSameAsShipping,
    shippingAddress,
  } = useCheckout();
 
  return {
    billingAddress,
    sameAsShipping,
    setBillingAddress,
    setSameAsShipping,
    shippingAddress,
  };
}
 
export default CheckoutContext;