UserGroupPageList.tsx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 mb-3">
  41. {currentPages.map(page => <li key={page._id}><PageListItemS page={page} /></li>)}
  42. </ul>
  43. {relatedPages != null && relatedPages.length === 0 ? <p>{t('user_group_management.no_pages')}</p> : (
  44. <PaginationWrapper
  45. activePage={activePage}
  46. changePage={handlePageChange}
  47. totalItemsCount={total}
  48. pagingLimit={pagingLimit}
  49. align="center"
  50. size="sm"
  51. />
  52. )}
  53. </>
  54. );
  55. };
  56. export default UserGroupPageList;