PageComments.jsx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. const comments = this.props.commentContainer.state.comments;
  59. comments.forEach((reply) => {
  60. if (reply.replyTo === comment._id) {
  61. this.props.commentContainer.deleteComment(reply);
  62. }
  63. });
  64. this.props.commentContainer.deleteComment(comment)
  65. .then(() => {
  66. this.closeDeleteConfirmModal();
  67. })
  68. .catch((err) => {
  69. this.setState({ errorMessageForDeleting: err.message });
  70. });
  71. }
  72. showDeleteConfirmModal() {
  73. this.setState({ isDeleteConfirmModalShown: true });
  74. }
  75. closeDeleteConfirmModal() {
  76. this.setState({
  77. commentToDelete: undefined,
  78. isDeleteConfirmModalShown: false,
  79. errorMessageForDeleting: undefined,
  80. });
  81. }
  82. replyButtonClickedHandler(commentId) {
  83. const ids = this.state.showEditorIds.add(commentId);
  84. this.setState({ showEditorIds: ids });
  85. }
  86. commentButtonClickedHandler(commentId) {
  87. this.setState((prevState) => {
  88. prevState.showEditorIds.delete(commentId);
  89. return {
  90. showEditorIds: prevState.showEditorIds,
  91. };
  92. });
  93. }
  94. // adds replies to specific comment object
  95. addRepliesToComments(comment, replies) {
  96. const replyList = [];
  97. replies.forEach((reply) => {
  98. if (reply.replyTo === comment._id) {
  99. replyList.push(reply);
  100. }
  101. });
  102. return replyList;
  103. }
  104. /**
  105. * generate Elements of Comment
  106. *
  107. * @param {any} comments Array of Comment Model Obj
  108. *
  109. * @memberOf PageComments
  110. */
  111. generateCommentElements(comments, replies) {
  112. return comments.map((comment) => {
  113. const commentId = comment._id;
  114. const showEditor = this.state.showEditorIds.has(commentId);
  115. const username = this.props.appContainer.me;
  116. const replyList = this.addRepliesToComments(comment, replies);
  117. return (
  118. <div key={commentId}>
  119. <Comment
  120. comment={comment}
  121. deleteBtnClicked={this.confirmToDeleteComment}
  122. growiRenderer={this.growiRenderer}
  123. replyList={replyList}
  124. />
  125. <div className="container-fluid">
  126. <div className="row">
  127. <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">
  128. { !showEditor && (
  129. <div>
  130. { username
  131. && (
  132. <div className="col-xs-offset-6 col-sm-offset-6 col-md-offset-6 col-lg-offset-6">
  133. <Button
  134. bsStyle="primary"
  135. className="fcbtn btn btn-sm btn-primary btn-outline btn-rounded btn-1b"
  136. onClick={() => { return this.replyButtonClickedHandler(commentId) }}
  137. >
  138. <i className="icon-bubble"></i> Reply
  139. </Button>
  140. </div>
  141. )
  142. }
  143. </div>
  144. )}
  145. { showEditor && (
  146. <CommentEditor
  147. growiRenderer={this.growiRenderer}
  148. replyTo={commentId}
  149. commentButtonClickedHandler={this.commentButtonClickedHandler}
  150. />
  151. )}
  152. </div>
  153. </div>
  154. </div>
  155. <br />
  156. </div>
  157. );
  158. });
  159. }
  160. render() {
  161. const currentComments = [];
  162. const currentReplies = [];
  163. let comments = this.props.commentContainer.state.comments;
  164. if (this.state.isLayoutTypeGrowi) {
  165. // replace with asc order array
  166. comments = comments.slice().reverse(); // non-destructive reverse
  167. }
  168. comments.forEach((comment) => {
  169. if (comment.replyTo === undefined) {
  170. // comment is not a reply
  171. currentComments.push(comment);
  172. }
  173. else {
  174. // comment is a reply
  175. currentReplies.push(comment);
  176. }
  177. });
  178. // generate elements
  179. const currentElements = this.generateCommentElements(currentComments, currentReplies);
  180. // generate blocks
  181. const currentBlock = (
  182. <div className="page-comments-list-current" id="page-comments-list-current">
  183. {currentElements}
  184. </div>
  185. );
  186. // layout blocks
  187. const commentsElements = (<div>{currentBlock}</div>);
  188. return (
  189. <div>
  190. {commentsElements}
  191. <DeleteCommentModal
  192. isShown={this.state.isDeleteConfirmModalShown}
  193. comment={this.state.commentToDelete}
  194. errorMessage={this.state.errorMessageForDeleting}
  195. cancel={this.closeDeleteConfirmModal}
  196. confirmedToDelete={this.deleteComment}
  197. />
  198. </div>
  199. );
  200. }
  201. }
  202. /**
  203. * Wrapper component for using unstated
  204. */
  205. const PageCommentsWrapper = (props) => {
  206. return createSubscribedElement(PageComments, props, [AppContainer, PageContainer, CommentContainer]);
  207. };
  208. PageComments.propTypes = {
  209. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  210. pageContainer: PropTypes.instanceOf(PageContainer).isRequired,
  211. commentContainer: PropTypes.instanceOf(CommentContainer).isRequired,
  212. };
  213. export default withTranslation()(PageCommentsWrapper);