فهرست منبع

Merge pull request #1168 from weseek/create-api-for-re-edit

Create api for re edit
itizawa 6 سال پیش
والد
کامیت
866f26ccc8

+ 37 - 27
src/client/js/components/PageComment/CommentEditor.jsx

@@ -88,40 +88,50 @@ class CommentEditor extends React.Component {
     this.props.commentButtonClickedHandler(targetId);
     this.props.commentButtonClickedHandler(targetId);
   }
   }
 
 
+  initializeEditor() {
+    this.setState({
+      comment: '',
+      isMarkdown: true,
+      html: '',
+      key: 1,
+      errorMessage: undefined,
+    });
+    // reset value
+    this.editor.setValue('');
+    this.toggleEditor();
+  }
+
   /**
   /**
    * Post comment with CommentContainer and update state
    * Post comment with CommentContainer and update state
    */
    */
-  postHandler(event) {
-    // TODO GW-61 implementation for reEdit comment
+  async postHandler(event) {
     if (event != null) {
     if (event != null) {
       event.preventDefault();
       event.preventDefault();
     }
     }
 
 
-    const { commentContainer } = this.props;
-
-    this.props.commentContainer.postComment(
-      this.state.comment,
-      this.state.isMarkdown,
-      this.props.replyTo,
-      commentContainer.state.isSlackEnabled,
-      commentContainer.state.slackChannels,
-    )
-      .then((res) => {
-        this.setState({
-          comment: '',
-          isMarkdown: true,
-          html: '',
-          key: 1,
-          errorMessage: undefined,
-        });
-        // reset value
-        this.editor.setValue('');
-        this.toggleEditor();
-      })
-      .catch((err) => {
-        const errorMessage = err.message || 'An unknown error occured when posting comment';
-        this.setState({ errorMessage });
-      });
+    try {
+      if (this.props.currentCommentId != null) {
+        await this.props.commentContainer.putComment(
+          this.state.comment,
+          this.state.isMarkdown,
+          this.props.currentCommentId,
+        );
+      }
+      else {
+        await this.props.commentContainer.postComment(
+          this.state.comment,
+          this.state.isMarkdown,
+          this.props.replyTo,
+          this.props.commentContainer.state.isSlackEnabled,
+          this.props.commentContainer.state.slackChannels,
+        );
+      }
+      this.initializeEditor();
+    }
+    catch (err) {
+      const errorMessage = err.message || 'An unknown error occured when posting comment';
+      this.setState({ errorMessage });
+    }
   }
   }
 
 
   uploadHandler(file) {
   uploadHandler(file) {

+ 22 - 0
src/client/js/services/CommentContainer.js

@@ -100,6 +100,28 @@ export default class CommentContainer extends Container {
       });
       });
   }
   }
 
 
+  /**
+   * Load data of comments and rerender <PageComments />
+   */
+  putComment(comment, isMarkdown, commentId) {
+    const { pageId, revisionId } = this.getPageContainer().state;
+
+    return this.appContainer.apiPost('/comments.update', {
+      commentForm: {
+        comment,
+        page_id: pageId,
+        revision_id: revisionId,
+        is_markdown: isMarkdown,
+        comment_id: commentId,
+      },
+    })
+      .then((res) => {
+        if (res.ok) {
+          return this.retrieveComments();
+        }
+      });
+  }
+
   deleteComment(comment) {
   deleteComment(comment) {
     return this.appContainer.apiPost('/comments.remove', { comment_id: comment._id })
     return this.appContainer.apiPost('/comments.remove', { comment_id: comment._id })
       .then((res) => {
       .then((res) => {

+ 7 - 0
src/server/models/comment.js

@@ -64,6 +64,13 @@ module.exports = function(crowi) {
     }));
     }));
   };
   };
 
 
