All files / src/hooks useMyRegistration.ts

100% Statements 9/9
100% Branches 6/6
100% Functions 3/3
100% Lines 9/9

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                                                                2x 20x   20x   11x         3x 3x         20x 2x     18x          
/**
 * useMyRegistration Hook
 * Story 10.10: Registration Status Indicator for Logged-in Users (T6, AC1, AC7, AC8)
 *
 * Fetches the authenticated user's registration status for a specific event.
 * - AC7: staleTime 5 minutes; invalidated on successful registration creation
 * - AC8: Returns undefined immediately for unauthenticated users (no API call)
 */
 
import { useQuery } from '@tanstack/react-query';
import { useAuth } from '@/hooks/useAuth';
import { getMyRegistration, type MyRegistrationResponse } from '@/services/registrationService';
 
export type { MyRegistrationResponse };
 
export interface UseMyRegistrationResult {
  data: MyRegistrationResponse | null | undefined;
  isLoading: boolean;
}
 
/**
 * React Query hook for the authenticated user's registration status.
 *
 * Query key: ['my-registration', eventCode]
 * staleTime: 5 minutes (AC7)
 *
 * @param eventCode Event code to check (undefined → no call made)
 * @returns { data, isLoading }
 *   - data: MyRegistrationResponse if registered
 *   - data: null if authenticated but not registered (404)
 *   - data: undefined if not authenticated or eventCode is missing
 */
export const useMyRegistration = (eventCode: string | undefined): UseMyRegistrationResult => {
  const { isAuthenticated } = useAuth();
 
  const query = useQuery<MyRegistrationResponse | null, Error>({
    queryKey: ['my-registration', eventCode],
    queryFn: () => getMyRegistration(eventCode!),
    enabled: !!eventCode && isAuthenticated,
    staleTime: 5 * 60 * 1000, // 5 minutes (AC7)
    // Don't retry 404s (not registered), but allow retry on transient errors (5xx, network)
    retry: (_failureCount, error: unknown) => {
      const status = (error as { response?: { status?: number } })?.response?.status;
      return status !== 404;
    },
  });
 
  // AC8: Return undefined immediately for unauthenticated users
  if (!isAuthenticated || !eventCode) {
    return { data: undefined, isLoading: false };
  }
 
  return {
    data: query.data,
    isLoading: query.isLoading,
  };
};