add useBlock hook

This commit is contained in:
Ren Amamiya
2023-07-19 12:47:45 +07:00
parent 6307e8d1e4
commit 22c1eaa541
13 changed files with 131 additions and 221 deletions

View 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 };
}