Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

PaginationComponentSWR.tsx 3.3KB

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