PageComments.js 8.1 KB

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