PageComments.js 9.3 KB

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