40 lines
1.9 KiB
TypeScript
40 lines
1.9 KiB
TypeScript
'use client'
|
|
import { useQuery, useMutation } from '@tanstack/react-query'
|
|
import { UsersListResponse } from './types'
|
|
import { ListArguments } from '@/types/listRequest'
|
|
|
|
const fetchGraphQlUsersList = async (params: ListArguments): Promise<UsersListResponse> => {
|
|
console.log('Fetching test data from local API');
|
|
const { limit, skip, sort, filters } = params;
|
|
try {
|
|
const res = await fetch('/api/users/list', { method: 'POST', cache: 'no-store', credentials: "include", body: JSON.stringify({ limit, skip, sort, filters }) });
|
|
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, totalCount: data.totalCount }
|
|
} catch (error) { console.error('Error fetching test data:', error); throw error }
|
|
};
|
|
|
|
const fetchGraphQlDeleteUser = async (uuid: string): Promise<boolean> => {
|
|
console.log('Fetching test data from local API');
|
|
try {
|
|
const res = await fetch(`/api/users/delete?uuid=${uuid}`, { method: 'GET', cache: 'no-store', credentials: "include" });
|
|
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
|
|
} catch (error) { console.error('Error fetching test data:', error); throw error }
|
|
};
|
|
|
|
export function useGraphQlUsersList(params: ListArguments) {
|
|
return useQuery({ queryKey: ['graphql-users-list', params], queryFn: () => fetchGraphQlUsersList(params) })
|
|
}
|
|
|
|
export function useDeleteUserMutation() {
|
|
return useMutation({
|
|
mutationFn: ({ uuid }: { uuid: string }) => fetchGraphQlDeleteUser(uuid),
|
|
onSuccess: () => { console.log("User deleted successfully") },
|
|
onError: (error) => { console.error("Delete user failed:", error) },
|
|
})
|
|
}
|
|
|
|
|