PageComments.jsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 { createSubscribedElement } from './UnstatedUtils';
  8. import CommentEditor from './PageComment/CommentEditor';
  9. import Comment from './PageComment/Comment';
  10. import DeleteCommentModal from './PageComment/DeleteCommentModal';
  11. import PageContainer from '../services/PageContainer';
  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. isLayoutTypeGrowi: false,
  26. // for deleting comment
  27. commentToDelete: undefined,
  28. isDeleteConfirmModalShown: false,
  29. errorMessageForDeleting: undefined,
  30. showEditorIds: new Set(),
  31. };
  32. this.growiRenderer = this.props.appContainer.getRenderer('comment');
  33. this.init = this.init.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. const layoutType = this.props.appContainer.getConfig().layoutType;
  49. this.setState({ isLayoutTypeGrowi: layoutType === 'crowi-plus' || layoutType === 'growi' });
  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. // adds replies to specific comment object
  89. addRepliesToComments(comment, replies) {
  90. const replyList = [];
  91. replies.forEach((reply) => {
  92. if (reply.replyTo === comment._id) {
  93. replyList.push(reply);
  94. }
  95. });
  96. return replyList;
  97. }
  98. /**
  99. * generate Elements of Comment
  100. *
  101. * @param {any} comments Array of Comment Model Obj
  102. *
  103. * @memberOf PageComments
  104. */
  105. generateCommentElements(comments, replies) {
  106. return comments.map((comment) => {
  107. const commentId = comment._id;
  108. const showEditor = this.state.showEditorIds.has(commentId);
  109. const username = this.props.appContainer.me;
  110. const replyList = this.addRepliesToComments(comment, replies);
  111. return (
  112. <div key={commentId}>
  113. <Comment
  114. comment={comment}
  115. deleteBtnClicked={this.confirmToDeleteComment}
  116. growiRenderer={this.growiRenderer}
  117. replyList={replyList}
  118. />
  119. <div className="container-fluid">
  120. <div className="row">
  121. <div className="col-xs-offset-1 col-xs-11 col-sm-offset-1 col-sm-11 col-md-offset-1 col-md-11 col-lg-offset-1 col-lg-11">
  122. { !showEditor && (
  123. <div>
  124. { username
  125. && (
  126. <div className="col-xs-offset-6 col-sm-offset-6 col-md-offset-6 col-lg-offset-6">
  127. <Button
  128. bsStyle="primary"
  129. className="fcbtn btn btn-outline btn-rounded btn-xxs"
  130. onClick={() => { return this.replyButtonClickedHandler(commentId) }}
  131. >
  132. Reply <i className="fa fa-mail-reply"></i>
  133. </Button>
  134. </div>
  135. )
  136. }
  137. </div>
  138. )}
  139. { showEditor && (
  140. <CommentEditor
  141. growiRenderer={this.growiRenderer}
  142. replyTo={commentId}
  143. commentButtonClickedHandler={this.commentButtonClickedHandler}
  144. />
  145. )}
  146. </div>
  147. </div>
  148. </div>
  149. <br />
  150. </div>
  151. );
  152. });
  153. }
  154. render() {
  155. const currentComments = [];
  156. const currentReplies = [];
  157. let comments = this.props.commentContainer.state.comments;
  158. if (this.state.isLayoutTypeGrowi) {
  159. // replace with asc order array
  160. comments = comments.slice().reverse(); // non-destructive reverse
  161. }
  162. comments.forEach((comment) => {
  163. if (comment.replyTo === undefined) {
  164. // comment is not a reply
  165. currentComments.push(comment);
  166. }
  167. else {
  168. // comment is a reply
  169. currentReplies.push(comment);
  170. }
  171. });
  172. // generate elements
  173. const currentElements = this.generateCommentElements(currentComments, currentReplies);
  174. // generate blocks
  175. const currentBlock = (
  176. <div className="page-comments-list-current" id="page-comments-list-current">
  177. {currentElements}
  178. </div>
  179. );
  180. // layout blocks
  181. const commentsElements = (<div>{currentBlock}</div>);
  182. return (
  183. <div>
  184. {commentsElements}
  185. <DeleteCommentModal
  186. isShown={this.state.isDeleteConfirmModalShown}
  187. comment={this.state.commentToDelete}
  188. errorMessage={this.state.errorMessageForDeleting}
  189. cancel={this.closeDeleteConfirmModal}
  190. confirmedToDelete={this.deleteComment}
  191. />
  192. </div>
  193. );
  194. }
  195. }
  196. /**
  197. * Wrapper component for using unstated
  198. */
  199. const PageCommentsWrapper = (props) => {
  200. return createSubscribedElement(PageComments, props, [AppContainer, PageContainer, CommentContainer]);
  201. };
  202. PageComments.propTypes = {
  203. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  204. pageContainer: PropTypes.instanceOf(PageContainer).isRequired,
  205. commentContainer: PropTypes.instanceOf(CommentContainer).isRequired,
  206. };
  207. export default withTranslation()(PageCommentsWrapper);