diff --git a/client/src/hooks/mutation/useAddGuestbookMutation.ts b/client/src/hooks/mutation/useAddGuestbookMutation.ts new file mode 100644 index 00000000..1a1e7053 --- /dev/null +++ b/client/src/hooks/mutation/useAddGuestbookMutation.ts @@ -0,0 +1,29 @@ +import { useParams } from 'next/navigation'; + +import { useMutation, useQueryClient } from '@tanstack/react-query'; + +import { addGuestbook } from '@/api/garden'; + +import useUserStore from '@/stores/userStore'; + +import { CommentInputValue } from '@/types/common'; + +const useAddGuestbookMutation = () => { + const queryClient = useQueryClient(); + + const { id } = useParams(); + + const { userId } = useUserStore(); + + const { mutate } = useMutation({ + mutationFn: ({ comment }: CommentInputValue) => + addGuestbook(id as string, comment), + onSuccess: () => { + queryClient.invalidateQueries({ queryKey: ['guestbook', userId] }); + }, + }); + + return { mutate }; +}; + +export default useAddGuestbookMutation;