import { Button, Container, Grid } from '@mui/material'; import { Box } from '@mui/system'; import Image from 'next/image'; import { useMemo, useState } from 'react'; import { useFetchProductsByCategory } from '../../hooks/useFetchProductData'; import { compare } from '../../utils/helpers/sortHelpers'; import ProductCard from '../product-card/ProductCard'; const ProductsGrid = ({ allProducts, hasNextPage, productType, fetchNextPage, sort, }) => { const productsPerPage = 9; const [next, setNext] = useState(productsPerPage); const { data: filteredData } = useFetchProductsByCategory(productType); const allItems = useMemo( () => allProducts?.pages?.flatMap((page) => page.product), [allProducts] ); const dataToDisplay = productType === 'All' || productType === '' ? allItems.sort(compare('name', sort)).map((item) => ( )) : filteredData?.productsByCategory .slice(0, next) .sort(compare('name', sort)) .map((item) => ( )); const handleMoreProducts = () => { setNext(next + productsPerPage); }; return ( {dataToDisplay} {hasNextPage && (productType === 'All' || productType === '') && ( )} {filteredData && next < filteredData.productsByCategory.length && ( )} ); }; export default ProductsGrid;