UserGroupPageList.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import React, { useEffect, useState, useCallback } from 'react';
  2. import { useTranslation } from 'next-i18next';
  3. import { apiv3Get } from '~/client/util/apiv3-client';
  4. import { toastError } from '~/client/util/toastr';
  5. import { IPageHasId } from '~/interfaces/page';
  6. import { PageListItemS } from '../../PageList/PageListItemS';
  7. import PaginationWrapper from '../../PaginationWrapper';
  8. const pagingLimit = 10;
  9. type Props = {
  10. userGroupId: string,
  11. relatedPages?: IPageHasId[],
  12. }
  13. const UserGroupPageList = (props: Props): JSX.Element => {
  14. const { t } = useTranslation('admin');
  15. const { userGroupId, relatedPages } = props;
  16. const [currentPages, setCurrentPages] = useState<IPageHasId[]>([]);
  17. const [activePage, setActivePage] = useState(1);
  18. const [total, setTotal] = useState(0);
  19. const handlePageChange = useCallback(async(pageNum) => {
  20. const offset = (pageNum - 1) * pagingLimit;
  21. try {
  22. const res = await apiv3Get(`/user-groups/${userGroupId}/pages`, {
  23. limit: pagingLimit,
  24. offset,
  25. });
  26. const { total, pages } = res.data;
  27. setTotal(total);
  28. setActivePage(pageNum);
  29. setCurrentPages(pages);
  30. }
  31. catch (err) {
  32. toastError(err);
  33. }
  34. }, [userGroupId]);
  35. useEffect(() => {
  36. handlePageChange(activePage);
  37. }, [activePage, handlePageChange]);
  38. return (
  39. <>
  40. <ul className="page-list-ul page-list-ul-flat mt-3 mb-3">
  41. { currentPages.map(page => (
  42. <li key={page._id} className="mt-2">
  43. <PageListItemS page={page} />
  44. </li>
  45. )) }
  46. </ul>
  47. {relatedPages != null && relatedPages.length === 0 ? <p>{t('user_group_management.no_pages')}</p> : (
  48. <PaginationWrapper
  49. activePage={activePage}
  50. changePage={handlePageChange}
  51. totalItemsCount={total}
  52. pagingLimit={pagingLimit}
  53. align="center"
  54. size="sm"
  55. />
  56. )}
  57. </>
  58. );
  59. };
  60. export default UserGroupPageList;