import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' import { pipelineApi } from './pipelineApi' export function usePipelineBoard(reqId: string | null) { return useQuery({ queryKey: ['recruitmatch', 'pipeline', reqId], queryFn: () => pipelineApi.board(reqId as string), enabled: !!reqId, }) } export function useApplication(id: string | null) { return useQuery({ queryKey: ['recruitmatch', 'application', id], queryFn: () => pipelineApi.get(id as string), enabled: !!id, }) } // The reason vocabulary is served rather than hardcoded, so the form cannot offer a code the database // would refuse. It never changes within a session, so it is cached indefinitely. export function useDispositions() { return useQuery({ queryKey: ['recruitmatch', 'dispositions'], queryFn: () => pipelineApi.dispositions(), staleTime: Infinity, }) } // A stage move changes the board, the application, the job order's seat count and the dashboard, so all // four are invalidated. Leaving any stale shows a seat as open seconds after it was filled. function useInvalidate(reqId?: string) { const qc = useQueryClient() return (appId?: string) => { void qc.invalidateQueries({ queryKey: ['recruitmatch', 'pipeline', reqId] }) void qc.invalidateQueries({ queryKey: ['recruitmatch', 'requisitions'] }) void qc.invalidateQueries({ queryKey: ['recruitmatch', 'dashboard'] }) if (appId) void qc.invalidateQueries({ queryKey: ['recruitmatch', 'application', appId] }) } } export function useSubmitCandidate(reqId: string) { const inv = useInvalidate(reqId) return useMutation({ mutationFn: (v: { candidate_id: string; stage?: string; source_touch?: string }) => pipelineApi.submit(reqId, v), onSuccess: () => inv(), }) } export function useMoveStage(reqId?: string) { const inv = useInvalidate(reqId) return useMutation({ mutationFn: (v: { id: string; expected_stage: string; to_stage: string; reason?: string; disposition_code?: string }) => pipelineApi.move(v.id, { expected_stage: v.expected_stage, to_stage: v.to_stage, reason: v.reason, disposition_code: v.disposition_code, }), onSuccess: (_d, v) => inv(v.id), }) }