PageComments.jsx 8.2 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 { Subscribe } from 'unstated';
  6. import { withTranslation } from 'react-i18next';
  7. import GrowiRenderer from '../util/GrowiRenderer';
  8. import CommentContainer from './PageComment/CommentContainer';
  9. import UserPicture from './User/UserPicture';
  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. }
  41. componentWillMount() {
  42. this.init();
  43. }
  44. init() {
  45. if (!this.props.pageId) {
  46. return;
  47. }
  48. const layoutType = this.props.crowi.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. // adds replies to specific comment object
  81. addRepliesToComments(comment, replies) {
  82. const replyList = [];
  83. replies.forEach((reply) => {
  84. if (reply.replyTo === comment._id) {
  85. replyList.push(reply);
  86. }
  87. });
  88. return replyList;
  89. }
  90. /**
  91. * generate Elements of Comment
  92. *
  93. * @param {any} comments Array of Comment Model Obj
  94. *
  95. * @memberOf PageComments
  96. */
  97. generateCommentElements(comments, replies) {
  98. return comments.map((comment) => {
  99. const commentId = comment._id;
  100. const showEditor = this.state.showEditorIds.has(commentId);
  101. const crowi = this.props.crowi;
  102. const username = crowi.me;
  103. const user = crowi.findUser(username);
  104. const isLayoutTypeGrowi = this.state.isLayoutTypeGrowi;
  105. const replyList = this.addRepliesToComments(comment, replies);
  106. return (
  107. <div key={commentId}>
  108. <Comment
  109. comment={comment}
  110. deleteBtnClicked={this.confirmToDeleteComment}
  111. crowiRenderer={this.growiRenderer}
  112. crowi={this.props.crowi}
  113. replyTo={undefined}
  114. replyList={replyList}
  115. revisionCreatedAt={this.props.revisionCreatedAt}
  116. revisionId={this.props.revisionId}
  117. />
  118. <div className="container-fluid">
  119. <div className="row">
  120. <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">
  121. { !showEditor && (
  122. <div className="form page-comment-form">
  123. { username
  124. && (
  125. <div className="comment-form">
  126. { isLayoutTypeGrowi
  127. && (
  128. <div className="comment-form-user">
  129. <UserPicture user={user} />
  130. </div>
  131. )
  132. }
  133. <div className="comment-form-main">
  134. <button
  135. type="button"
  136. className={`btn btn-lg ${this.state.isLayoutTypeGrowi ? 'btn-link' : 'btn-primary'} center-block`}
  137. onClick={() => { return this.replyButtonClickedHandler(commentId) }}
  138. >
  139. <i className="icon-bubble"></i> Reply
  140. </button>
  141. </div>
  142. </div>
  143. )
  144. }
  145. </div>
  146. )}
  147. { showEditor && (
  148. <CommentEditor
  149. crowi={this.props.crowi}
  150. crowiOriginRenderer={this.props.crowiOriginRenderer}
  151. editorOptions={this.props.editorOptions}
  152. slackChannels={this.props.slackChannels}
  153. replyTo={commentId}
  154. />
  155. )}
  156. </div>
  157. </div>
  158. </div>
  159. </div>
  160. );
  161. });
  162. }
  163. render() {
  164. const currentComments = [];
  165. const currentReplies = [];
  166. let comments = this.props.commentContainer.state.comments;
  167. if (this.state.isLayoutTypeGrowi) {
  168. // replace with asc order array
  169. comments = comments.slice().reverse(); // non-destructive reverse
  170. }
  171. comments.forEach((comment) => {
  172. if (comment.replyTo === undefined) {
  173. // comment is not a reply
  174. currentComments.push(comment);
  175. }
  176. else {
  177. // comment is a reply
  178. currentReplies.push(comment);
  179. }
  180. });
  181. // generate elements
  182. const currentElements = this.generateCommentElements(currentComments, currentReplies);
  183. // generate blocks
  184. const currentBlock = (
  185. <div className="page-comments-list-current" id="page-comments-list-current">
  186. {currentElements}
  187. </div>
  188. );
  189. // layout blocks
  190. const commentsElements = (<div>{currentBlock}</div>);
  191. return (
  192. <div>
  193. {commentsElements}
  194. <DeleteCommentModal
  195. isShown={this.state.isDeleteConfirmModalShown}
  196. comment={this.state.commentToDelete}
  197. errorMessage={this.state.errorMessageForDeleting}
  198. cancel={this.closeDeleteConfirmModal}
  199. confirmedToDelete={this.deleteComment}
  200. />
  201. </div>
  202. );
  203. }
  204. }
  205. /**
  206. * Wrapper component for using unstated
  207. */
  208. class PageCommentsWrapper extends React.Component {
  209. render() {
  210. return (
  211. <Subscribe to={[CommentContainer]}>
  212. { commentContainer => (
  213. // eslint-disable-next-line arrow-body-style
  214. <PageComments commentContainer={commentContainer} {...this.props} />
  215. )}
  216. </Subscribe>
  217. );
  218. }
  219. }
  220. PageCommentsWrapper.propTypes = {
  221. crowi: PropTypes.object.isRequired,
  222. crowiOriginRenderer: PropTypes.object.isRequired,
  223. pageId: PropTypes.string.isRequired,
  224. revisionId: PropTypes.string.isRequired,
  225. revisionCreatedAt: PropTypes.number,
  226. pagePath: PropTypes.string,
  227. editorOptions: PropTypes.object,
  228. slackChannels: PropTypes.string,
  229. };
  230. PageComments.propTypes = {
  231. commentContainer: PropTypes.object.isRequired,
  232. crowi: PropTypes.object.isRequired,
  233. crowiOriginRenderer: PropTypes.object.isRequired,
  234. pageId: PropTypes.string.isRequired,
  235. revisionId: PropTypes.string.isRequired,
  236. revisionCreatedAt: PropTypes.number,
  237. pagePath: PropTypes.string,
  238. editorOptions: PropTypes.object,
  239. slackChannels: PropTypes.string,
  240. };
  241. export default withTranslation(null, { withRef: true })(PageCommentsWrapper);