PageComments.jsx 6.5 KB

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