28 lines
1.9 KiB
TypeScript
28 lines
1.9 KiB
TypeScript
'use client'
|
|
import { useMutation } from '@tanstack/react-query'
|
|
import { UserUpdate } from './schema';
|
|
import { toISOIfNotZ } from '@/lib/utils';
|
|
|
|
const fetchGraphQlUsersUpdate = async (record: UserUpdate, uuid: string, selectedBuildIDS: string[], selectedCompanyIDS: string[], defaultSelection: string, personID: string, refetchTable: () => void): Promise<{ data: UserUpdate | null; status: number }> => {
|
|
console.log('Fetching test data from local API');
|
|
record.expiryStarts = record?.expiryStarts ? toISOIfNotZ(record.expiryStarts) : undefined;
|
|
record.expiryEnds = record?.expiryEnds ? toISOIfNotZ(record.expiryEnds) : undefined;
|
|
const payload = { ...record, person: personID, collectionTokens: { defaultSelection, selectedBuildIDS, selectedCompanyIDS } }
|
|
try {
|
|
const res = await fetch(`/api/users/update?uuid=${uuid || ''}`, { method: 'POST', cache: 'no-store', credentials: "include", body: JSON.stringify(payload) });
|
|
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 useUpdateUserMutation() {
|
|
return useMutation({
|
|
mutationFn: (
|
|
{ data, uuid, selectedBuildIDS, selectedCompanyIDS, defaultSelection, personID, refetchTable }: { data: UserUpdate, uuid: string, selectedBuildIDS: string[], selectedCompanyIDS: string[], defaultSelection: string, personID: string, refetchTable: () => void }
|
|
) => fetchGraphQlUsersUpdate(data, uuid, selectedBuildIDS, selectedCompanyIDS, defaultSelection, personID, refetchTable),
|
|
onSuccess: () => { console.log("User updated successfully") },
|
|
onError: (error) => { console.error("Update user failed:", error) },
|
|
})
|
|
}
|