Skip to content

Commit

Permalink
search pagination fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Mariia Lutska committed Jun 16, 2024
1 parent 76d59d9 commit 0ce3c30
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/pages/ProductsPage/ProductsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,22 @@ export const ProductsPage: FC<Props> = ({ category }) => {
setSearchParams(params);
};

const pagesExist = itemsPerPage === 'all';
const totalPages = pagesExist
? 0
: Math.ceil(products.length / +itemsPerPage);

const startIndex = currentPage === 1 ? 0 : +itemsPerPage * (currentPage - 1);
const endIndex = startIndex + +itemsPerPage;

const filteredProducts = products.filter(product =>
product.name
.toLowerCase()
.trim()
.includes(searchQuery.toLowerCase().trim()),
);
const sortedProducts = sortProducts(filteredProducts, sortBy);

const pagesExist = itemsPerPage === 'all';
const totalPages = pagesExist
? 0
: Math.ceil(filteredProducts.length / +itemsPerPage);

const startIndex = currentPage === 1 ? 0 : +itemsPerPage * (currentPage - 1);
const endIndex = startIndex + +itemsPerPage;

const visibleProducts = pagesExist
? sortedProducts
: sortedProducts.slice(startIndex, endIndex);
Expand Down

0 comments on commit 0ce3c30

Please sign in to comment.