PageComments.jsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 GrowiRenderer from '../util/GrowiRenderer';
  6. import AppContainer from '../services/AppContainer';
  7. import CommentContainer from '../services/CommentContainer';
  8. import { createSubscribedElement } from './UnstatedUtils';
  9. import CommentEditor from './PageComment/CommentEditor';
  10. import Comment from './PageComment/Comment';
  11. import DeleteCommentModal from './PageComment/DeleteCommentModal';
  12. import PageContainer from '../services/PageContainer';
  13. /**
  14. * Load data of comments and render the list of <Comment />
  15. *
  16. * @author Yuki Takei <yuki@weseek.co.jp>
  17. *
  18. * @export
  19. * @class PageComments
  20. * @extends {React.Component}
  21. */
  22. class PageComments extends React.Component {
  23. constructor(props) {
  24. super(props);
  25. this.state = {
  26. isLayoutTypeGrowi: false,
  27. // for deleting comment
  28. commentToDelete: undefined,
  29. isDeleteConfirmModalShown: false,
  30. errorMessageForDeleting: undefined,
  31. showEditorIds: new Set(),
  32. };
  33. this.growiRenderer = new GrowiRenderer(window.crowi, this.props.crowiOriginRenderer, { mode: 'comment' });
  34. this.init = this.init.bind(this);
  35. this.confirmToDeleteComment = this.confirmToDeleteComment.bind(this);
  36. this.deleteComment = this.deleteComment.bind(this);
  37. this.showDeleteConfirmModal = this.showDeleteConfirmModal.bind(this);
  38. this.closeDeleteConfirmModal = this.closeDeleteConfirmModal.bind(this);
  39. this.replyButtonClickedHandler = this.replyButtonClickedHandler.bind(this);
  40. this.commentButtonClickedHandler = this.commentButtonClickedHandler.bind(this);
  41. }
  42. componentWillMount() {
  43. this.init();
  44. }
  45. init() {
  46. if (!this.props.pageContainer.state.pageId) {
  47. return;
  48. }
  49. const layoutType = this.props.appContainer.getConfig().layoutType;
  50. this.setState({ isLayoutTypeGrowi: layoutType === 'crowi-plus' || layoutType === 'growi' });
  51. this.props.commentContainer.retrieveComments();
  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. // adds replies to specific comment object
  90. addRepliesToComments(comment, replies) {
  91. const replyList = [];
  92. replies.forEach((reply) => {
  93. if (reply.replyTo === comment._id) {
  94. replyList.push(reply);
  95. }
  96. });
  97. return replyList;
  98. }
  99. /**
  100. * generate Elements of Comment
  101. *
  102. * @param {any} comments Array of Comment Model Obj
  103. *
  104. * @memberOf PageComments
  105. */
  106. generateCommentElements(comments, replies) {
  107. return comments.map((comment) => {
  108. const commentId = comment._id;
  109. const showEditor = this.state.showEditorIds.has(commentId);
  110. const username = this.props.appContainer.me;
  111. const replyList = this.addRepliesToComments(comment, replies);
  112. return (
  113. <div key={commentId}>
  114. <Comment
  115. comment={comment}
  116. deleteBtnClicked={this.confirmToDeleteComment}
  117. crowiRenderer={this.growiRenderer}
  118. replyList={replyList}
  119. revisionCreatedAt={this.props.revisionCreatedAt}
  120. />
  121. <div className="container-fluid">
  122. <div className="row">
  123. <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">
  124. { !showEditor && (
  125. <div>
  126. { username
  127. && (
  128. <div className="col-xs-offset-6 col-sm-offset-6 col-md-offset-6 col-lg-offset-6">
  129. <Button
  130. bsStyle="primary"
  131. className="fcbtn btn btn-sm btn-primary btn-outline btn-rounded btn-1b"
  132. onClick={() => { return this.replyButtonClickedHandler(commentId) }}
  133. >
  134. <i className="icon-bubble"></i> Reply
  135. </Button>
  136. </div>
  137. )
  138. }
  139. </div>
  140. )}
  141. { showEditor && (
  142. <CommentEditor
  143. crowiOriginRenderer={this.props.crowiOriginRenderer}
  144. slackChannels={this.props.slackChannels}
  145. replyTo={commentId}
  146. commentButtonClickedHandler={this.commentButtonClickedHandler}
  147. />
  148. )}
  149. </div>
  150. </div>
  151. </div>
  152. <br />
  153. </div>
  154. );
  155. });
  156. }
  157. render() {
  158. const currentComments = [];
  159. const currentReplies = [];
  160. let comments = this.props.commentContainer.state.comments;
  161. if (this.state.isLayoutTypeGrowi) {
  162. // replace with asc order array
  163. comments = comments.slice().reverse(); // non-destructive reverse
  164. }
  165. comments.forEach((comment) => {
  166. if (comment.replyTo === undefined) {
  167. // comment is not a reply
  168. currentComments.push(comment);
  169. }
  170. else {
  171. // comment is a reply
  172. currentReplies.push(comment);
  173. }
  174. });
  175. // generate elements
  176. const currentElements = this.generateCommentElements(currentComments, currentReplies);
  177. // generate blocks
  178. const currentBlock = (
  179. <div className="page-comments-list-current" id="page-comments-list-current">
  180. {currentElements}
  181. </div>
  182. );
  183. // layout blocks
  184. const commentsElements = (<div>{currentBlock}</div>);
  185. return (
  186. <div>
  187. {commentsElements}
  188. <DeleteCommentModal
  189. isShown={this.state.isDeleteConfirmModalShown}
  190. comment={this.state.commentToDelete}
  191. errorMessage={this.state.errorMessageForDeleting}
  192. cancel={this.closeDeleteConfirmModal}
  193. confirmedToDelete={this.deleteComment}
  194. />
  195. </div>
  196. );
  197. }
  198. }
  199. /**
  200. * Wrapper component for using unstated
  201. */
  202. const PageCommentsWrapper = (props) => {
  203. return createSubscribedElement(PageComments, props, [AppContainer, PageContainer, CommentContainer]);
  204. };
  205. PageComments.propTypes = {
  206. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  207. pageContainer: PropTypes.instanceOf(PageContainer).isRequired,
  208. commentContainer: PropTypes.instanceOf(CommentContainer).isRequired,
  209. crowiOriginRenderer: PropTypes.object.isRequired,
  210. revisionCreatedAt: PropTypes.number,
  211. slackChannels: PropTypes.string,
  212. };
  213. export default withTranslation()(PageCommentsWrapper);