Next.js template
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

PaginationComponentSWR.jsx 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { Box, Button, Grid, Paper, Typography } from '@mui/material';
  2. import { useTranslation } from 'next-i18next';
  3. import { useState } from 'react';
  4. import useDebounce from '../../../hooks/use-debounce';
  5. import useSWRWithFallbackData from '../../../hooks/use-swr-with-initial-data';
  6. import { getData } from '../../../requests/dataRequest';
  7. import { compare } from '../../../utils/helpers/sortHelpers';
  8. import DataCard from '../../cards/data-card/DataCard';
  9. import FilterSortComponent from '../filter-sort/FilterSortComponent';
  10. const PaginationComponent = ({ initialData = {} }) => {
  11. const [pageIndex, setPageIndex] = useState(1);
  12. const [filter, setFilter] = useState('');
  13. const [sort, setSort] = useState('');
  14. const { t } = useTranslation('pagination');
  15. const fetcher = (page) => getData(page);
  16. const { data: paginationData } = useSWRWithFallbackData(pageIndex, fetcher, {
  17. fallbackData: initialData,
  18. });
  19. const debouncedFilter = useDebounce(filter, 500);
  20. const handleFilterChange = (event) => {
  21. const filterText = event.target.value;
  22. setFilter(filterText);
  23. };
  24. const handleSortChange = (event) => {
  25. const sort = event.target.value;
  26. setSort(sort);
  27. };
  28. const dataToDisplay = paginationData?.data
  29. .filter((item) =>
  30. item.name.toLowerCase().startsWith(debouncedFilter.toLowerCase())
  31. )
  32. .sort((a, b) => compare(a.name, b.name, sort))
  33. .map((item, index) => (
  34. // ! DON'T USE index for key, this is for example only
  35. <Grid item sx={{ p: 2 }} xs={12} sm={6} md={4} lg={3} key={index}>
  36. <DataCard data={item} t={t} />
  37. </Grid>
  38. ));
  39. return (
  40. <Paper
  41. sx={{
  42. display: 'flex',
  43. flexDirection: 'column',
  44. justifyContent: 'start',
  45. py: 2,
  46. minHeight: 400,
  47. marginTop: 5,
  48. }}
  49. elevation={5}
  50. >
  51. <Typography sx={{ my: 4 }} variant="h4" gutterBottom align="center">
  52. {t('Title')}
  53. </Typography>
  54. <Box
  55. sx={{
  56. display: 'flex',
  57. justifyContent: 'space-between',
  58. flexWrap: 'wrap',
  59. mx: 2,
  60. }}
  61. >
  62. <Box
  63. sx={{
  64. display: 'flex',
  65. justifyContent: 'space-between',
  66. width: '100%',
  67. }}
  68. >
  69. <FilterSortComponent
  70. sort={sort}
  71. handleSortChange={handleSortChange}
  72. filter={filter}
  73. handleFilterChange={handleFilterChange}
  74. />
  75. </Box>
  76. </Box>
  77. <Grid container>{dataToDisplay}</Grid>
  78. <Box
  79. sx={{
  80. width: '100%',
  81. textAlign: 'center',
  82. marginTop: 3,
  83. }}
  84. >
  85. <Button
  86. disabled={pageIndex === 1}
  87. onClick={() => setPageIndex(pageIndex - 1)}
  88. sx={{
  89. marginRight: 5,
  90. }}
  91. >
  92. {t('Btns.PrevBtn')}
  93. </Button>
  94. <Button
  95. disabled={pageIndex * 4 > paginationData?.dataCount}
  96. onClick={() => setPageIndex(pageIndex + 1)}
  97. sx={{
  98. marginRight: 5,
  99. }}
  100. >
  101. {t('Btns.NextBtn')}
  102. </Button>
  103. </Box>
  104. </Paper>
  105. );
  106. };
  107. export default PaginationComponent;