PageHistoryContainer.js 3.9 KB

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