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 Page from '../PageList/Page';
  6. import PaginationWrapper from '../PaginationWrapper';
  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: 10,
  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. // pagesList get and pagination calculate
  28. const res = await appContainer.apiv3Get(`/users/${userId}/recent`, { page });
  29. const { totalCount, pages, limit } = res.data;
  30. this.setState({
  31. pages,
  32. activePage: selectedPage,
  33. totalPages: totalCount,
  34. pagingLimit: limit,
  35. });
  36. }
  37. /**
  38. * generate Elements of Page
  39. *
  40. * @param {any} pages Array of pages Model Obj
  41. *
  42. */
  43. generatePageList(pages) {
  44. return pages.map(page => (
  45. <li key={`recent-created:list-view:${page._id}`} className="mt-4">
  46. <Page page={page} />
  47. </li>
  48. ));
  49. }
  50. render() {
  51. const pageList = this.generatePageList(this.state.pages);
  52. return (
  53. <div className="page-list-container-create">
  54. <ul className="page-list-ul page-list-ul-flat mb-3">
  55. {pageList}
  56. </ul>
  57. <PaginationWrapper
  58. align="center"
  59. activePage={this.state.activePage}
  60. changePage={this.handlePage}
  61. totalItemsCount={this.state.totalPages}
  62. pagingLimit={this.state.pagingLimit}
  63. size="sm"
  64. />
  65. </div>
  66. );
  67. }
  68. }
  69. /**
  70. * Wrapper component for using unstated
  71. */
  72. const RecentCreatedWrapper = withUnstatedContainers(RecentCreated, [AppContainer]);
  73. RecentCreated.propTypes = {
  74. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  75. userId: PropTypes.string.isRequired,
  76. };
  77. export default RecentCreatedWrapper;