first commit
This commit is contained in:
45
client/src/hooks/useCats.ts
Normal file
45
client/src/hooks/useCats.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
import { getCats, getUserCats, getCat, uploadCat, deleteCat } from '@/api/endpoints';
|
||||
|
||||
export function useCats(page: number) {
|
||||
return useQuery({
|
||||
queryKey: ['cats', page],
|
||||
queryFn: () => getCats(page).then((r) => r.data),
|
||||
});
|
||||
}
|
||||
|
||||
export function useUserCats(userId: number) {
|
||||
return useQuery({
|
||||
queryKey: ['cats', 'user', userId],
|
||||
queryFn: () => getUserCats(userId).then((r) => r.data),
|
||||
enabled: !!userId,
|
||||
});
|
||||
}
|
||||
|
||||
export function useCat(id: number) {
|
||||
return useQuery({
|
||||
queryKey: ['cat', id],
|
||||
queryFn: () => getCat(id).then((r) => r.data),
|
||||
enabled: !!id,
|
||||
});
|
||||
}
|
||||
|
||||
export function useUploadCat() {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: (formData: FormData) => uploadCat(formData).then((r) => r.data),
|
||||
onSuccess: () => {
|
||||
qc.invalidateQueries({ queryKey: ['cats'] });
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useDeleteCat() {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: (id: number) => deleteCat(id),
|
||||
onSuccess: () => {
|
||||
qc.invalidateQueries({ queryKey: ['cats'] });
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user