Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

PaginationComponentRQ.tsx 3.4KB

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