From 734c3e0e78a728cafbe03426b00a4359a9fe21b0 Mon Sep 17 00:00:00 2001
From: chris <kokosias@yahoo.gr>
Date: Thu, 7 Mar 2024 12:49:04 +0200
Subject: [PATCH] remove/resolve comment

---
 .../src/CommentsService/CommentsService.js    |  1 +
 .../components/ConnectedComment.js            | 45 ++++++++-----------
 .../ui/comments/CommentBubbleComponent.js     |  2 +-
 3 files changed, 20 insertions(+), 28 deletions(-)

diff --git a/wax-prosemirror-services/src/CommentsService/CommentsService.js b/wax-prosemirror-services/src/CommentsService/CommentsService.js
index 324d7f553..dedf6d5bf 100644
--- a/wax-prosemirror-services/src/CommentsService/CommentsService.js
+++ b/wax-prosemirror-services/src/CommentsService/CommentsService.js
@@ -33,6 +33,7 @@ export default class CommentsService extends Service {
           ...items,
         ]);
         commentsConfig.getComments(this.allCommentsFromStates);
+
         this.app.context.setOption({ comments: this.allCommentsFromStates });
       },
       onAnnotationListChange: () => true,
diff --git a/wax-prosemirror-services/src/CommentsService/components/ConnectedComment.js b/wax-prosemirror-services/src/CommentsService/components/ConnectedComment.js
index ce1132a49..38c6f979b 100644
--- a/wax-prosemirror-services/src/CommentsService/components/ConnectedComment.js
+++ b/wax-prosemirror-services/src/CommentsService/components/ConnectedComment.js
@@ -7,6 +7,7 @@ import styled from 'styled-components';
 import { WaxContext, DocumentHelpers, Commands } from 'wax-prosemirror-core';
 import { override } from '@pubsweet/ui-toolkit';
 import CommentBox from './ui/comments/CommentBox';
+import { AnnotationPluginKey } from '../plugins/AnnotationPlugin';
 
 const ConnectedCommentStyled = styled.div`
   margin-left: ${props => (props.active ? `${-20}px` : `${50}px`)};
@@ -33,6 +34,7 @@ export default ({ comment, top, commentId, recalculateTops, users }) => {
     app,
     activeView,
     activeViewId,
+    options: { comments },
   } = context;
 
   const [isActive, setIsActive] = useState(false);
@@ -41,8 +43,6 @@ export default ({ comment, top, commentId, recalculateTops, users }) => {
   const { state, dispatch } = activeView;
   const { viewId, conversation } = comment.data;
 
-  const commentMark = state.schema.marks.comment;
-
   const styles = {
     top: `${top}px`,
   };
@@ -106,36 +106,27 @@ export default ({ comment, top, commentId, recalculateTops, users }) => {
   };
 
   const onClickResolve = () => {
-    let maxPos = comment.pos;
-    let minPos = comment.pos;
-
-    allCommentsWithSameId.forEach(singleComment => {
-      const markPosition = DocumentHelpers.findMarkPosition(
-        state,
-        singleComment.pos,
-        'comment',
-      );
-      if (markPosition.from < minPos) minPos = markPosition.from;
-      if (markPosition.to > maxPos) maxPos = markPosition.to;
-    });
-    // if (allCommentsWithSameId.length > 1);
-    // maxPos += last(allCommentsWithSameId).node.nodeSize;
+    dispatch(
+      state.tr.setMeta(AnnotationPluginKey, {
+        type: 'deleteComment',
+        id: activeComment.id,
+      }),
+    );
     recalculateTops();
-    dispatch(state.tr.removeMark(minPos, maxPos, commentMark));
     activeView.focus();
+    setTimeout(() => {
+      const newComments = comments.filter(comm => {
+        return comm.id !== activeComment.id;
+      });
+      context.setOption({ comments: newComments });
+    });
   };
 
   const onTextAreaBlur = () => {
-    // TODO Save into local storage
-    // if (content !== '') {
-    //   onClickPost(content);
-    // }
-    setTimeout(() => {
-      if (conversation.length === 0 && !clickPost) {
-        // onClickResolve();
-        activeView.focus();
-      }
-    }, 400);
+    if (conversation.length === 0 && !clickPost) {
+      onClickResolve();
+      activeView.focus();
+    }
   };
 
   const MemorizedComponent = useMemo(
diff --git a/wax-prosemirror-services/src/CommentsService/components/ui/comments/CommentBubbleComponent.js b/wax-prosemirror-services/src/CommentsService/components/ui/comments/CommentBubbleComponent.js
index 3cabe3ac4..afef107f2 100644
--- a/wax-prosemirror-services/src/CommentsService/components/ui/comments/CommentBubbleComponent.js
+++ b/wax-prosemirror-services/src/CommentsService/components/ui/comments/CommentBubbleComponent.js
@@ -1,6 +1,6 @@
 /* eslint react/prop-types: 0 */
 import React, { useLayoutEffect, useContext } from 'react';
-import { WaxContext, Commands, DocumentHelpers } from 'wax-prosemirror-core';
+import { WaxContext, DocumentHelpers } from 'wax-prosemirror-core';
 import CommentBubble from './CommentBubble';
 import { AnnotationPluginKey } from '../../../plugins/AnnotationPlugin';
 
-- 
GitLab