PageHistoryContainer.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import { Container } from 'unstated';
  2. import loggerFactory from '@alias/logger';
  3. import { toastError } from '../util/apiNotification';
  4. const logger = loggerFactory('growi:PageHistoryContainer');
  5. /**
  6. * Service container for personal settings page (PageHistory.jsx)
  7. * @extends {Container} unstated Container
  8. */
  9. export default class PageHistoryContainer extends Container {
  10. constructor(appContainer, pageContainer) {
  11. super();
  12. this.appContainer = appContainer;
  13. this.pageContainer = pageContainer;
  14. this.dummyRevisions = 0;
  15. this.state = {
  16. errorMessage: null,
  17. // set dummy rivisions for using suspense
  18. revisions: this.dummyRevisions,
  19. diffOpened: {},
  20. totalPages: 0,
  21. activePage: 1,
  22. pagingLimit: null,
  23. };
  24. this.retrieveRevisions = this.retrieveRevisions.bind(this);
  25. this.onDiffOpenClicked = this.onDiffOpenClicked.bind(this);
  26. this.getPreviousRevision = this.getPreviousRevision.bind(this);
  27. this.fetchPageRevisionBody = this.fetchPageRevisionBody.bind(this);
  28. }
  29. /**
  30. * Workaround for the mangling in production build to break constructor.name
  31. */
  32. static getClassName() {
  33. return 'PageHistoryContainer';
  34. }
  35. /**
  36. * syncRevisions of selectedPage
  37. * @param {number} selectedPage
  38. */
  39. async retrieveRevisions(selectedPage) {
  40. const { pageId, shareLinkId } = this.pageContainer.state;
  41. const page = selectedPage;
  42. if (!pageId) {
  43. return;
  44. }
  45. const res = await this.appContainer.apiv3Get('/revisions/list', {
  46. pageId, shareLinkId, page,
  47. });
  48. const rev = res.data.docs;
  49. // set Pagination state
  50. this.setState({
  51. activePage: selectedPage,
  52. totalPages: res.data.totalDocs,
  53. pagingLimit: res.data.limit,
  54. });
  55. const diffOpened = {};
  56. const lastId = rev.length - 1;
  57. res.data.docs.forEach((revision, i) => {
  58. const user = revision.author;
  59. if (user) {
  60. rev[i].author = user;
  61. }
  62. if (i === 0 || i === lastId) {
  63. diffOpened[revision._id] = true;
  64. }
  65. else {
  66. diffOpened[revision._id] = false;
  67. }
  68. });
  69. this.setState({ revisions: rev });
  70. this.setState({ diffOpened });
  71. // load 0, and last default
  72. if (rev[0]) {
  73. this.fetchPageRevisionBody(rev[0]);
  74. }
  75. if (rev[1]) {
  76. this.fetchPageRevisionBody(rev[1]);
  77. }
  78. if (lastId !== 0 && lastId !== 1 && rev[lastId]) {
  79. this.fetchPageRevisionBody(rev[lastId]);
  80. }
  81. return;
  82. }
  83. onDiffOpenClicked(revision) {
  84. const { diffOpened } = this.state;
  85. const revisionId = revision._id;
  86. diffOpened[revisionId] = !(diffOpened[revisionId]);
  87. this.setState(diffOpened);
  88. this.fetchPageRevisionBody(revision);
  89. this.fetchPageRevisionBody(this.getPreviousRevision(revision));
  90. }
  91. getPreviousRevision(currentRevision) {
  92. let cursor = null;
  93. for (const revision of this.state.revisions) {
  94. // comparing ObjectId
  95. // eslint-disable-next-line eqeqeq
  96. if (cursor && cursor._id == currentRevision._id) {
  97. cursor = revision;
  98. break;
  99. }
  100. cursor = revision;
  101. }
  102. return cursor;
  103. }
  104. /**
  105. * fetch page revision body by revision in argument
  106. * @param {object} revision
  107. */
  108. async fetchPageRevisionBody(revision) {
  109. const { pageId, shareLinkId } = this.pageContainer.state;
  110. if (revision.body) {
  111. return;
  112. }
  113. try {
  114. const res = await this.appContainer.apiv3Get(`/revisions/${revision._id}`, { pageId, shareLinkId });
  115. this.setState({
  116. revisions: this.state.revisions.map((rev) => {
  117. // comparing ObjectId
  118. // eslint-disable-next-line eqeqeq
  119. if (rev._id == res.data.revision._id) {
  120. return res.data.revision;
  121. }
  122. return rev;
  123. }),
  124. });
  125. }
  126. catch (err) {
  127. toastError(err);
  128. this.setState({ errorMessage: err.message });
  129. logger.error(err);
  130. }
  131. }
  132. }