PageHistoryContainer.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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: 10,
  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 { pagingLimit } = this.state;
  42. const page = selectedPage;
  43. const pagingLimitForApiParam = pagingLimit + 1;
  44. if (!pageId) {
  45. return;
  46. }
  47. // Get one more for the bottom display
  48. const res = await this.appContainer.apiv3Get('/revisions/list', {
  49. pageId, shareLinkId, page, limit: pagingLimitForApiParam,
  50. });
  51. const rev = res.data.docs;
  52. // set Pagination state
  53. this.setState({
  54. activePage: selectedPage,
  55. totalPages: res.data.totalDocs,
  56. pagingLimit,
  57. });
  58. const diffOpened = {};
  59. let lastId = rev.length - 1;
  60. // If the number of rev count is the same, the last rev is for diff display, so exclude it.
  61. if (rev.length > pagingLimit) {
  62. lastId = rev.length - 2;
  63. }
  64. res.data.docs.forEach((revision, i) => {
  65. const user = revision.author;
  66. if (user) {
  67. rev[i].author = user;
  68. }
  69. if (i === 0 || i === lastId) {
  70. diffOpened[revision._id] = true;
  71. }
  72. else {
  73. diffOpened[revision._id] = false;
  74. }
  75. });
  76. this.setState({ revisions: rev });
  77. this.setState({ diffOpened });
  78. // load 0, and last default
  79. if (rev[0]) {
  80. this.fetchPageRevisionBody(rev[0]);
  81. }
  82. if (rev[1]) {
  83. this.fetchPageRevisionBody(rev[1]);
  84. }
  85. if (lastId !== 0 && lastId !== 1 && rev[lastId]) {
  86. this.fetchPageRevisionBody(rev[lastId]);
  87. }
  88. return;
  89. }
  90. onDiffOpenClicked(revision) {
  91. const { diffOpened } = this.state;
  92. const revisionId = revision._id;
  93. diffOpened[revisionId] = !(diffOpened[revisionId]);
  94. this.setState(diffOpened);
  95. this.fetchPageRevisionBody(revision);
  96. this.fetchPageRevisionBody(this.getPreviousRevision(revision));
  97. }
  98. getPreviousRevision(currentRevision) {
  99. let cursor = null;
  100. for (const revision of this.state.revisions) {
  101. // comparing ObjectId
  102. // eslint-disable-next-line eqeqeq
  103. if (cursor && cursor._id == currentRevision._id) {
  104. cursor = revision;
  105. break;
  106. }
  107. cursor = revision;
  108. }
  109. return cursor;
  110. }
  111. /**
  112. * fetch page revision body by revision in argument
  113. * @param {object} revision
  114. */
  115. async fetchPageRevisionBody(revision) {
  116. const { pageId, shareLinkId } = this.pageContainer.state;
  117. if (revision.body) {
  118. return;
  119. }
  120. try {
  121. const res = await this.appContainer.apiv3Get(`/revisions/${revision._id}`, { pageId, shareLinkId });
  122. this.setState({
  123. revisions: this.state.revisions.map((rev) => {
  124. // comparing ObjectId
  125. // eslint-disable-next-line eqeqeq
  126. if (rev._id == res.data.revision._id) {
  127. return res.data.revision;
  128. }
  129. return rev;
  130. }),
  131. });
  132. }
  133. catch (err) {
  134. toastError(err);
  135. this.setState({ errorMessage: err.message });
  136. logger.error(err);
  137. }
  138. }
  139. }