add useBlock hook
This commit is contained in:
33
src/utils/hooks/useBlock.tsx
Normal file
33
src/utils/hooks/useBlock.tsx
Normal file
@@ -0,0 +1,33 @@
|
||||
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
|
||||
import { createBlock, removeBlock } from '@libs/storage';
|
||||
|
||||
interface BlockData {
|
||||
kind: number;
|
||||
title: string;
|
||||
content: string;
|
||||
}
|
||||
|
||||
export function useBlock() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const add = useMutation({
|
||||
mutationFn: (data: BlockData) => {
|
||||
return createBlock(data.kind, data.title, data.content);
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['blocks'] });
|
||||
},
|
||||
});
|
||||
|
||||
const remove = useMutation({
|
||||
mutationFn: (id: string) => {
|
||||
return removeBlock(id);
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['blocks'] });
|
||||
},
|
||||
});
|
||||
|
||||
return { add, remove };
|
||||
}
|
||||
Reference in New Issue
Block a user