All files / src/api/inventory/hooks useSuppliersQuery.ts

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

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 561x 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 2x 2x 2x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x  
/**
 * @file useSuppliersQuery.ts
 * @module api/inventory/hooks
 *
 * @summary
 * Hook to load suppliers for dropdown/autocomplete selection.
 * Provides normalized supplier options with consistent caching strategy.
 *
 * @enterprise
 * - 5-minute cache reduces API calls and improves UX
 * - Conditional enabling for performance optimization
 * - Normalized to SupplierOption shape for UI consistency
 * - Graceful error handling with React Query
 */
 
import { useQuery } from '@tanstack/react-query';
import { getSuppliersLite } from '../../analytics/suppliers';
import type { SupplierOption } from '../../analytics/types';
 
/**
 * Hook to load suppliers for dropdown/autocomplete selection.
 * Caches results for 5 minutes to reduce API calls.
 *
 * @param enabled - Whether to fetch suppliers (typically tied to dialog open state)
 * @returns React Query result with supplier options
 *
 * @enterprise
 * - Only fetches when enabled (performance optimization)
 * - 5-minute cache reduces backend load
 * - Maps backend data to normalized SupplierOption shape
 * - Handles loading and error states automatically
 *
 * @example
 * ```typescript
 * const { data: suppliers, isLoading } = useSuppliersQuery(dialogOpen);
 *
 * <Select disabled={isLoading}>
 *   {suppliers?.map(s => <MenuItem key={s.id} value={s.id}>{s.name}</MenuItem>)}
 * </Select>
 * ```
 */
export function useSuppliersQuery(enabled: boolean) {
  return useQuery({
    queryKey: ['suppliers', 'lite'],
    queryFn: async () => {
      const suppliers = await getSuppliersLite();
      return suppliers.map((supplier): SupplierOption => ({
        id: supplier.id,
        label: supplier.name,
      }));
    },
    enabled,
    staleTime: 5 * 60 * 1000, // 5 minutes cache
  });
}