All files / src/pages/suppliers/handlers useDataFetchingLogic.ts

100% Statements 56/56
100% Branches 4/4
100% Functions 1/1
100% Lines 56/56

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 571x 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 4x 4x 4x 4x 4x 4x 2x 2x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x  
/**
 * @file useDataFetchingLogic.ts
 * @module pages/suppliers/handlers/useDataFetchingLogic
 *
 * @summary
 * Custom hook that prepares data fetching parameters from board state.
 * Transforms UI state into server-compatible query parameters.
 *
 * @enterprise
 * - Separation of concerns: data parameter transformation isolated
 * - Single responsibility: converts state to API query format
 * - No memoization needed - simple calculations are cheap
 */
 
import { useSuppliersBoardData } from '../hooks/useSuppliersBoardData';
import type { UseSuppliersBoardStateReturn } from '../hooks/useSuppliersBoardState';
 
/**
 * Hook that prepares and executes data fetching with transformed state parameters.
 *
 * Responsibilities:
 * - Convert 0-based pagination to 1-based for server
 * - Format sort model into server query string
 * - Execute useSuppliersBoardData with prepared parameters
 *
 * @param state - Suppliers board state object
 * @returns Data fetching result from useSuppliersBoardData
 *
 * @example
 * ```tsx
 * const data = useDataFetchingLogic(state);
 * ```
 */
export function useDataFetchingLogic(state: UseSuppliersBoardStateReturn) {
  console.log('[DATA FETCHING LOGIC] called with pagination:', state.paginationModel);
  
  // Prepare parameters for data hook (no memoization needed)
  const serverPage = state.paginationModel.page + 1;
 
  const serverSort = state.sortModel.length
    ? `${state.sortModel[0].field},${state.sortModel[0].sort ?? 'asc'}`
    : 'name,asc';
 
  // Always pass the search query to the data hook for search results dropdown
  // The data hook will use it appropriately for both search and pagination
  const data = useSuppliersBoardData(
    serverPage,
    state.paginationModel.pageSize,
    serverSort,
    state.searchQuery,
    state.showAllSuppliers  // Pass this flag so the hook knows whether to filter paginated results
  );
 
  console.log('[DATA FETCHING LOGIC] returning data, suppliers count:', data.suppliers.length);
  return data;
}