Next.js template
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

PaginationComponentRQ.jsx 2.9KB

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