All files / src/providers MasterItemsProvider.tsx

100% Statements 5/5
100% Branches 3/3
100% Functions 2/2
100% Lines 5/5

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                          38x         320x   320x 320x               320x    
// src/providers/MasterItemsProvider.tsx
import React, { ReactNode, useMemo } from 'react';
import { MasterItemsContext } from '../contexts/MasterItemsContext';
import { useMasterItemsQuery } from '../hooks/queries/useMasterItemsQuery';
 
/**
 * Provider for master grocery items using TanStack Query (ADR-0005).
 *
 * This replaces the previous custom useApiOnMount implementation with
 * TanStack Query for better caching, automatic refetching, and state management.
 *
 * Master items are cached longer (10 minutes) since they change infrequently.
 */
export const MasterItemsProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
  const {
    data: masterItems = [],
    isLoading,
    error,
  } = useMasterItemsQuery();
 
  const value = useMemo(
    () => ({
      masterItems,
      isLoading,
      error: error?.message || null,
    }),
    [masterItems, isLoading, error]
  );
 
  return <MasterItemsContext.Provider value={value}>{children}</MasterItemsContext.Provider>;
};