PageComments.jsx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /* eslint-disable react/no-multi-comp */
  2. /* eslint-disable react/no-access-state-in-setstate */
  3. import React from 'react';
  4. import PropTypes from 'prop-types';
  5. import Button from 'react-bootstrap/es/Button';
  6. import { Subscribe } from 'unstated';
  7. import { withTranslation } from 'react-i18next';
  8. import GrowiRenderer from '../util/GrowiRenderer';
  9. import CommentContainer from './PageComment/CommentContainer';
  10. import CommentEditor from './PageComment/CommentEditor';
  11. import Comment from './PageComment/Comment';
  12. import DeleteCommentModal from './PageComment/DeleteCommentModal';
  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(this.props.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.pageId) {
  47. return;
  48. }
  49. const layoutType = this.props.crowi.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 crowi = this.props.crowi;
  111. const username = crowi.me;
  112. const replyList = this.addRepliesToComments(comment, replies);
  113. return (
  114. <div key={commentId}>
  115. <Comment
  116. comment={comment}
  117. deleteBtnClicked={this.confirmToDeleteComment}
  118. crowiRenderer={this.growiRenderer}
  119. crowi={this.props.crowi}
  120. replyList={replyList}
  121. revisionCreatedAt={this.props.revisionCreatedAt}
  122. revisionId={this.props.revisionId}
  123. />
  124. <div className="container-fluid">
  125. <div className="row">
  126. <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">
  127. { !showEditor && (
  128. <div>
  129. { username
  130. && (
  131. <div className="col-xs-offset-6 col-sm-offset-6 col-md-offset-6 col-lg-offset-6">
  132. <Button
  133. bsStyle="primary"
  134. className="fcbtn btn btn-sm btn-primary btn-outline btn-rounded btn-1b"
  135. onClick={() => { return this.replyButtonClickedHandler(commentId) }}
  136. >
  137. <i className="icon-bubble"></i> Reply
  138. </Button>
  139. </div>
  140. )
  141. }
  142. </div>
  143. )}
  144. { showEditor && (
  145. <CommentEditor
  146. crowi={this.props.crowi}
  147. crowiOriginRenderer={this.props.crowiOriginRenderer}
  148. editorOptions={this.props.editorOptions}
  149. slackChannels={this.props.slackChannels}
  150. replyTo={commentId}
  151. commentButtonClickedHandler={this.commentButtonClickedHandler}
  152. />
  153. )}
  154. </div>
  155. </div>
  156. </div>
  157. <br />
  158. </div>
  159. );
  160. });
  161. }
  162. render() {
  163. const currentComments = [];
  164. const currentReplies = [];
  165. let comments = this.props.commentContainer.state.comments;
  166. if (this.state.isLayoutTypeGrowi) {
  167. // replace with asc order array
  168. comments = comments.slice().reverse(); // non-destructive reverse
  169. }
  170. comments.forEach((comment) => {
  171. if (comment.replyTo === undefined) {
  172. // comment is not a reply
  173. currentComments.push(comment);
  174. }
  175. else {
  176. // comment is a reply
  177. currentReplies.push(comment);
  178. }
  179. });
  180. // generate elements
  181. const currentElements = this.generateCommentElements(currentComments, currentReplies);
  182. // generate blocks
  183. const currentBlock = (
  184. <div className="page-comments-list-current" id="page-comments-list-current">
  185. {currentElements}
  186. </div>
  187. );
  188. // layout blocks
  189. const commentsElements = (<div>{currentBlock}</div>);
  190. return (
  191. <div>
  192. {commentsElements}
  193. <DeleteCommentModal
  194. isShown={this.state.isDeleteConfirmModalShown}
  195. comment={this.state.commentToDelete}
  196. errorMessage={this.state.errorMessageForDeleting}
  197. cancel={this.closeDeleteConfirmModal}
  198. confirmedToDelete={this.deleteComment}
  199. />
  200. </div>
  201. );
  202. }
  203. }
  204. /**
  205. * Wrapper component for using unstated
  206. */
  207. class PageCommentsWrapper extends React.Component {
  208. render() {
  209. return (
  210. <Subscribe to={[CommentContainer]}>
  211. { commentContainer => (
  212. // eslint-disable-next-line arrow-body-style
  213. <PageComments commentContainer={commentContainer} {...this.props} />
  214. )}
  215. </Subscribe>
  216. );
  217. }
  218. }
  219. PageCommentsWrapper.propTypes = {
  220. crowi: PropTypes.object.isRequired,
  221. crowiOriginRenderer: PropTypes.object.isRequired,
  222. pageId: PropTypes.string.isRequired,
  223. revisionId: PropTypes.string.isRequired,
  224. revisionCreatedAt: PropTypes.number,
  225. pagePath: PropTypes.string,
  226. editorOptions: PropTypes.object,
  227. slackChannels: PropTypes.string,
  228. };
  229. PageComments.propTypes = {
  230. commentContainer: PropTypes.object.isRequired,
  231. crowi: PropTypes.object.isRequired,
  232. crowiOriginRenderer: PropTypes.object.isRequired,
  233. pageId: PropTypes.string.isRequired,
  234. revisionId: PropTypes.string.isRequired,
  235. revisionCreatedAt: PropTypes.number,
  236. pagePath: PropTypes.string,
  237. editorOptions: PropTypes.object,
  238. slackChannels: PropTypes.string,
  239. };
  240. export default withTranslation(null, { withRef: true })(PageCommentsWrapper);