PageHistory.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. let diffOpened = {};
  24. const lastId = rev.length - 1;
  25. res.revisions.map((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: 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. }).catch(err => {
  52. // do nothing
  53. });
  54. }
  55. getPreviousRevision(currentRevision) {
  56. let cursor = null;
  57. for (let revision of this.state.revisions) {
  58. if (cursor && cursor._id == currentRevision._id) {
  59. cursor = revision;
  60. break;
  61. }
  62. cursor = revision;
  63. }
  64. return cursor;
  65. }
  66. onDiffOpenClicked(revision) {
  67. const diffOpened = this.state.diffOpened,
  68. revisionId = revision._id;
  69. diffOpened[revisionId] = !(diffOpened[revisionId]);
  70. this.setState({
  71. diffOpened
  72. });
  73. this.fetchPageRevisionBody(revision);
  74. this.fetchPageRevisionBody(this.getPreviousRevision(revision));
  75. }
  76. fetchPageRevisionBody(revision) {
  77. if (revision.body) {
  78. return ;
  79. }
  80. this.props.crowi.apiGet('/revisions.get', {revision_id: revision._id})
  81. .then(res => {
  82. if (res.ok) {
  83. this.setState({
  84. revisions: this.state.revisions.map((rev) => {
  85. if (rev._id == res.revision._id) {
  86. return res.revision;
  87. }
  88. return rev;
  89. })
  90. });
  91. }
  92. }).catch(err => {
  93. });
  94. }
  95. render() {
  96. return (
  97. <div>
  98. <PageRevisionList
  99. t={this.props.t}
  100. revisions={this.state.revisions}
  101. diffOpened={this.state.diffOpened}
  102. getPreviousRevision={this.getPreviousRevision}
  103. onDiffOpenClicked={this.onDiffOpenClicked}
  104. />
  105. </div>
  106. );
  107. }
  108. }
  109. PageHistory.propTypes = {
  110. t: PropTypes.func.isRequired, // i18next
  111. pageId: PropTypes.string,
  112. crowi: PropTypes.object.isRequired,
  113. };
  114. export default translate()(PageHistory);