PageHistory.js 3.0 KB

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