+  commentSchema.statics.updateCommentsByPageId = function(comment, isMarkdown, pageId) {
+    // TODO GW-61 update new comment add return comment data
+    console.log('ここはmodel');
+
+    return pageId;
+  };
+
   commentSchema.statics.removeCommentsByPageId = function(pageId) {
   commentSchema.statics.removeCommentsByPageId = function(pageId) {
     const Comment = this;
     const Comment = this;
 
 

+ 50 - 0
src/server/routes/comment.js

@@ -147,6 +147,56 @@ module.exports = function(crowi, app) {
     }
     }
   };
   };
 
 
+  /**
+   * @api {post} /comments.update Update comment dody
+   * @apiName UpdateComment
+   * @apiGroup Comment
+   *
+   */
+  api.update = async function(req, res) {
+    const { commentForm } = req.body;
+
+    const pageId = commentForm.page_id;
+    const revisionId = commentForm.revision_id;
+    const comment = commentForm.comment;
+    const isMarkdown = commentForm.is_markdown;
+    const commentId = commentForm.comment_id;
+
+    if (comment === '') {
+      return res.json(ApiResponse.error('Comment text is required'));
+    }
+
+    if (commentId == null) {
+      return res.json(ApiResponse.error('\'comment_id\' is undefined'));
+    }
+
+    // check whether accessible
+    const isAccessible = await Page.isAccessiblePageByViewer(pageId, req.user._id, revisionId, comment, isMarkdown, req.user);
+    if (!isAccessible) {
+      return res.json(ApiResponse.error('Current user is not accessible to this page.'));
+    }
+
+    try {
+      const updatedComment = await Comment.updateCommentsByPageId(comment, isMarkdown, commentId);
+
+      const page = await Page.findOneAndUpdate({ _id: pageId }, {
+        lastUpdateUser: req.user,
+        updatedAt: new Date(),
+      });
+
+      res.json(ApiResponse.success({ comment: updatedComment }));
+
+      const path = page.path;
+
+      // global notification
+      globalNotificationService.notifyComment(updatedComment, path);
+    }
+    catch (err) {
+      return res.json(ApiResponse.error(err));
+    }
+  };
+
+
   /**
   /**
    * @api {post} /comments.remove Remove specified comment
    * @api {post} /comments.remove Remove specified comment
    * @apiName RemoveComment
    * @apiName RemoveComment

+ 1 - 0
src/server/routes/index.js

@@ -202,6 +202,7 @@ module.exports = function(crowi, app) {
   app.post('/_api/tags.update'        , accessTokenParser, loginRequired(false), tag.api.update);
   app.post('/_api/tags.update'        , accessTokenParser, loginRequired(false), tag.api.update);
   app.get('/_api/comments.get'        , accessTokenParser , loginRequired(false) , comment.api.get);
   app.get('/_api/comments.get'        , accessTokenParser , loginRequired(false) , comment.api.get);
   app.post('/_api/comments.add'       , comment.api.validators.add(), accessTokenParser , loginRequired() , csrf, comment.api.add);
   app.post('/_api/comments.add'       , comment.api.validators.add(), accessTokenParser , loginRequired() , csrf, comment.api.add);
+  app.post('/_api/comments.update'       , comment.api.validators.add(), accessTokenParser , loginRequired() , csrf, comment.api.update);
   app.post('/_api/comments.remove'    , accessTokenParser , loginRequired() , csrf, comment.api.remove);
   app.post('/_api/comments.remove'    , accessTokenParser , loginRequired() , csrf, comment.api.remove);
   app.get('/_api/bookmarks.get'       , accessTokenParser , loginRequired(false) , bookmark.api.get);
   app.get('/_api/bookmarks.get'       , accessTokenParser , loginRequired(false) , bookmark.api.get);
   app.post('/_api/bookmarks.add'      , accessTokenParser , loginRequired() , csrf, bookmark.api.add);
   app.post('/_api/bookmarks.add'      , accessTokenParser , loginRequired() , csrf, bookmark.api.add);