All files / src/hooks useInfiniteEvents.ts

100% Statements 10/10
100% Branches 9/9
100% Functions 4/4
100% Lines 10/10

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                                150x             150x 17x     150x                       150x   150x     72x       66x     228x 228x            
/**
 * useInfiniteEvents Hook (Story 4.2 - Task 2b)
 *
 * React Query infinite scroll hook for archive browsing
 * Handles pagination, filtering, sorting, and caching
 */
 
import { useInfiniteQuery } from '@tanstack/react-query';
import { eventApiClient } from '@/services/eventApiClient';
import type { ArchiveFilters, EventFilters } from '@/types/event.types';
 
/**
 * Convert ArchiveFilters to EventFilters for API
 * Story BAT-109: Archive browsing filter conversion
 */
function convertToEventFilters(archiveFilters: ArchiveFilters): EventFilters {
  const eventFilters: EventFilters = {
    includeArchived: true, // Archive page shows archived events
    workflowState: ['ARCHIVED'], // Only show archived events
    search: archiveFilters.search,
  };
 
  // Topic filter - filter by topicCode
  if (archiveFilters.topics && archiveFilters.topics.length > 0) {
    eventFilters.topicCode = archiveFilters.topics;
  }
 
  return eventFilters;
}
 
/**
 * Infinite scroll hook for events archive
 *
 * @param filters - Archive filters (time period, topics, search)
 * @param sort - Sort parameter (e.g., '-date' for newest first)
 * @returns React Query infinite query result with pagination controls
 */
export function useInfiniteEvents(filters: ArchiveFilters = {}, sort: string = '-date') {
  // Convert ArchiveFilters to EventFilters for API
  const eventFilters = convertToEventFilters(filters);
 
  return useInfiniteQuery({
    queryKey: ['events', 'archive', filters, sort],
    queryFn: async ({ pageParam = 1 }) => {
      const result = await eventApiClient.getEvents({ page: pageParam, limit: 20 }, eventFilters, {
        expand: ['topics', 'sessions', 'speakers'],
        sort,
      });
      return result;
    },
    getNextPageParam: (lastPage) => {
      const { hasNext, page } = lastPage.pagination;
      return hasNext ? page + 1 : undefined;
    },
    initialPageParam: 1,
    staleTime: 5 * 60 * 1000, // 5 minutes - cache results to reduce API calls
  });
}