All files / src/hooks/useThanks useThanks.ts

6.25% Statements 1/16
100% Branches 0/0
0% Functions 0/12
6.25% Lines 1/16

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                                5x                                                                                                                                    
/**
 * useThanks Hook (Story 7.4 — "Thank the Organizers")
 *
 * React Query hooks for the public thank-the-organizers count + submit.
 */
 
import {
  useMutation,
  useQuery,
  useQueryClient,
  type UseMutationResult,
  type UseQueryResult,
} from '@tanstack/react-query';
import * as thanksService from '@/services/thanksService';
import type { ThanksCountResponse, ThanksNoteResponse } from '@/services/thanksService';
 
export const THANKS_QUERY_KEYS = {
  count: (eventCode: string) => ['thanks', eventCode] as const,
  /** Organizer notes view (Story 7.7) — same endpoint, returns notes[] with id + featured state. */
  admin: (eventCode: string) => ['thanks', 'admin', eventCode] as const,
};
 
export interface SubmitThanksVars {
  note?: string | null;
  turnstileToken?: string | null;
}
 
export interface SetFeaturedVars {
  id: string;
  featured: boolean;
}
 
/** Public aggregate count for an event. */
export function useThanksCount(eventCode: string): UseQueryResult<ThanksCountResponse, Error> {
  return useQuery({
    queryKey: THANKS_QUERY_KEYS.count(eventCode),
    queryFn: () => thanksService.getThanks(eventCode),
    enabled: !!eventCode,
    staleTime: 60 * 1000,
  });
}
 
/** Submit a thank-you. On success the count query is updated with the returned aggregate. */
export function useSubmitThanks(
  eventCode: string
): UseMutationResult<ThanksCountResponse, Error, SubmitThanksVars> {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: ({ note, turnstileToken }) =>
      thanksService.submitThanks(eventCode, note, turnstileToken),
    onSuccess: (data) => {
      queryClient.setQueryData(THANKS_QUERY_KEYS.count(eventCode), data);
    },
  });
}
 
/**
 * Organizer notes view (Story 7.7). Same GET as the public count, but an organizer caller also
 * receives notes[] (with id + featured state). Used by the Appreciation panel.
 */
export function useEventThanks(eventCode: string): UseQueryResult<ThanksCountResponse, Error> {
  return useQuery({
    queryKey: THANKS_QUERY_KEYS.admin(eventCode),
    queryFn: () => thanksService.getThanks(eventCode),
    enabled: !!eventCode,
    staleTime: 30 * 1000,
  });
}
 
/** Organizer feature-toggle (Story 7.7). Refreshes the notes list + the public marquee pool. */
export function useSetThanksFeatured(
  eventCode: string
): UseMutationResult<ThanksNoteResponse, Error, SetFeaturedVars> {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: ({ id, featured }) => thanksService.setThanksFeatured(eventCode, id, featured),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: THANKS_QUERY_KEYS.admin(eventCode) });
      queryClient.invalidateQueries({ queryKey: ['thanks', 'featured'] });
    },
  });
}