You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProductsGrid.jsx 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { Button, Container, Grid } from '@mui/material';
  2. import { Box } from '@mui/system';
  3. import Image from 'next/image';
  4. import { useMemo, useState } from 'react';
  5. import { useFetchProductsByCategory } from '../../hooks/useFetchProductData';
  6. import { compare } from '../../utils/helpers/sortHelpers';
  7. import ProductCard from '../product-card/ProductCard';
  8. const ProductsGrid = ({
  9. allProducts,
  10. hasNextPage,
  11. productType,
  12. fetchNextPage,
  13. sort,
  14. }) => {
  15. const productsPerPage = 9;
  16. const [next, setNext] = useState(productsPerPage);
  17. const { data: filteredData } = useFetchProductsByCategory(productType);
  18. const allItems = useMemo(
  19. () => allProducts?.pages?.flatMap((page) => page.product),
  20. [allProducts]
  21. );
  22. const dataToDisplay =
  23. productType === 'All' || productType === ''
  24. ? allItems.sort(compare('name', sort)).map((item) => (
  25. <Grid key={item._id} item md={4} sm={6} xs={12} sx={{ mb: '100px' }}>
  26. <ProductCard product={item} />
  27. </Grid>
  28. ))
  29. : filteredData?.productsByCategory
  30. .slice(0, next)
  31. .sort(compare('name', sort))
  32. .map((item) => (
  33. <Grid
  34. key={item._id}
  35. item
  36. md={4}
  37. sm={6}
  38. xs={12}
  39. sx={{ mb: '100px' }}
  40. >
  41. <ProductCard product={item} />
  42. </Grid>
  43. ));
  44. const handleMoreProducts = () => {
  45. setNext(next + productsPerPage);
  46. };
  47. return (
  48. <Container
  49. sx={{
  50. mt: 10,
  51. }}
  52. >
  53. <Grid container spacing={2}>
  54. {dataToDisplay}
  55. </Grid>
  56. <Box textAlign="center" mt={-5} mb={5}>
  57. {hasNextPage && (productType === 'All' || productType === '') && (
  58. <Button
  59. onClick={fetchNextPage}
  60. startIcon={
  61. <Image
  62. src="/images/arrow.svg"
  63. alt="arrow down"
  64. width={29}
  65. height={29}
  66. />
  67. }
  68. sx={{
  69. backgroundColor: 'primary.main',
  70. height: 50,
  71. width: 150,
  72. color: 'white',
  73. ':hover': {
  74. bgcolor: 'primary.main', // theme.palette.primary.main
  75. color: 'white',
  76. },
  77. }}
  78. >
  79. Load More
  80. </Button>
  81. )}
  82. {filteredData && next < filteredData.productsByCategory.length && (
  83. <Button
  84. onClick={handleMoreProducts}
  85. startIcon={
  86. <Image
  87. src="/images/arrow.svg"
  88. alt="arrow down"
  89. width={29}
  90. height={29}
  91. />
  92. }
  93. sx={{
  94. backgroundColor: 'primary.main',
  95. height: 50,
  96. width: 150,
  97. color: 'white',
  98. ':hover': {
  99. bgcolor: 'primary.main', // theme.palette.primary.main
  100. color: 'white',
  101. },
  102. }}
  103. >
  104. Load More
  105. </Button>
  106. )}
  107. </Box>
  108. </Container>
  109. );
  110. };
  111. export default ProductsGrid;