All files / src/hooks useDeregistration.ts

100% Statements 8/8
100% Branches 2/2
100% Functions 6/6
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                                        5x   2x               4x 4x     2x 1x                       8x 1x      
/**
 * useDeregistration Hook (Story 10.12)
 *
 * React Query hooks for self-service deregistration.
 */
 
import {
  useMutation,
  useQuery,
  useQueryClient,
  type UseMutationResult,
  type UseQueryResult,
} from '@tanstack/react-query';
import * as deregistrationService from '@/services/deregistrationService';
import type { DeregistrationVerifyResponse } from '@/services/deregistrationService';
 
/** Verify a deregistration token. Enabled only when token is non-null. */
export function useVerifyDeregistrationToken(
  token: string | null
): UseQueryResult<DeregistrationVerifyResponse, Error> {
  return useQuery({
    queryKey: ['deregistration', 'verify-token', token],
    queryFn: () => deregistrationService.verifyDeregistrationToken(token!),
    enabled: !!token,
    retry: false,
  });
}
 
/** Cancel registration by deregistration token. Invalidates my-registration cache on success. */
export function useDeregisterByToken(eventCode?: string): UseMutationResult<void, Error, string> {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: deregistrationService.deregisterByToken,
    onSuccess: () => {
      if (eventCode) {
        queryClient.invalidateQueries({ queryKey: ['my-registration', eventCode] });
      }
    },
  });
}
 
/** Request deregistration link by email. Always shows success (anti-enumeration). */
export function useDeregistrationByEmail(): UseMutationResult<
  void,
  Error,
  { email: string; eventCode: string }
> {
  return useMutation({
    mutationFn: ({ email, eventCode }) => deregistrationService.deregisterByEmail(email, eventCode),
  });
}