PageHistoryContainer.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. latestRevision: this.dummyRevisions,
  20. diffOpened: {},
  21. totalPages: 0,
  22. activePage: 1,
  23. pagingLimit: 10,
  24. };
  25. this.retrieveRevisions = this.retrieveRevisions.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. if (selectedPage === 1) {
  79. this.setState({ latestRevision: rev[0] });
  80. }
  81. // load 0, and last default
  82. if (rev[0]) {
  83. this.fetchPageRevisionBody(rev[0]);
  84. }
  85. if (rev[1]) {
  86. this.fetchPageRevisionBody(rev[1]);
  87. }
  88. if (lastId !== 0 && lastId !== 1 && rev[lastId]) {
  89. this.fetchPageRevisionBody(rev[lastId]);
  90. }
  91. return;
  92. }
  93. getPreviousRevision(currentRevision) {
  94. let cursor = null;
  95. for (const revision of this.state.revisions) {
  96. // comparing ObjectId
  97. // eslint-disable-next-line eqeqeq
  98. if (cursor && cursor._id == currentRevision._id) {
  99. cursor = revision;
  100. break;
  101. }
  102. cursor = revision;
  103. }
  104. return cursor;
  105. }
  106. /**
  107. * fetch page revision body by revision in argument
  108. * @param {object} revision
  109. */
  110. async fetchPageRevisionBody(revision) {
  111. const { pageId, shareLinkId } = this.pageContainer.state;
  112. if (revision.body) {
  113. return;
  114. }
  115. try {
  116. const res = await this.appContainer.apiv3Get(`/revisions/${revision._id}`, { pageId, shareLinkId });
  117. this.setState({
  118. revisions: this.state.revisions.map((rev) => {
  119. // comparing ObjectId
  120. // eslint-disable-next-line eqeqeq
  121. if (rev._id == res.data.revision._id) {
  122. return res.data.revision;
  123. }
  124. return rev;
  125. }),
  126. });
  127. }
  128. catch (err) {
  129. toastError(err);
  130. this.setState({ errorMessage: err.message });
  131. logger.error(err);
  132. }
  133. }
  134. }