All files / src/hooks usePartnerNotes.ts

100% Statements 15/15
100% Branches 0/0
100% Functions 8/8
100% Lines 15/15

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 88                                                                                          5x 13x 13x   13x   8x         13x 1x   1x       13x 1x     1x       13x 1x   1x       13x                  
/**
 * usePartnerNotes Hook
 *
 * React Query hook for fetching and mutating partner notes.
 * Story 8.4: wired to real backend (replaces stub from Story 2.8.2).
 *
 * Features:
 * - Fetches partner notes for Notes tab (ORGANIZER-only)
 * - 2-minute stale time (notes data is volatile)
 * - Lazy loading (only fetches when companyName is defined)
 * - Mutations: create, update, delete notes with cache invalidation
 *
 * AC: 6 (frontend wired), 7 (ORGANIZER-only)
 */
 
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import {
  getPartnerNotes,
  createPartnerNote,
  updatePartnerNote,
  deletePartnerNote,
} from '@/services/api/partnerNotesApi';
import type {
  PartnerNoteDTO,
  CreateNoteRequest,
  UpdateNoteRequest,
} from '@/services/api/partnerNotesApi';
 
interface UsePartnerNotesReturn {
  data: PartnerNoteDTO[] | undefined;
  isLoading: boolean;
  error: Error | null;
  createNote: (note: CreateNoteRequest) => void;
  updateNote: (params: { noteId: string } & UpdateNoteRequest) => void;
  deleteNote: (noteId: string) => void;
}
 
/**
 * usePartnerNotes - Fetch and mutate partner notes
 *
 * @param companyName - Company name (meaningful ID per ADR-003)
 * @returns Object with notes data and mutation functions
 *
 * Cache: 2 minutes stale time
 */
export const usePartnerNotes = (companyName: string): UsePartnerNotesReturn => {
  const queryClient = useQueryClient();
  const queryKey = ['partner', companyName, 'notes'];
 
  const { data, isLoading, error } = useQuery({
    queryKey,
    queryFn: () => getPartnerNotes(companyName),
    enabled: !!companyName,
    staleTime: 2 * 60 * 1000, // 2 minutes
  });
 
  const createMutation = useMutation({
    mutationFn: (note: CreateNoteRequest) => createPartnerNote(companyName, note),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey });
    },
  });
 
  const updateMutation = useMutation({
    mutationFn: async ({ noteId, ...req }: { noteId: string } & UpdateNoteRequest) =>
      updatePartnerNote(companyName, noteId, req),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey });
    },
  });
 
  const deleteMutation = useMutation({
    mutationFn: (noteId: string) => deletePartnerNote(companyName, noteId),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey });
    },
  });
 
  return {
    data,
    isLoading,
    error: error as Error | null,
    createNote: createMutation.mutate,
    updateNote: updateMutation.mutate,
    deleteNote: deleteMutation.mutate,
  };
};