PageComments.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. return (
  61. <div>
  62. <div>{currentElements}</div>
  63. <div>{newerElements}</div>
  64. <div>{olderElements}</div>
  65. </div>
  66. );
  67. }
  68. }
  69. PageComments.propTypes = {
  70. pageId: PropTypes.string,
  71. revisionId: PropTypes.string,
  72. revisionCreatedAt: PropTypes.number,
  73. crowi: PropTypes.object.isRequired,
  74. };