PageHistory.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { translate } from 'react-i18next';
  4. import PageRevisionList from './PageHistory/PageRevisionList';
  5. class PageHistory extends React.Component {
  6. constructor(props) {
  7. super(props);
  8. this.state = {
  9. revisions: [],
  10. diffOpened: {},
  11. };
  12. this.getPreviousRevision = this.getPreviousRevision.bind(this);
  13. this.onDiffOpenClicked = this.onDiffOpenClicked.bind(this);
  14. }
  15. componentDidMount() {
  16. const pageId = this.props.pageId;
  17. if (!pageId) {
  18. return;
  19. }
  20. this.props.crowi.apiGet('/revisions.ids', { page_id: pageId })
  21. .then((res) => {
  22. const rev = res.revisions;
  23. const diffOpened = {};
  24. const lastId = rev.length - 1;
  25. res.revisions.forEach((revision, i) => {
  26. const user = this.props.crowi.findUserById(revision.author);
  27. if (user) {
  28. rev[i].author = user;
  29. }
  30. if (i === 0 || i === lastId) {
  31. diffOpened[revision._id] = true;
  32. }
  33. else {
  34. diffOpened[revision._id] = false;
  35. }
  36. });
  37. this.setState({
  38. revisions: rev,
  39. diffOpened,
  40. });
  41. // load 0, and last default
  42. if (rev[0]) {
  43. this.fetchPageRevisionBody(rev[0]);
  44. }
  45. if (rev[1]) {
  46. this.fetchPageRevisionBody(rev[1]);
  47. }
  48. if (lastId !== 0 && lastId !== 1 && rev[lastId]) {
  49. this.fetchPageRevisionBody(rev[lastId]);
  50. }
  51. })
  52. .catch((err) => {
  53. // do nothing
  54. });
  55. }
  56. getPreviousRevision(currentRevision) {
  57. let cursor = null;
  58. for (const revision of this.state.revisions) {
  59. // comparing ObjectId
  60. // eslint-disable-next-line eqeqeq
  61. if (cursor && cursor._id == currentRevision._id) {
  62. cursor = revision;
  63. break;
  64. }
  65. cursor = revision;
  66. }
  67. return cursor;
  68. }
  69. onDiffOpenClicked(revision) {
  70. const diffOpened = this.state.diffOpened;
  71. const revisionId = revision._id;
  72. diffOpened[revisionId] = !(diffOpened[revisionId]);
  73. this.setState({
  74. diffOpened,
  75. });
  76. this.fetchPageRevisionBody(revision);
  77. this.fetchPageRevisionBody(this.getPreviousRevision(revision));
  78. }
  79. fetchPageRevisionBody(revision) {
  80. if (revision.body) {
  81. return;
  82. }
  83. this.props.crowi.apiGet('/revisions.get',
  84. { page_id: this.props.pageId, revision_id: revision._id })
  85. .then((res) => {
  86. if (res.ok) {
  87. this.setState({
  88. revisions: this.state.revisions.map((rev) => {
  89. // comparing ObjectId
  90. // eslint-disable-next-line eqeqeq
  91. if (rev._id == res.revision._id) {
  92. return res.revision;
  93. }
  94. return rev;
  95. }),
  96. });
  97. }
  98. })
  99. .catch((err) => {
  100. });
  101. }
  102. render() {
  103. return (
  104. <div>
  105. <PageRevisionList
  106. t={this.props.t}
  107. revisions={this.state.revisions}
  108. diffOpened={this.state.diffOpened}
  109. getPreviousRevision={this.getPreviousRevision}
  110. onDiffOpenClicked={this.onDiffOpenClicked}
  111. />
  112. </div>
  113. );
  114. }
  115. }
  116. PageHistory.propTypes = {
  117. t: PropTypes.func.isRequired, // i18next
  118. pageId: PropTypes.string,
  119. crowi: PropTypes.object.isRequired,
  120. };
  121. export default translate()(PageHistory);