import { publishSafely, toCommerceCart, toLineChange } from '@components/frontStore/commerceEventData.js'; import { dispatchCommerceEvent } from '@evershop/evershop/components/common/commerceEvents'; import { _ } from '@evershop/evershop/lib/locale/translate/_'; import { Address, AddressGraphql } from '@evershop/evershop/types/address'; import { ApiResponse } from '@evershop/evershop/types/apiResponse'; import { produce } from 'immer'; import React, { createContext, useReducer, useContext, ReactNode, useCallback } from 'react'; import { useClient } from 'urql'; const ShippingMethodsQuery = ` query GetCartShippingMethods($country: String!, $administrativeArea: String, $locality: String, $dependentLocality: String, $postalCode: String) { myCart { availableShippingMethods(country: $country, administrativeArea: $administrativeArea, locality: $locality, dependentLocality: $dependentLocality, postalCode: $postalCode) { providerCode code name carrier cost { value text } } } } `; export enum CartSyncTrigger { ADD_ITEM = 'addItem', REMOVE_ITEM = 'removeItem', UPDATE_ITEM = 'updateItem', ADD_PAYMENT_METHOD = 'addPaymentMethod', ADD_SHIPPING_METHOD = 'addShippingMethod', ADD_SHIPPING_ADDRESS = 'addShippingAddress', ADD_BILLING_ADDRESS = 'addBillingAddress', ADD_CONTACT_INFO = 'addContactInfo', APPLY_COUPON = 'applyCoupon', REMOVE_COUPON = 'removeCoupon' } export interface CartItem { cartItemId: string; uuid: string; productId: string; qty: number; productSku: string; productName: string; productUrl: string; thumbnail?: string; noShippingRequired: boolean; productWeight: { value: number; unit: string; }; variantOptions?: { attributeCode: string; attributeName: string; attributeId: number; optionId: number; optionText: string; }[]; productPrice: { value: number; text: string; }; productPriceInclTax: { value: number; text: string; }; finalPrice: { value: number; text: string; }; finalPriceInclTax: { value: number; text: string; }; taxPercent: number; taxAmount: { value: number; text: string; }; taxAmountBeforeDiscount: { value: number; text: string; }; discountAmount: { value: number; text: string; }; lineTotal: { value: number; text: string; }; subTotal: { value: number; text: string; }; lineTotalWithDiscount: { value: number; text: string; }; lineTotalWithDiscountInclTax: { value: number; text: string; }; lineTotalInclTax: { value: number; text: string; }; total: { value: number; text: string; }; variantGroupId?: number; removeApi: string; // API endpoint to remove item from cart updateQtyApi: string; // API endpoint to update item quantity errors?: string[]; // Validation errors for this item category?: { name?: string | null } | null; // For the commerce events } export interface PaymentMethod { id: string; name: string; code: string; } export interface ShippingMethod { id: string; name: string; code: string; price: number; } /** Destination fields that affect shipping quotes (spec § 3.10, D-16). */ export interface ShippingAddressParams { country: string; administrativeArea?: string; locality?: string; dependentLocality?: string; postalCode?: string; } export interface CartError { field?: string; message: string; code?: string; } // Extensible cart data interface - third-party extensions can add any fields from server-side cart data export interface CartData { uuid?: string; // Cart unique identifier currency: string; // Currency code totalQty: number; // Total quantity of items totalWeight: { value: number; unit: string; }; // Total weight of items customerId?: number; // Optional customer ID customerGroupId?: number; // Optional customer group ID customerEmail?: string; // Optional customer email customerFullName?: string; // Optional customer full name coupon?: string; // Coupon code applied to cart noShippingRequired: boolean; shippingMethod?: string; // Selected shipping method code shippingMethodName?: string; // Selected shipping method name paymentMethod?: string; // Selected payment method code paymentMethodName?: string; // Selected payment method name shippingNote?: string; // Shipping note items: CartItem[]; taxAmount: { value: number; text: string; }; totalTaxAmount: { value: number; text: string; }; taxAmountBeforeDiscount: { value: number; text: string; }; discountAmount: { value: number; text: string; }; shippingFeeExclTax: { value: number; text: string; }; shippingFeeInclTax: { value: number; text: string; }; shippingTaxAmount: { value: number; text: string; }; subTotal: { value: number; text: string; }; subTotalInclTax: { value: number; text: string; }; subTotalWithDiscount: { value: number; text: string; }; subTotalWithDiscountInclTax: { value: number; text: string; }; grandTotal: { value: number; text: string; }; billingAddress?: AddressGraphql; shippingAddress?: AddressGraphql; createdAt: { value: string; text: string; }; updatedAt: { value: string; text: string; }; // API endpoints addItemApi: string; addPaymentMethodApi: string; addShippingMethodApi: string; addContactInfoApi: string; addAddressApi: string; addNoteApi: string; applyCouponApi: string; checkoutApi: string; removeCouponApi?: string; // Available methods availablePaymentMethods: { code: string; name: string; }[]; availableShippingMethods: { /** * Required. Identifies the provider this method came from — Core, USPS, * EasyPost, etc. The server-side GraphQL field is `String!`. The * storefront passes it through to `addShippingMethod` so the cart can * route validation to the correct provider's `validateMethod`. * Silently defaulting to `'core'` mis-routes non-core methods. */ providerCode: string; code: string; name: string; cost?: { value: number; text: string; }; }[]; // Errors errors: CartError[]; error: string | null; [extendedFields: string]: unknown; // Allow third-party extensions to add fields } // Complete cart state with detailed loading states export interface CartState { data: CartData; // Cart data, can be undefined if not initialized loading: boolean; // Overall loading state (true if any operation is loading) - derived from loadingStates loadingStates: { addingItem: boolean; removingItem: string | null; // Item ID being removed, null if none updatingItem: string | null; // Item ID being updated, null if none addingPaymentMethod: boolean; addingShippingMethod: boolean; addingShippingAddress: boolean; addingBillingAddress: boolean; /** * Never true: `addContactInfo` saves the email in the background, and * the email changes no amount, so nothing shows as loading. Kept for * code that reads it. */ addingContactInfo: boolean; applyingCoupon: boolean; removingCoupon: boolean; fetchingShippingMethods: boolean; // New loading state for fetching shipping methods }; syncStatus: { syncing: boolean; // Whether a sync operation is in progress synced: boolean; // Whether the last sync was successful trigger?: string; // The reason/trigger that caused the sync (can be internal enum or external string) }; } // Action type for cart operations type CartAction = | { type: 'SET_CART'; payload: Partial } | { type: 'SET_SPECIFIC_LOADING'; payload: { operation: keyof CartState['loadingStates']; loading: boolean; itemId?: string; }; } | { type: 'SET_ERROR'; payload: string | null } | { type: 'CLEAR_ERROR' } | { type: 'SET_CUSTOMER_EMAIL'; payload: string } | { type: 'SET_SYNC_STATUS'; payload: { syncing?: boolean; synced?: boolean; trigger?: string }; }; // The shape of the functions that will be returned by the useCartDispatch hook interface CartDispatch { addItem: (payload: { sku: string; qty: number }) => Promise; removeItem: (itemId: string) => Promise; updateItem: ( itemId: string, payload: { qty: number; action: 'increase' | 'decrease' } ) => Promise; addPaymentMethod: (code: string, name: string) => Promise; addShippingMethod: ( code: string, name: string, providerCode: string ) => Promise; addShippingAddress: (address: Address) => Promise; addBillingAddress: (address: Address) => Promise; addContactInfo: (contactInfo: { email: string }) => Promise; applyCoupon: (couponCode: string) => Promise; removeCoupon: () => Promise; clearError: () => void; isShippingRequired: () => boolean; isReadyForCheckout: () => boolean; getErrors: () => CartError[]; getId: () => string | null; fetchAvailableShippingMethods: ( params: ShippingAddressParams ) => Promise; // Resolves with the new cart, or undefined when it could not be fetched syncCartWithServer: (trigger?: string) => Promise; } /** Publish the quantity change of one line (none when it did not change) */ function publishLineChange( before: CartItem | undefined, after: CartItem | undefined, cart: CartData ): void { publishSafely(() => { const change = toLineChange(before, after, cart); if (change) { dispatchCommerceEvent(change.name, change.data); } }); } const cartReducer = (state: CartState, action: CartAction): CartState => { return produce(state, (draft) => { switch (action.type) { case 'SET_CART': if (draft.data) { Object.assign(draft.data, action.payload); draft.data.error = null; } else { draft.data = action.payload as CartData; } // Clear all loading states when cart is set draft.loadingStates = { addingItem: false, removingItem: null, updatingItem: null, addingPaymentMethod: false, addingShippingMethod: false, addingShippingAddress: false, addingBillingAddress: false, addingContactInfo: false, applyingCoupon: false, removingCoupon: false, fetchingShippingMethods: false }; draft.loading = false; break; case 'SET_SPECIFIC_LOADING': const { operation, loading, itemId } = action.payload; if (operation === 'removingItem' || operation === 'updatingItem') { draft.loadingStates[operation] = loading ? itemId || null : null; } else { (draft.loadingStates as any)[operation] = loading; } // Update overall loading state based on loadingStates draft.loading = Object.values(draft.loadingStates).some( (state) => state === true || (typeof state === 'string' && state !== null) ); break; case 'SET_ERROR': if (draft.data) { draft.data.error = action.payload; } // Clear all loading states on error draft.loadingStates = { addingItem: false, removingItem: null, updatingItem: null, addingPaymentMethod: false, addingShippingMethod: false, addingShippingAddress: false, addingBillingAddress: false, addingContactInfo: false, applyingCoupon: false, removingCoupon: false, fetchingShippingMethods: false }; draft.loading = false; break; case 'CLEAR_ERROR': if (draft.data) { draft.data.error = null; draft.data.errors = []; } break; // Only the email: SET_CART would also clear the error and the loading // states of other operations still running case 'SET_CUSTOMER_EMAIL': if (draft.data) { draft.data.customerEmail = action.payload; } break; case 'SET_SYNC_STATUS': Object.assign(draft.syncStatus, action.payload); break; } }); }; const CartStateContext = createContext(undefined); const CartDispatchContext = createContext(undefined); interface CartProviderProps { children: ReactNode; query: string; cart?: CartData; addMineCartItemApi: string; } const initialEmptyState: CartState = { data: { currency: 'USD', addItemApi: '', // initial addItemApi items: [], totalQty: 0, noShippingRequired: false, totalWeight: { value: 0, unit: 'kg' }, billingAddress: undefined, shippingAddress: undefined, errors: [], error: null, taxAmount: { value: 0, text: '0.00' }, totalTaxAmount: { value: 0, text: '0.00' }, taxAmountBeforeDiscount: { value: 0, text: '0.00' }, discountAmount: { value: 0, text: '0.00' }, shippingFeeExclTax: { value: 0, text: '0.00' }, shippingFeeInclTax: { value: 0, text: '0.00' }, shippingTaxAmount: { value: 0, text: '0.00' }, subTotal: { value: 0, text: '0.00' }, subTotalInclTax: { value: 0, text: '0.00' }, subTotalWithDiscount: { value: 0, text: '0.00' }, subTotalWithDiscountInclTax: { value: 0, text: '0.00' }, grandTotal: { value: 0, text: '0.00' }, createdAt: { value: '', text: '' }, updatedAt: { value: '', text: '' }, coupon: '', addPaymentMethodApi: '', // Will be set by server addShippingMethodApi: '', // Will be set by server addAddressApi: '', // Will be set by server applyCouponApi: '', // Will be set by server addNoteApi: '', // Will be set by server addContactInfoApi: '', // Will be set by server checkoutApi: '', // Will be set by server availablePaymentMethods: [], availableShippingMethods: [] }, loading: false, loadingStates: { addingItem: false, removingItem: null, updatingItem: null, addingPaymentMethod: false, addingShippingMethod: false, addingShippingAddress: false, addingBillingAddress: false, addingContactInfo: false, applyingCoupon: false, removingCoupon: false, fetchingShippingMethods: false }, syncStatus: { syncing: false, synced: false, trigger: undefined } }; export const CartProvider = ({ children, query, cart, addMineCartItemApi }: CartProviderProps) => { const client = useClient(); // Get urql client for GraphQL queries const hydratedInitialState: Partial = { loading: initialEmptyState.loading, loadingStates: { ...initialEmptyState.loadingStates }, syncStatus: { ...initialEmptyState.syncStatus } }; if (cart) { hydratedInitialState.data = cart; } else { hydratedInitialState.data = { ...initialEmptyState.data, addItemApi: addMineCartItemApi }; } const [state, dispatch] = useReducer(cartReducer, hydratedInitialState); // The cart as last rendered, for the "before" of an event: some callbacks // below keep an older `state` in their closure const stateRef = React.useRef(state); stateRef.current = state; const retry = async function ( fn: () => Promise, retries = 2, delay = 1000 ): Promise { try { return await fn(); } catch (error) { if (retries > 0) { await new Promise((resolve) => setTimeout(resolve, delay)); return retry(fn, retries - 1, delay * 2); } throw error; } }; // Fetch the cart again and wait for it. This used `useQuery`'s re-run // function, which in urql 3 returns nothing: awaiting it waited for // nothing, so `synced` turned true, and every `await addItem()` (and the // other changes) resolved, before the new cart arrived. MiniCart opens on // `synced` after ADD_ITEM, so it showed the old contents, and a failed // fetch never reached the catch. The new cart and `synced` now land // together. const syncCartWithServer = useCallback( async (trigger?: string): Promise => { try { // Set syncing to true and synced to false when starting sync dispatch({ type: 'SET_SYNC_STATUS', payload: { syncing: true, synced: false, trigger } }); const result = await client .query(query, {}, { requestPolicy: 'network-only' }) .toPromise(); if (result.error) { throw new Error(result.error.message || 'Failed to sync cart'); } // No cart on the server (yet): an empty one const cart: CartData = result.data?.myCart ?? { ...initialEmptyState.data, addItemApi: addMineCartItemApi }; dispatch({ type: 'SET_CART', payload: cart }); // Set syncing to false and synced to true on success dispatch({ type: 'SET_SYNC_STATUS', payload: { syncing: false, synced: true, trigger } }); return cart; } catch (error) { dispatch({ type: 'SET_ERROR', payload: error instanceof Error ? error.message : 'Failed to sync cart' }); // Set syncing to false and keep synced as false on error dispatch({ type: 'SET_SYNC_STATUS', payload: { syncing: false, synced: false, trigger } }); return undefined; } }, [client, query, addMineCartItemApi] ); React.useEffect(() => { if (cart && JSON.stringify(cart) !== JSON.stringify(state.data)) { dispatch({ type: 'SET_CART', payload: cart }); } }, [cart]); const addItem = useCallback( async (payload: { sku: string; qty: number }) => { if (!state.data) { throw new Error('Cannot add item: cart not initialized'); } const linesBefore = stateRef.current.data?.items ?? []; try { // Set specific loading state dispatch({ type: 'SET_SPECIFIC_LOADING', payload: { operation: 'addingItem', loading: true } }); // Server request with retry const response = await retry(() => fetch(state.data!.addItemApi, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload) }) ); const json = (await response.json()) as ApiResponse<{ item?: { uuid?: string }; }>; if (!response.ok) { throw new Error(json.error?.message || 'Failed to add item.'); } // Sync with server (both immediate update and GraphQL refetch) const cart = await syncCartWithServer(CartSyncTrigger.ADD_ITEM); if (cart) { // The line the API added to: a new one, or one that had the product const addedUuid = 'data' in json ? json.data?.item?.uuid : undefined; const isAdded = (line: CartItem) => addedUuid ? line.uuid === addedUuid : line.productSku === payload.sku; const after = cart.items.find(isAdded); const before = linesBefore.find(isAdded); publishLineChange(before, after, cart); } } catch (error) { dispatch({ type: 'SET_ERROR', payload: error instanceof Error ? error.message : 'Failed to add item' }); throw error; } finally { dispatch({ type: 'SET_SPECIFIC_LOADING', payload: { operation: 'addingItem', loading: false } }); } }, [state.data?.addItemApi, syncCartWithServer] ); const removeItem = useCallback( async (itemId: string) => { if (!state.data) { throw new Error('Cannot remove item: cart not initialized'); } const item = state.data.items.find((item) => item.cartItemId === itemId); if (!item) { throw new Error('Item not found in cart'); } try { // Set specific loading state for this item dispatch({ type: 'SET_SPECIFIC_LOADING', payload: { operation: 'removingItem', loading: true, itemId } }); // Server request with retry using item's remove API const response = await retry(() => fetch(item.removeApi, { method: 'DELETE' }) ); const json = await response.json(); if (!response.ok) { throw new Error(json.error?.message || 'Failed to remove item.'); } // Sync with server (both immediate update and GraphQL refetch) const cart = await syncCartWithServer(CartSyncTrigger.REMOVE_ITEM); if (cart) { publishLineChange( item, cart.items.find((line) => line.uuid === item.uuid), cart ); } } catch (error) { dispatch({ type: 'SET_ERROR', payload: error instanceof Error ? error.message : 'Failed to remove item' }); throw error; } finally { dispatch({ type: 'SET_SPECIFIC_LOADING', payload: { operation: 'removingItem', loading: false } }); } }, [state, syncCartWithServer] ); const updateItem = useCallback( async ( itemId: string, payload: { qty: number; action: 'increase' | 'decrease' } ) => { if (!state.data) { throw new Error('Cannot update item: cart not initialized'); } const item = state.data.items.find((item) => item.cartItemId === itemId); if (!item) { throw new Error('Item not found in cart'); } try { // Set specific loading state for this item dispatch({ type: 'SET_SPECIFIC_LOADING', payload: { operation: 'updatingItem', loading: true, itemId } }); // Server request with retry using item's update API const response = await retry(() => fetch(item.updateQtyApi, { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload) }) ); const json = await response.json(); if (!response.ok) { throw new Error(json.error?.message || 'Failed to update item.'); } // Sync with server (both immediate update and GraphQL refetch) const cart = await syncCartWithServer(CartSyncTrigger.UPDATE_ITEM); if (cart) { // 2 → 5 is "added" with qty 3; 5 → 2 "removed" with qty 3 publishLineChange( item, cart.items.find((line) => line.uuid === item.uuid), cart ); } } catch (error) { dispatch({ type: 'SET_ERROR', payload: error instanceof Error ? error.message : 'Failed to update item' }); throw error; } finally { dispatch({ type: 'SET_SPECIFIC_LOADING', payload: { operation: 'updatingItem', loading: false } }); } }, [state, syncCartWithServer] ); // Clear error function const clearError = useCallback(() => { dispatch({ type: 'CLEAR_ERROR' }); }, []); // Add payment method const addPaymentMethod = useCallback( async (code: string, name: string) => { if (!state.data) { throw new Error(_('Cannot add payment method: cart not initialized')); } try { dispatch({ type: 'SET_SPECIFIC_LOADING', payload: { operation: 'addingPaymentMethod', loading: true } }); const response = await retry(() => fetch(state.data!.addPaymentMethodApi, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ method_code: code, method_name: name }) }) ); const json = await response.json(); if (!response.ok) { throw new Error( json.error?.message || _('Failed to add payment method.') ); } // Sync with server (both immediate update and GraphQL refetch) await syncCartWithServer(CartSyncTrigger.ADD_PAYMENT_METHOD); } catch (error) { dispatch({ type: 'SET_ERROR', payload: error instanceof Error ? error.message : _('Failed to add payment method') }); throw error; } finally { dispatch({ type: 'SET_SPECIFIC_LOADING', payload: { operation: 'addingPaymentMethod', loading: false } }); } }, [state.data?.addPaymentMethodApi, syncCartWithServer] ); // Add shipping method const addShippingMethod = useCallback( async (code: string, name: string, providerCode: string) => { if (!state.data) { throw new Error(_('Cannot add shipping method: cart not initialized')); } if (!providerCode) { // No more silent default to 'core' — the storefront must thread // providerCode from `availableShippingMethods.providerCode` // (server-side `String!`). Defaulting hid non-core mis-routes // behind a "method no longer available" error from resolveShippingQuote. throw new Error( _('Cannot add shipping method: providerCode is required') ); } try { dispatch({ type: 'SET_SPECIFIC_LOADING', payload: { operation: 'addingShippingMethod', loading: true } }); const response = await retry(() => fetch(state.data!.addShippingMethodApi, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ provider_code: providerCode, method_code: code, method_name: name }) }) ); const json = await response.json(); if (!response.ok) { throw new Error( json.error?.message || _('Failed to add shipping method.') ); } // Sync with server (both immediate update and GraphQL refetch) const cart = await syncCartWithServer( CartSyncTrigger.ADD_SHIPPING_METHOD ); if (cart) { publishSafely(() => dispatchCommerceEvent('checkout_shipping_info_submitted', { ...toCommerceCart(cart), shippingMethod: { code, name } }) ); } } catch (error) { dispatch({ type: 'SET_ERROR', payload: error instanceof Error ? error.message : _('Failed to add shipping method') }); throw error; } finally { dispatch({ type: 'SET_SPECIFIC_LOADING', payload: { operation: 'addingShippingMethod', loading: false } }); } }, [state.data?.addShippingMethodApi, syncCartWithServer] ); // Add shipping address const addShippingAddress = useCallback( async (address: Address) => { if (!state.data) { throw new Error(_('Cannot add shipping address: cart not initialized')); } try { dispatch({ type: 'SET_SPECIFIC_LOADING', payload: { operation: 'addingShippingAddress', loading: true } }); const response = await retry(() => fetch(state.data!.addAddressApi, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ address: { ...address }, type: 'shipping' }) }) ); const json = await response.json(); if (!response.ok) { // Spec § 3.8: `error.errors[]` carries field-targeted messages; keep // them on the thrown error so a form can highlight the inputs. const failure = new Error( json.error?.message || _('Failed to add shipping address.') ) as Error & { errors?: unknown }; failure.errors = json.error?.errors; throw failure; } // Sync with server (both immediate update and GraphQL refetch) await syncCartWithServer(CartSyncTrigger.ADD_SHIPPING_ADDRESS); } catch (error) { dispatch({ type: 'SET_ERROR', payload: error instanceof Error ? error.message : _('Failed to add shipping address') }); throw error; } finally { dispatch({ type: 'SET_SPECIFIC_LOADING', payload: { operation: 'addingShippingAddress', loading: false } }); } }, [state.data?.addAddressApi, syncCartWithServer] ); // Add billing address const addBillingAddress = useCallback( async (address: Address) => { if (!state.data) { throw new Error(_('Cannot add billing address: cart not initialized')); } try { dispatch({ type: 'SET_SPECIFIC_LOADING', payload: { operation: 'addingBillingAddress', loading: true } }); const response = await retry(() => fetch(state.data!.addAddressApi, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ address: { ...address }, type: 'billing' }) }) ); const json = await response.json(); if (!response.ok) { // Spec § 3.8: `error.errors[]` carries field-targeted messages; keep // them on the thrown error so a form can highlight the inputs. const failure = new Error( json.error?.message || _('Failed to add billing address.') ) as Error & { errors?: unknown }; failure.errors = json.error?.errors; throw failure; } // Sync with server (both immediate update and GraphQL refetch) await syncCartWithServer(CartSyncTrigger.ADD_BILLING_ADDRESS); } catch (error) { dispatch({ type: 'SET_ERROR', payload: error instanceof Error ? error.message : _('Failed to add billing address') }); throw error; } finally { dispatch({ type: 'SET_SPECIFIC_LOADING', payload: { operation: 'addingBillingAddress', loading: false } }); } }, [state.data?.addAddressApi, syncCartWithServer] ); // Add contact info const addContactInfo = useCallback( async (contactInfo: { email: string }) => { if (!state.data) { throw new Error(_('Cannot add contact info: cart not initialized')); } // The checkout saves the email in the background, as soon as a guest // enters it, and the email changes no amount. So no loading state (the // cart items and totals would flash as loading), no refetch, and no // SET_ERROR on a failure (the cart error shows in the coupon form): the // caller gets the error. const response = await retry(() => fetch(state.data!.addContactInfoApi, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(contactInfo) }) ); const json = await response.json(); if (!response.ok) { throw new Error(json.error?.message || _('Failed to add contact info.')); } // The cart keeps its data and gets the email the server saved const email: string = json.data?.email ?? contactInfo.email; dispatch({ type: 'SET_CUSTOMER_EMAIL', payload: email }); const cart = stateRef.current.data; if (cart) { publishSafely(() => dispatchCommerceEvent('checkout_contact_info_submitted', { ...toCommerceCart(cart), loggedIn: Boolean(cart.customerId) }) ); } }, [state.data?.addContactInfoApi] ); // Apply coupon const applyCoupon = useCallback( async (couponCode: string) => { if (!state.data) { throw new Error(_('Cannot apply coupon: cart not initialized')); } try { dispatch({ type: 'SET_SPECIFIC_LOADING', payload: { operation: 'applyingCoupon', loading: true } }); const response = await retry(() => fetch(state.data!.applyCouponApi, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ coupon: couponCode }) }) ); const json = await response.json(); if (!response.ok) { throw new Error(json.error?.message || 'Failed to apply coupon.'); } // Sync with server (both immediate update and GraphQL refetch) const cart = await syncCartWithServer(CartSyncTrigger.APPLY_COUPON); if (cart) { publishSafely(() => dispatchCommerceEvent('coupon_applied', { coupon: cart.coupon || couponCode, cart: toCommerceCart(cart) }) ); } } catch (error) { dispatch({ type: 'SET_ERROR', payload: error instanceof Error ? error.message : 'Failed to apply coupon' }); throw error; } finally { dispatch({ type: 'SET_SPECIFIC_LOADING', payload: { operation: 'applyingCoupon', loading: false } }); } }, [state.data?.applyCouponApi, syncCartWithServer] ); // Remove coupon const removeCoupon = useCallback(async () => { if (!state.data) { throw new Error(_('Cannot remove coupon: cart not initialized')); } if (!state.data?.removeCouponApi) { throw new Error(_('No coupon to remove')); } const removedCoupon = stateRef.current.data?.coupon || ''; try { dispatch({ type: 'SET_SPECIFIC_LOADING', payload: { operation: 'removingCoupon', loading: true } }); const response = await retry(() => fetch(state.data!.removeCouponApi as string, { method: 'DELETE', headers: { 'Content-Type': 'application/json' } }) ); const json = await response.json(); if (!response.ok) { throw new Error(json.error?.message || _('Failed to remove coupon.')); } // Sync with server (both immediate update and GraphQL refetch) const cart = await syncCartWithServer(CartSyncTrigger.REMOVE_COUPON); if (cart) { publishSafely(() => dispatchCommerceEvent('coupon_removed', { coupon: removedCoupon, cart: toCommerceCart(cart) }) ); } } catch (error) { dispatch({ type: 'SET_ERROR', payload: error instanceof Error ? error.message : _('Failed to remove coupon') }); throw error; } finally { dispatch({ type: 'SET_SPECIFIC_LOADING', payload: { operation: 'removingCoupon', loading: false } }); } }, [state.data?.removeCouponApi, syncCartWithServer]); // Check if shipping is required // Note: Currently assumes all items require shipping // If you need to support virtual/downloadable products, add a 'virtual' or 'requiresShipping' field to CartItem const isShippingRequired = useCallback(() => { if (!state.data) return false; // If there are items in the cart, shipping is required // This can be enhanced with a virtual/downloadable product check if needed return state.data.items.length > 0; }, [state.data?.items]); // Check if cart is ready for checkout const isReadyForCheckout = useCallback(() => { if (!state.data) return false; const hasItems = state.data.items.length > 0; const hasBillingAddress = !!state.data.billingAddress; const hasShippingAddress = !isShippingRequired() || !!state.data.shippingAddress; const noErrors = state.data.errors.length === 0; return hasItems && hasBillingAddress && hasShippingAddress && noErrors; }, [state.data, isShippingRequired]); // Get validation errors const getErrors = useCallback(() => { return state.data?.errors ?? []; }, [state.data?.errors]); // Get cart ID const getId = useCallback(() => { return state.data?.uuid ?? null; }, [state.data?.uuid]); // Fetch available shipping methods based on address parameters and update cart state const fetchAvailableShippingMethods = useCallback( async (params: ShippingAddressParams) => { if (!state.data?.uuid) { throw new Error('Cannot fetch shipping methods: cart not initialized'); } try { dispatch({ type: 'SET_SPECIFIC_LOADING', payload: { operation: 'fetchingShippingMethods', loading: true } }); const result = await client .query(ShippingMethodsQuery, { country: params.country, administrativeArea: params.administrativeArea || null, locality: params.locality || null, dependentLocality: params.dependentLocality || null, postalCode: params.postalCode || null }) .toPromise(); if (result.error) { throw new Error( result.error.message || 'Failed to fetch shipping methods' ); } // Update cart state with new shipping methods if (result.data?.myCart?.availableShippingMethods) { dispatch({ type: 'SET_CART', payload: { availableShippingMethods: result.data.myCart.availableShippingMethods } }); } } catch (error) { const errorMessage = error instanceof Error ? error.message : 'Failed to fetch shipping methods'; dispatch({ type: 'SET_ERROR', payload: errorMessage }); throw new Error(errorMessage); } finally { dispatch({ type: 'SET_SPECIFIC_LOADING', payload: { operation: 'fetchingShippingMethods', loading: false } }); } }, [state.data?.uuid, client] ); const cartDispatch: CartDispatch = { addItem, removeItem, updateItem, addPaymentMethod, addShippingMethod, addShippingAddress, addBillingAddress, addContactInfo, applyCoupon, removeCoupon, clearError, isShippingRequired, isReadyForCheckout, getErrors, getId, fetchAvailableShippingMethods, syncCartWithServer }; return ( {children} ); }; export const useCartState = (): CartState => { const context = useContext(CartStateContext); if (!context) { throw new Error('useCartState must be used within a CartProvider'); } return context; }; /** * Non-throwing variant of useCartState for components that may render both * inside and outside a CartProvider — e.g. widgets, which merchants can * place into ANY area, including ones above the provider. Returns undefined * when no provider is present; such placements simply stay non-reactive. */ export const useOptionalCartState = (): CartState | undefined => useContext(CartStateContext); export const useCartDispatch = (): CartDispatch => { const context = useContext(CartDispatchContext); if (!context) { throw new Error('useCartDispatch must be used within a CartProvider'); } return context; };