PageComments.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import Comment from './PageComment/Comment';
  4. export default class PageComments extends React.Component {
  5. constructor(props) {
  6. super(props);
  7. this.state = {
  8. currentComments: [],
  9. newerComments: [],
  10. olderComments: [],
  11. };
  12. this.init = this.init.bind(this);
  13. }
  14. componentWillMount() {
  15. const pageId = this.props.pageId;
  16. if (pageId) {
  17. this.init();
  18. }
  19. }
  20. init() {
  21. if (!this.props.pageId) {
  22. return ;
  23. }
  24. const pageId = this.props.pageId;
  25. const revisionId = this.props.revisionId;
  26. const revisionCreatedAt = this.props.revisionCreatedAt;
  27. this.props.crowi.apiGet('/comments.get', {page_id: pageId})
  28. .then(res => {
  29. if (res.ok) {
  30. let currentComments = [];
  31. let newerComments = [];
  32. let olderComments = [];
  33. // divide by revisionId and createdAt
  34. res.comments.forEach((comment) => {
  35. if (comment.revision == revisionId) {
  36. currentComments.push(comment);
  37. }
  38. else if (Date.parse(comment.createdAt)/1000 > revisionCreatedAt) {
  39. newerComments.push(comment);
  40. }
  41. else {
  42. olderComments.push(comment);
  43. }
  44. });
  45. this.setState({currentComments, newerComments, olderComments});
  46. }
  47. }).catch(err => {
  48. });
  49. }
  50. /**
  51. * generate Elements of Comment
  52. *
  53. * @param {any} comments Array of Comment Model Obj
  54. *
  55. * @memberOf PageComments
  56. */
  57. generateCommentElements(comments) {
  58. return comments.map((comment) => {
  59. return (
  60. <Comment key={comment._id} comment={comment}
  61. currentUserId={this.props.crowi.me}
  62. currentRevisionId={this.props.revisionId} />
  63. );
  64. });
  65. }
  66. render() {
  67. let currentElements = this.generateCommentElements(this.state.currentComments);
  68. let newerElements = this.generateCommentElements(this.state.newerComments);
  69. let olderElements = this.generateCommentElements(this.state.olderComments);
  70. let toggleNewer = (newerElements.length === 0)
  71. ? <div></div>
  72. : (
  73. <a className="page-comments-list-toggle-newer text-center" data-toggle="collapse" href="#page-comments-list-newer">
  74. <i className="fa fa-angle-double-up"></i> Comments for Newer Revision <i className="fa fa-angle-double-up"></i>
  75. </a>
  76. )
  77. let toggleOlder = (olderElements.length === 0)
  78. ? <div></div>
  79. : (
  80. <a className="page-comments-list-toggle-older text-center" data-toggle="collapse" href="#page-comments-list-older">
  81. <i className="fa fa-angle-double-down"></i> Comments for Older Revision <i className="fa fa-angle-double-down"></i>
  82. </a>
  83. )
  84. return (
  85. <div>
  86. <div className="page-comments-list-newer collapse" id="page-comments-list-newer">
  87. {newerElements}
  88. </div>
  89. {toggleNewer}
  90. <div className="page-comments-list-current" id="page-comments-list-current">
  91. {currentElements}
  92. </div>
  93. {toggleOlder}
  94. <div className="page-comments-list-older collapse in" id="page-comments-list-older">
  95. {olderElements}
  96. </div>
  97. </div>
  98. );
  99. }
  100. }
  101. PageComments.propTypes = {
  102. pageId: PropTypes.string,
  103. revisionId: PropTypes.string,
  104. revisionCreatedAt: PropTypes.number,
  105. crowi: PropTypes.object.isRequired,
  106. };