forked from MastersAcademy/fe-react-2024
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from NosatskyiK/task/05-hooks
added sorting, filters and pagination
- Loading branch information
Showing
7 changed files
with
421 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,69 @@ | ||
import React from 'react'; | ||
import React, { useState } from 'react'; | ||
|
||
import { FiltersBar } from '@/components/filtersBar/FiltersBar.component.tsx'; | ||
import { ProductCard } from '@/components/productCard/ProductCard.component.tsx'; | ||
import type { Product } from '@/interface/interfaceProductCategory.ts'; | ||
import type { ProductListProps } from '@/interface/interfaceProductFilters.ts'; | ||
|
||
import styles from './productList.module.css'; | ||
|
||
export const ProductList: React.FC<{ products: Product[] }> = ({ products }) => ( | ||
<> | ||
<FiltersBar /> | ||
<section className={styles.productList}> | ||
{products.map((product, index) => ( | ||
<ProductCard key={index} product={product} /> | ||
))} | ||
</section> | ||
</> | ||
); | ||
export const ProductList: React.FC<ProductListProps> = ({ products, selectedCategory, sortCriteria }) => { | ||
const [currentPage, setCurrentPage] = useState(1); | ||
const itemsPerPage = 8; | ||
|
||
const filteredProducts = selectedCategory ? products.filter((product) => product.category.name === selectedCategory) : products; | ||
|
||
const sortedProducts = [...filteredProducts].sort((a, b) => { | ||
switch (sortCriteria) { | ||
case 'Price (High - Low)': { | ||
return b.price - a.price; | ||
} | ||
case 'Price (Low - High)': { | ||
return a.price - b.price; | ||
} | ||
case 'Newest': { | ||
return new Date(b.creationAt).getTime() - new Date(a.creationAt).getTime(); | ||
} | ||
case 'Oldest': { | ||
return new Date(a.creationAt).getTime() - new Date(b.creationAt).getTime(); | ||
} | ||
default: { | ||
return 0; | ||
} | ||
} | ||
}); | ||
|
||
const totalPages = Math.ceil(sortedProducts.length / itemsPerPage); | ||
const indexOfLastItem = currentPage * itemsPerPage; | ||
const indexOfFirstItem = indexOfLastItem - itemsPerPage; | ||
const currentItems = sortedProducts.slice(indexOfFirstItem, indexOfLastItem); | ||
|
||
return ( | ||
<> | ||
<section className={styles.productList}> | ||
{currentItems.map((product, index) => ( | ||
<ProductCard key={index} product={product} /> | ||
))} | ||
</section> | ||
<div className={styles.pagination}> | ||
<button | ||
className={styles.paginationButton} | ||
onClick={() => currentPage < totalPages && setCurrentPage(currentPage - 1)} | ||
disabled={currentPage === 1} | ||
> | ||
< | ||
</button> | ||
{Array.from({ length: totalPages }, (_, index) => ( | ||
<button className={styles.paginationButton} key={index} onClick={() => setCurrentPage(index + 1)}> | ||
{index + 1} | ||
</button> | ||
))} | ||
<button | ||
className={styles.paginationButton} | ||
onClick={() => currentPage < totalPages && setCurrentPage(currentPage + 1)} | ||
disabled={currentPage === totalPages} | ||
> | ||
> | ||
</button> | ||
</div> | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.