PageComments.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /* eslint-disable react/no-access-state-in-setstate */
  2. import React from 'react';
  3. import PropTypes from 'prop-types';
  4. import { withTranslation } from 'react-i18next';
  5. import GrowiRenderer from '../util/GrowiRenderer';
  6. import CommentForm from './PageComment/CommentForm';
  7. import CommentEditor from './PageComment/CommentEditor';
  8. import Comment from './PageComment/Comment';
  9. import DeleteCommentModal from './PageComment/DeleteCommentModal';
  10. /**
  11. * Load data of comments and render the list of <Comment />
  12. *
  13. * @author Yuki Takei <yuki@weseek.co.jp>
  14. *
  15. * @export
  16. * @class PageComments
  17. * @extends {React.Component}
  18. */
  19. class PageComments extends React.Component {
  20. constructor(props) {
  21. super(props);
  22. this.state = {
  23. // desc order array
  24. comments: [],
  25. children: {},
  26. isLayoutTypeGrowi: false,
  27. // for deleting comment
  28. commentToDelete: undefined,
  29. isDeleteConfirmModalShown: false,
  30. errorMessageForDeleting: undefined,
  31. };
  32. this.growiRenderer = new GrowiRenderer(this.props.crowi, this.props.crowiOriginRenderer, { mode: '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.replyToComment = this.replyToComment.bind(this);
  39. }
  40. componentWillMount() {
  41. this.init();
  42. this.retrieveData = this.retrieveData.bind(this);
  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.retrieveData();
  51. }
  52. /**
  53. * Load data of comments and store them in state
  54. */
  55. retrieveData() {
  56. // get data (desc order array)
  57. this.props.crowi.apiGet('/comments.get', { page_id: this.props.pageId })
  58. .then((res) => {
  59. if (res.ok) {
  60. this.setState({ comments: res.comments });
  61. const tempChildren = {};
  62. res.comments.forEach((comment) => {
  63. tempChildren[comment._id] = React.createRef();
  64. });
  65. this.setState({ children: tempChildren });
  66. }
  67. });
  68. }
  69. confirmToDeleteComment(comment) {
  70. this.setState({ commentToDelete: comment });
  71. this.showDeleteConfirmModal();
  72. }
  73. replyToComment(comment) {
  74. this.state.children[comment._id].toggleEditor();
  75. }
  76. deleteComment() {
  77. const comment = this.state.commentToDelete;
  78. this.props.crowi.apiPost('/comments.remove', { comment_id: comment._id })
  79. .then((res) => {
  80. if (res.ok) {
  81. this.findAndSplice(comment);
  82. }
  83. this.closeDeleteConfirmModal();
  84. })
  85. .catch((err) => {
  86. this.setState({ errorMessageForDeleting: err.message });
  87. });
  88. }
  89. findAndSplice(comment) {
  90. const comments = this.state.comments;
  91. const index = comments.indexOf(comment);
  92. if (index < 0) {
  93. return;
  94. }
  95. comments.splice(index, 1);
  96. this.setState({ comments });
  97. }
  98. showDeleteConfirmModal() {
  99. this.setState({ isDeleteConfirmModalShown: true });
  100. }
  101. closeDeleteConfirmModal() {
  102. this.setState({
  103. commentToDelete: undefined,
  104. isDeleteConfirmModalShown: false,
  105. errorMessageForDeleting: undefined,
  106. });
  107. }
  108. // inserts reply after each corresponding comment
  109. reorderBasedOnReplies(comments, replies) {
  110. // const connections = this.findConnections(comments, replies);
  111. // const replyConnections = this.findConnectionsWithinReplies(replies);
  112. const repliesReversed = replies.slice().reverse();
  113. for (let i = 0; i < comments.length; i++) {
  114. for (let j = 0; j < repliesReversed.length; j++) {
  115. if (repliesReversed[j].replyTo === comments[i]._id) {
  116. comments.splice(i + 1, 0, repliesReversed[j]);
  117. }
  118. }
  119. }
  120. return comments;
  121. }
  122. /**
  123. * generate Elements of Comment
  124. *
  125. * @param {any} comments Array of Comment Model Obj
  126. *
  127. * @memberOf PageComments
  128. */
  129. generateCommentElements(comments, replies) {
  130. const commentsWithReplies = this.reorderBasedOnReplies(comments, replies);
  131. return commentsWithReplies.map((comment) => {
  132. return (
  133. <CommentForm
  134. key={comment._id}
  135. onPostComplete={this.retrieveData}
  136. replyTo={comment.replyTo}
  137. pageId={this.props.pageId}
  138. pagePath={this.props.pagePath}
  139. slackChannels={this.props.slackChannels}
  140. crowi={this.props.crowi}
  141. revisionId={this.props.revisionId}
  142. >
  143. <Comment
  144. comment={comment}
  145. deleteBtnClicked={this.confirmToDeleteComment}
  146. crowiRenderer={this.growiRenderer}
  147. onReplyButtonClicked={this.replyToComment}
  148. crowi={this.props.crowi}
  149. />
  150. <CommentEditor
  151. ref={(instance) => { this.state.children[comment._id] = instance }}
  152. editorOptions={this.props.editorOptions}
  153. crowiOriginRenderer={this.props.crowiOriginRenderer}
  154. showCommentEditor={false}
  155. crowi={this.props.crowi}
  156. />
  157. </CommentForm>
  158. );
  159. });
  160. }
  161. render() {
  162. const currentComments = [];
  163. const newerComments = [];
  164. const olderComments = [];
  165. const currentReplies = [];
  166. const newerReplies = [];
  167. const olderReplies = [];
  168. let comments = this.state.comments;
  169. if (this.state.isLayoutTypeGrowi) {
  170. // replace with asc order array
  171. comments = comments.slice().reverse(); // non-destructive reverse
  172. }
  173. // divide by revisionId and createdAt
  174. const revisionId = this.props.revisionId;
  175. const revisionCreatedAt = this.props.revisionCreatedAt;
  176. comments.forEach((comment) => {
  177. // comparing ObjectId
  178. // eslint-disable-next-line eqeqeq
  179. if (comment.replyTo === undefined) {
  180. // comment is not a reply
  181. if (comment.revision === revisionId) {
  182. currentComments.push(comment);
  183. }
  184. else if (Date.parse(comment.createdAt) / 1000 > revisionCreatedAt) {
  185. newerComments.push(comment);
  186. }
  187. else {
  188. olderComments.push(comment);
  189. }
  190. }
  191. else
  192. // comment is a reply
  193. if (comment.revision === revisionId) {
  194. currentReplies.push(comment);
  195. }
  196. else if (Date.parse(comment.createdAt) / 1000 > revisionCreatedAt) {
  197. newerReplies.push(comment);
  198. }
  199. else {
  200. olderReplies.push(comment);
  201. }
  202. });
  203. // generate elements
  204. const currentElements = this.generateCommentElements(currentComments, currentReplies);
  205. const newerElements = this.generateCommentElements(newerComments, newerReplies);
  206. const olderElements = this.generateCommentElements(olderComments, olderReplies);
  207. // generate blocks
  208. const currentBlock = (
  209. <div className="page-comments-list-current" id="page-comments-list-current">
  210. {currentElements}
  211. </div>
  212. );
  213. const newerBlock = (
  214. <div className="page-comments-list-newer collapse in" id="page-comments-list-newer">
  215. {newerElements}
  216. </div>
  217. );
  218. const olderBlock = (
  219. <div className="page-comments-list-older collapse in" id="page-comments-list-older">
  220. {olderElements}
  221. </div>
  222. );
  223. // generate toggle elements
  224. const iconForNewer = (this.state.isLayoutTypeGrowi)
  225. ? <i className="fa fa-angle-double-down"></i>
  226. : <i className="fa fa-angle-double-up"></i>;
  227. const toggleNewer = (newerElements.length === 0)
  228. ? <div></div>
  229. : (
  230. <a className="page-comments-list-toggle-newer text-center" data-toggle="collapse" href="#page-comments-list-newer">
  231. {iconForNewer} Comments for Newer Revision {iconForNewer}
  232. </a>
  233. );
  234. const iconForOlder = (this.state.isLayoutTypeGrowi)
  235. ? <i className="fa fa-angle-double-up"></i>
  236. : <i className="fa fa-angle-double-down"></i>;
  237. const toggleOlder = (olderElements.length === 0)
  238. ? <div></div>
  239. : (
  240. <a className="page-comments-list-toggle-older text-center" data-toggle="collapse" href="#page-comments-list-older">
  241. {iconForOlder} Comments for Older Revision {iconForOlder}
  242. </a>
  243. );
  244. // layout blocks
  245. const commentsElements = (this.state.isLayoutTypeGrowi)
  246. ? (
  247. <div>
  248. {olderBlock}
  249. {toggleOlder}
  250. {currentBlock}
  251. {toggleNewer}
  252. {newerBlock}
  253. </div>
  254. )
  255. : (
  256. <div>
  257. {newerBlock}
  258. {toggleNewer}
  259. {currentBlock}
  260. {toggleOlder}
  261. {olderBlock}
  262. </div>
  263. );
  264. return (
  265. <div>
  266. {commentsElements}
  267. <DeleteCommentModal
  268. isShown={this.state.isDeleteConfirmModalShown}
  269. comment={this.state.commentToDelete}
  270. errorMessage={this.state.errorMessageForDeleting}
  271. cancel={this.closeDeleteConfirmModal}
  272. confirmedToDelete={this.deleteComment}
  273. />
  274. </div>
  275. );
  276. }
  277. }
  278. PageComments.propTypes = {
  279. revisionCreatedAt: PropTypes.number,
  280. pageId: PropTypes.string,
  281. pagePath: PropTypes.string,
  282. editorOptions: PropTypes.object,
  283. slackChannels: PropTypes.string,
  284. crowi: PropTypes.object.isRequired,
  285. crowiOriginRenderer: PropTypes.object.isRequired,
  286. revisionId: PropTypes.string,
  287. };
  288. export default withTranslation(null, { withRef: true })(PageComments);