refactor react-query usage

This commit is contained in:
Philipinho
2023-11-13 17:48:32 +00:00
parent dc65fbafa4
commit fb057b7801
22 changed files with 286 additions and 274 deletions

View File

@@ -4,7 +4,6 @@ import { useClickOutside } from '@mantine/hooks';
import { useAtom } from 'jotai';
import {
activeCommentIdAtom,
commentsAtom,
draftCommentIdAtom,
showCommentPopupAtom,
} from '@/features/comment/atoms/comment-atom';
@@ -12,7 +11,7 @@ import { Editor } from '@tiptap/core';
import CommentEditor from '@/features/comment/components/comment-editor';
import CommentActions from '@/features/comment/components/comment-actions';
import { currentUserAtom } from '@/features/user/atoms/current-user-atom';
import useComment from '@/features/comment/hooks/use-comment';
import { useCreateCommentMutation } from '@/features/comment/queries/comment';
interface CommentDialogProps {
editor: Editor,
@@ -24,12 +23,11 @@ function CommentDialog({ editor, pageId }: CommentDialogProps) {
const [, setShowCommentPopup] = useAtom<boolean>(showCommentPopupAtom);
const [, setActiveCommentId] = useAtom<string | null>(activeCommentIdAtom);
const [draftCommentId, setDraftCommentId] = useAtom<string | null>(draftCommentIdAtom);
const [comments, setComments] = useAtom(commentsAtom(pageId));
const [currentUser] = useAtom(currentUserAtom);
const useClickOutsideRef = useClickOutside(() => {
handleDialogClose();
});
const { createCommentMutation } = useComment();
const createCommentMutation = useCreateCommentMutation();
const { isLoading } = createCommentMutation;
const handleDialogClose = () => {
@@ -43,17 +41,16 @@ function CommentDialog({ editor, pageId }: CommentDialogProps) {
};
const handleAddComment = async () => {
const selectedText = getSelectedText();
const commentData = {
id: draftCommentId,
pageId: pageId,
content: JSON.stringify(comment),
selection: selectedText,
};
try {
const selectedText = getSelectedText();
const commentData = {
id: draftCommentId,
pageId: pageId,
content: JSON.stringify(comment),
selection: selectedText,
};
const createdComment = await createCommentMutation.mutateAsync(commentData);
setComments(prevComments => [...prevComments, createdComment]);
editor.chain().setComment(createdComment.id).unsetCommentDecoration().run();
setActiveCommentId(createdComment.id);