PageHistoryContainer.js 4.2 KB

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