All files / src/hooks useEventTypes.ts

100% Statements 10/10
100% Branches 2/2
100% Functions 8/8
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87                                                5x   2x                     66x   35x                           3x   1x                             5x   5x             3x     2x        
/**
 * Event Types React Query Hooks (Story 5.1 - Task 3b)
 *
 * Custom hooks for event type data fetching and mutations
 * Features:
 * - React Query for server state caching
 * - Generated types from OpenAPI spec (ADR-006)
 * - Automatic cache invalidation on updates
 * - 1 hour stale time (event types rarely change)
 */
 
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { eventTypeService } from '@/services/eventTypeService';
import type { components } from '@/types/generated/events-core-api.types';
 
// Import generated types from OpenAPI spec (ADR-006 compliance)
type EventType = components['schemas']['EventType'];
type UpdateEventSlotConfigurationRequest =
  components['schemas']['UpdateEventSlotConfigurationRequest'];
 
/**
 * Query key factory for event types
 * Centralizes query key management for cache invalidation
 */
export const eventTypeKeys = {
  all: ['eventTypes'] as const,
  detail: (type: EventType) => ['eventTypes', type] as const,
};
 
/**
 * Hook to fetch all event type configurations
 *
 * @returns Query result with array of event type configurations
 * @example
 * const { data: eventTypes, isLoading } = useEventTypes();
 */
export function useEventTypes() {
  return useQuery({
    queryKey: eventTypeKeys.all,
    queryFn: () => eventTypeService.getAllEventTypes(),
    staleTime: 1000 * 60 * 60, // 1 hour - event types rarely change
  });
}
 
/**
 * Hook to fetch specific event type configuration
 *
 * @param type - Event type enum value (FULL_DAY, AFTERNOON, EVENING)
 * @returns Query result with event type configuration
 * @example
 * const { data: config } = useEventType('FULL_DAY');
 */
export function useEventType(type?: EventType) {
  return useQuery({
    queryKey: type ? eventTypeKeys.detail(type) : (['eventTypes', 'none'] as const),
    queryFn: () => eventTypeService.getEventType(type!),
    enabled: !!type,
    staleTime: 1000 * 60 * 60, // 1 hour
  });
}
 
/**
 * Hook to update event type configuration (ORGANIZER only)
 *
 * @returns Mutation object with mutate function
 * @example
 * const updateMutation = useUpdateEventType();
 * updateMutation.mutate({ type: 'FULL_DAY', config: {...} });
 */
export function useUpdateEventType() {
  const queryClient = useQueryClient();
 
  return useMutation({
    mutationFn: ({
      type,
      config,
    }: {
      type: EventType;
      config: UpdateEventSlotConfigurationRequest;
    }) => eventTypeService.updateEventType(type, config),
    onSuccess: () => {
      // Invalidate all event type queries to refetch fresh data
      queryClient.invalidateQueries({ queryKey: eventTypeKeys.all });
    },
  });
}