mirror of
https://github.com/Shadowfita/docmost.git
synced 2025-11-26 14:33:31 +10:00
refactor react-query usage
This commit is contained in:
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user