PageComments.js 3.1 KB

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