RecentCreated.jsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withUnstatedContainers } from '../UnstatedUtils';
  4. import AppContainer from '../../services/AppContainer';
  5. import PaginationWrapper from '../PaginationWrapper';
  6. import Page from '../PageList/Page';
  7. class RecentCreated extends React.Component {
  8. constructor(props) {
  9. super(props);
  10. this.state = {
  11. pages: [],
  12. activePage: 1,
  13. totalPages: 0,
  14. pagingLimit: null,
  15. };
  16. this.handlePage = this.handlePage.bind(this);
  17. }
  18. componentWillMount() {
  19. this.getRecentCreatedList(1);
  20. }
  21. async handlePage(selectedPage) {
  22. await this.getRecentCreatedList(selectedPage);
  23. }
  24. async getRecentCreatedList(selectedPage) {
  25. const { appContainer, userId } = this.props;
  26. const page = selectedPage;
  27. // const userId = appContainer.currentUserId;
  28. // pagesList get and pagination calculate
  29. const res = await appContainer.apiv3Get(`/users/${userId}/recent`, { page });
  30. const { totalCount, pages, limit } = res.data;
  31. this.setState({
  32. pages,
  33. activePage: selectedPage,
  34. totalPages: totalCount,
  35. pagingLimit: limit,
  36. });
  37. }
  38. /**
  39. * generate Elements of Page
  40. *
  41. * @param {any} pages Array of pages Model Obj
  42. *
  43. */
  44. generatePageList(pages) {
  45. return pages.map(page => (
  46. <li key={`recent-created:list-view:${page._id}`}>
  47. <Page page={page} />
  48. </li>
  49. ));
  50. }
  51. render() {
  52. const pageList = this.generatePageList(this.state.pages);
  53. return (
  54. <div className="page-list-container-create">
  55. <ul className="page-list-ul page-list-ul-flat mb-3">
  56. {pageList}
  57. </ul>
  58. <PaginationWrapper
  59. activePage={this.state.activePage}
  60. changePage={this.handlePage}
  61. totalItemsCount={this.state.totalPages}
  62. pagingLimit={this.state.pagingLimit}
  63. />
  64. </div>
  65. );
  66. }
  67. }
  68. /**
  69. * Wrapper component for using unstated
  70. */
  71. const RecentCreatedWrapper = withUnstatedContainers(RecentCreated, [AppContainer]);
  72. RecentCreated.propTypes = {
  73. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  74. userId: PropTypes.string.isRequired,
  75. };
  76. export default RecentCreatedWrapper;