MyBookmarkList.jsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. import loggerFactory from '@alias/logger';
  5. import { withUnstatedContainers } from '../UnstatedUtils';
  6. import AppContainer from '../../services/AppContainer';
  7. import PageContainer from '../../services/PageContainer';
  8. import { toastError } from '../../util/apiNotification';
  9. import PaginationWrapper from '../PaginationWrapper';
  10. import Page from '../PageList/Page';
  11. const logger = loggerFactory('growi:MyBookmarkList');
  12. class MyBookmarkList extends React.Component {
  13. constructor(props) {
  14. super(props);
  15. this.state = {
  16. pages: [],
  17. activePage: 1,
  18. totalPages: 0,
  19. pagingLimit: null,
  20. };
  21. this.handlePage = this.handlePage.bind(this);
  22. }
  23. componentWillMount() {
  24. this.getMyBookmarkList(1);
  25. }
  26. async handlePage(selectPageNumber) {
  27. await this.getMyBookmarkList(selectPageNumber);
  28. }
  29. async getMyBookmarkList(selectPageNumber) {
  30. const { appContainer } = this.props;
  31. const userId = appContainer.currentUserId;
  32. const page = selectPageNumber;
  33. try {
  34. const { data } = await this.props.appContainer.apiv3.get(`/bookmarks/${userId}`, { page });
  35. if (data.paginationResult == null) {
  36. throw new Error('data must conclude \'paginateResult\' property.');
  37. }
  38. const {
  39. docs: pages, totalDocs: totalPages, limit: pagingLimit, page: activePage,
  40. } = data.paginationResult;
  41. this.setState({
  42. pages,
  43. totalPages,
  44. pagingLimit,
  45. activePage,
  46. });
  47. }
  48. catch (error) {
  49. logger.error('failed to fetch data', error);
  50. toastError(error, 'Error occurred in bookmark page list');
  51. }
  52. }
  53. /**
  54. * generate Elements of Page
  55. *
  56. * @param {any} pages Array of pages Model Obj
  57. *
  58. */
  59. generatePageList(pages) {
  60. return pages.map(page => (
  61. <li key={`my-bookmarks:${page._id}`}>
  62. <Page page={page.page} />
  63. </li>
  64. ));
  65. }
  66. renderNoBookmarkList() {
  67. const { t } = this.props;
  68. return t('No bookmarks yet');
  69. }
  70. renderBookmarkList() {
  71. console.log(`activePage2 = ${this.state.activePage}`);
  72. console.log(`totalItemsCount = ${this.state.totalItemsCount}`);
  73. console.log(`pagingLimit = ${this.state.pagingLimit}`);
  74. console.log(`changePage = ${this.state.changePage}`);
  75. return (
  76. <>
  77. <ul className="page-list-ul page-list-ul-flat mb-3">
  78. {this.generatePageList(this.state.pages)}
  79. </ul>
  80. <PaginationWrapper
  81. activePage={this.state.activePage}
  82. changePage={this.handlePage}
  83. totalItemsCount={this.state.totalPages}
  84. pagingLimit={this.state.pagingLimit}
  85. />
  86. </>
  87. );
  88. }
  89. render() {
  90. console.log(this.state.totalPages);
  91. console.log(`activePage1 = ${this.state.activePage}`);
  92. return (
  93. <>
  94. <div className="page-list-container-create">
  95. {this.state.totalPages === 0 ? this.renderNoBookmarkList() : this.renderBookmarkList()}
  96. </div>
  97. </>
  98. );
  99. }
  100. }
  101. /**
  102. * Wrapper component for using unstated
  103. */
  104. const MyBookmarkListWrapper = withUnstatedContainers(MyBookmarkList, [AppContainer, PageContainer]);
  105. MyBookmarkList.propTypes = {
  106. t: PropTypes.func.isRequired,
  107. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  108. pageContainer: PropTypes.instanceOf(PageContainer).isRequired,
  109. };
  110. export default withTranslation()(MyBookmarkListWrapper);