evyos-frontend-development/frontend/pages/people/add/queries.tsx

26 lines
1.4 KiB
TypeScript

'use client'
import { useMutation } from '@tanstack/react-query'
import { PeopleAdd } from './types'
import { toISOIfNotZ } from '@/lib/utils';
const fetchGraphQlPeopleAdd = async (record: PeopleAdd, refetchTable: () => void): Promise<{ data: PeopleAdd | null; status: number }> => {
console.log('Fetching test data from local API');
record.birthDate = toISOIfNotZ(record.birthDate || "");
record.expiryStarts = toISOIfNotZ(record.expiryStarts || "");
record.expiryEnds = toISOIfNotZ(record.expiryEnds || "");
try {
const res = await fetch('/api/people/add', { method: 'POST', cache: 'no-store', credentials: "include", body: JSON.stringify(record) });
if (!res.ok) { const errorText = await res.text(); console.error('Test data API error:', errorText); throw new Error(`API error: ${res.status} ${res.statusText}`) }
const data = await res.json(); refetchTable();
return { data: data.data, status: res.status }
} catch (error) { console.error('Error fetching test data:', error); throw error }
};
export function useAddPeopleMutation() {
return useMutation({
mutationFn: ({ data, refetchTable }: { data: PeopleAdd, refetchTable: () => void }) => fetchGraphQlPeopleAdd(data, refetchTable),
onSuccess: () => { console.log("People created successfully") },
onError: (error) => { console.error("Create people failed:", error) },
})
}