PageComments.jsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import Button from 'react-bootstrap/es/Button';
  4. import { withTranslation } from 'react-i18next';
  5. import AppContainer from '../services/AppContainer';
  6. import CommentContainer from '../services/CommentContainer';
  7. import PageContainer from '../services/PageContainer';
  8. import { createSubscribedElement } from './UnstatedUtils';
  9. import CommentEditor from './PageComment/CommentEditor';
  10. import Comment from './PageComment/Comment';
  11. import DeleteCommentModal from './PageComment/DeleteCommentModal';
  12. /**
  13. * Load data of comments and render the list of <Comment />
  14. *
  15. * @author Yuki Takei <yuki@weseek.co.jp>
  16. *
  17. * @export
  18. * @class PageComments
  19. * @extends {React.Component}
  20. */
  21. class PageComments extends React.Component {
  22. constructor(props) {
  23. super(props);
  24. this.state = {
  25. // for deleting comment
  26. commentToDelete: undefined,
  27. isDeleteConfirmModalShown: false,
  28. errorMessageForDeleting: undefined,
  29. showEditorIds: new Set(),
  30. };
  31. this.growiRenderer = this.props.appContainer.getRenderer('comment');
  32. this.init = this.init.bind(this);
  33. this.confirmToEditComment = this.confirmToEditComment.bind(this);
  34. this.confirmToDeleteComment = this.confirmToDeleteComment.bind(this);
  35. this.deleteComment = this.deleteComment.bind(this);
  36. this.showDeleteConfirmModal = this.showDeleteConfirmModal.bind(this);
  37. this.closeDeleteConfirmModal = this.closeDeleteConfirmModal.bind(this);
  38. this.replyButtonClickedHandler = this.replyButtonClickedHandler.bind(this);
  39. this.commentButtonClickedHandler = this.commentButtonClickedHandler.bind(this);
  40. }
  41. componentWillMount() {
  42. this.init();
  43. }
  44. init() {
  45. if (!this.props.pageContainer.state.pageId) {
  46. return;
  47. }
  48. this.props.commentContainer.retrieveComments();
  49. }
  50. confirmToEditComment(comment) {
  51. console.log("Pushed Edit button");
  52. }
  53. confirmToDeleteComment(comment) {
  54. this.setState({ commentToDelete: comment });
  55. this.showDeleteConfirmModal();
  56. }
  57. deleteComment() {
  58. const comment = this.state.commentToDelete;
  59. this.props.commentContainer.deleteComment(comment)
  60. .then(() => {
  61. this.closeDeleteConfirmModal();
  62. })
  63. .catch((err) => {
  64. this.setState({ errorMessageForDeleting: err.message });
  65. });
  66. }
  67. showDeleteConfirmModal() {
  68. this.setState({ isDeleteConfirmModalShown: true });
  69. }
  70. closeDeleteConfirmModal() {
  71. this.setState({
  72. commentToDelete: undefined,
  73. isDeleteConfirmModalShown: false,
  74. errorMessageForDeleting: undefined,
  75. });
  76. }
  77. replyButtonClickedHandler(commentId) {
  78. const ids = this.state.showEditorIds.add(commentId);
  79. this.setState({ showEditorIds: ids });
  80. }
  81. commentButtonClickedHandler(commentId) {
  82. this.setState((prevState) => {
  83. prevState.showEditorIds.delete(commentId);
  84. return {
  85. showEditorIds: prevState.showEditorIds,
  86. };
  87. });
  88. }
  89. // get replies to specific comment object
  90. getRepliesFor(comment, allReplies) {
  91. const replyList = [];
  92. allReplies.forEach((reply) => {
  93. if (reply.replyTo === comment._id) {
  94. replyList.push(reply);
  95. }
  96. });
  97. return replyList;
  98. }
  99. /**
  100. * render Elements of Comment Thread
  101. *
  102. * @param {any} comment Comment Model Obj
  103. * @param {any} replies List of Reply Comment Model Obj
  104. *
  105. * @memberOf PageComments
  106. */
  107. renderThread(comment, replies) {
  108. const commentId = comment._id;
  109. const showEditor = this.state.showEditorIds.has(commentId);
  110. const isLoggedIn = this.props.appContainer.me != null;
  111. let rootClassNames = 'page-comment-thread';
  112. if (replies.length === 0) {
  113. rootClassNames += ' page-comment-thread-no-replies';
  114. }
  115. return (
  116. <div key={commentId} className={`mb-5 ${rootClassNames}`}>
  117. <Comment
  118. comment={comment}
  119. editBtnClicked={this.confirmToEditComment}
  120. deleteBtnClicked={this.confirmToDeleteComment}
  121. growiRenderer={this.growiRenderer}
  122. replyList={replies}
  123. />
  124. { !showEditor && isLoggedIn && (
  125. <div className="text-right">
  126. <Button
  127. bsStyle="default"
  128. className="btn btn-outline btn-default btn-sm btn-comment-reply"
  129. onClick={() => { return this.replyButtonClickedHandler(commentId) }}
  130. >
  131. <i className="icon-fw icon-action-redo"></i> Reply
  132. </Button>
  133. </div>
  134. )}
  135. { showEditor && isLoggedIn && (
  136. <div className="page-comment-reply-form">
  137. <CommentEditor
  138. growiRenderer={this.growiRenderer}
  139. replyTo={commentId}
  140. commentButtonClickedHandler={this.commentButtonClickedHandler}
  141. />
  142. </div>
  143. )}
  144. </div>
  145. );
  146. }
  147. render() {
  148. const topLevelComments = [];
  149. const allReplies = [];
  150. const layoutType = this.props.appContainer.getConfig().layoutType;
  151. const isBaloonStyle = layoutType.match(/crowi-plus|growi|kibela/);
  152. let comments = this.props.commentContainer.state.comments;
  153. if (isBaloonStyle) {
  154. // replace with asc order array
  155. comments = comments.slice().reverse(); // non-destructive reverse
  156. }
  157. comments.forEach((comment) => {
  158. if (comment.replyTo === undefined) {
  159. // comment is not a reply
  160. topLevelComments.push(comment);
  161. }
  162. else {
  163. // comment is a reply
  164. allReplies.push(comment);
  165. }
  166. });
  167. return (
  168. <div>
  169. { topLevelComments.map((topLevelComment) => {
  170. // get related replies
  171. const replies = this.getRepliesFor(topLevelComment, allReplies);
  172. return this.renderThread(topLevelComment, replies);
  173. }) }
  174. <DeleteCommentModal
  175. isShown={this.state.isDeleteConfirmModalShown}
  176. comment={this.state.commentToDelete}
  177. errorMessage={this.state.errorMessageForDeleting}
  178. cancel={this.closeDeleteConfirmModal}
  179. confirmedToDelete={this.deleteComment}
  180. />
  181. </div>
  182. );
  183. }
  184. }
  185. /**
  186. * Wrapper component for using unstated
  187. */
  188. const PageCommentsWrapper = (props) => {
  189. return createSubscribedElement(PageComments, props, [AppContainer, PageContainer, CommentContainer]);
  190. };
  191. PageComments.propTypes = {
  192. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  193. pageContainer: PropTypes.instanceOf(PageContainer).isRequired,
  194. commentContainer: PropTypes.instanceOf(CommentContainer).isRequired,
  195. };
  196. export default withTranslation()(PageCommentsWrapper);