All files / src/hooks/useEventManagement useEventRegistrations.ts

100% Statements 8/8
100% Branches 7/7
100% Functions 2/2
100% Lines 8/8

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 60 61 62 63 64                                                                  3x                 24x     24x 7x       24x 4x     24x   15x              
/**
 * useEventRegistrations Hook
 *
 * React Query hook for fetching event registrations (participants) with filters
 * and pagination.
 *
 * Story 3.3: Event Participants Tab
 */
 
import { useQuery, keepPreviousData } from '@tanstack/react-query';
import { getEventRegistrations } from '@/services/api/eventRegistrationService';
import type { ParticipantFilters, ParticipantPagination } from '@/types/eventParticipant.types';
 
export interface UseEventRegistrationsOptions {
  eventCode: string;
  filters?: ParticipantFilters;
  pagination?: ParticipantPagination;
  search?: string;
  enabled?: boolean;
}
 
/**
 * Fetch event registrations with React Query
 *
 * Features:
 * - Automatic caching (5 min stale time)
 * - Keep previous data during refetch (prevents flickering)
 * - Configurable enabled flag
 * - Automatic refetching on filter/pagination changes
 *
 * @param options - Event code, filters, pagination, search, and enabled flag
 * @returns React Query result with registrations data
 */
export const useEventRegistrations = ({
  eventCode,
  filters,
  pagination,
  search,
  enabled = true,
}: UseEventRegistrationsOptions) => {
  // Build query key that includes all parameters affecting the API call
  // Explicitly type as array that can contain strings and objects
  const queryKey: (string | Record<string, unknown>)[] = ['event-registrations', eventCode];
 
  // Include filters and pagination in query key if provided
  if (filters || pagination) {
    queryKey.push({ filters, pagination });
  }
 
  // Include search in query key if provided
  if (search) {
    queryKey.push({ search });
  }
 
  return useQuery({
    queryKey,
    queryFn: () => getEventRegistrations(eventCode, { filters, pagination, search }),
    staleTime: 5 * 60 * 1000, // 5 minutes
    gcTime: 30 * 60 * 1000, // 30 minutes (formerly cacheTime)
    placeholderData: keepPreviousData, // Keep previous data during refetch
    enabled,
  });
};