37 lines
1.9 KiB
TypeScript
37 lines
1.9 KiB
TypeScript
'use client'
|
|
import { z } from 'zod'
|
|
import { useMutation } from '@tanstack/react-query'
|
|
import { toISOIfNotZ } from '@/lib/utils'
|
|
|
|
export const formSchema = z.object({
|
|
userTypeID: z.string({ error: "User Type ID is required" }),
|
|
partID: z.string({ error: "Part ID is required" }),
|
|
companyID: z.string({ error: "Company ID is required" }),
|
|
personID: z.string({ error: "Person ID is required" }),
|
|
expiryStarts: z.string().optional(),
|
|
expiryEnds: z.string().optional(),
|
|
});
|
|
|
|
export type schemaType = z.infer<typeof formSchema>;
|
|
|
|
const fetchGraphQlLivingSpaceUpdate = async (record: schemaType, uuid: string, buildID: string): Promise<{ data: schemaType | 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;
|
|
try {
|
|
const res = await fetch(`/api/living-space/update?uuid=${uuid || ''}&buildID=${buildID || ''}`, { 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();
|
|
return { data: data.data, status: res.status }
|
|
} catch (error) { console.error('Error fetching test data:', error); throw error }
|
|
};
|
|
|
|
|
|
export function useUpdateLivingSpaceMutation() {
|
|
return useMutation({
|
|
mutationFn: ({ data, uuid, buildID }: { data: schemaType, uuid: string, buildID: string }) => fetchGraphQlLivingSpaceUpdate(data, uuid, buildID),
|
|
onSuccess: () => { console.log("Living Space updated successfully") },
|
|
onError: (error) => { console.error("Update Living Space update failed:", error) },
|
|
})
|
|
}
|