evyos-frontend-development/frontend/pages/living-space/add/queries.tsx

59 lines
3.1 KiB
TypeScript

'use client'
import { z } from 'zod'
import { useMutation } from '@tanstack/react-query'
import { toISOIfNotZ } from '@/lib/utils'
export const formSchema = z.object({
buildID: z.string({ error: "Build ID is required" }),
collectionToken: z.string({ error: "Collection Token is required" }),
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 fetchGraphQlLivingSpaceAdd = async (record: schemaType): 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;
console.dir({ record })
try {
const res = await fetch('/api/living-spaces/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();
return { data: data.data, status: res.status }
} catch (error) { console.error('Error fetching test data:', error); throw error }
};
const fetchGraphQlLivingSpaceUpdate = async (record: schemaType, uuid: 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-spaces/update?uuid=${uuid || ''}`, { 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 useAddLivingSpaceMutation() {
return useMutation({
mutationFn: ({ data }: { data: schemaType }) => fetchGraphQlLivingSpaceAdd(data),
onSuccess: () => { console.log("Living Space added successfully") },
onError: (error) => { console.error("Add Living Space add failed:", error) },
})
}
export function useUpdateLivingSpaceMutation() {
return useMutation({
mutationFn: ({ data, uuid }: { data: schemaType, uuid: string }) => fetchGraphQlLivingSpaceUpdate(data, uuid),
onSuccess: () => { console.log("Living Space updated successfully") },
onError: (error) => { console.error("Update Living Space update failed:", error) },
})
}