Skip to content

Commit

Permalink
Merge pull request #27 from atlp-rwanda/Ft-Products-searching
Browse files Browse the repository at this point in the history
Finishes [#187355072] product searching
  • Loading branch information
UmuhireJessie authored Jul 22, 2024
2 parents 42709eb + 4899ffa commit d62eb35
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 42 deletions.
64 changes: 55 additions & 9 deletions src/components/AvailableProduct/productPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,96 @@ import { RootState, AppDispatch } from '../../redux/store';
import { fetchAvailableProducts } from '../../redux/slices/availableProductSlice';
import Product from '../AvailableProduct/product';
import Pagination from '../AvailableProduct/pagination';
import { FaSlidersH } from 'react-icons/fa';

const ProductsPage: React.FC = () => {
const dispatch: AppDispatch = useDispatch();
const { products, loading, error, totalPages, currentPage: reduxCurrentPage } = useSelector((state: RootState) => state.products);

const [currentPage, setCurrentPage] = useState(reduxCurrentPage);
const [currentPage, setCurrentPage] = useState(reduxCurrentPage);
const [searchKeyword, setSearchKeyword] = useState('');
const [minPrice, setMinPrice] = useState<number | undefined>(undefined);
const [maxPrice, setMaxPrice] = useState<number | undefined>(undefined);
const [showSearchBar, setShowSearchBar] = useState(false);
const productsPerPage = 10;

useEffect(() => {
dispatch(fetchAvailableProducts(currentPage));
}, [dispatch, currentPage]);
dispatch(fetchAvailableProducts({ page: currentPage, searchKeyword, minPrice, maxPrice }));
}, [dispatch, currentPage, searchKeyword, minPrice, maxPrice]);

const handlePageChange = (pageNumber: number) => {
setCurrentPage(pageNumber);
setCurrentPage(pageNumber);
};


const handleSearch = () => {
setCurrentPage(1);
dispatch(fetchAvailableProducts({ page: 1, searchKeyword, minPrice, maxPrice }));
};

const toggleSearchBar = () => {
setShowSearchBar((prevState) => !prevState);
};

const handleMaxPriceChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const value = e.target.value;
setMaxPrice(value === '' ? undefined : Number(value));
};

return (
<div className="products-page">
<h1 className="title">Products</h1>
<div className="search-toggle">
<pre>Filter your Results </pre>
<FaSlidersH onClick={toggleSearchBar} style={{ cursor: 'pointer' }} />
</div>
{showSearchBar && (
<div className="search-bar">
<input
type="text"
placeholder="Search keyword (eg.: Name, Category ....)"
value={searchKeyword}
onChange={(e) => setSearchKeyword(e.target.value)}
/>
<input
type="number"
placeholder="Min Price"
value={minPrice || ''}
onChange={(e) => setMinPrice(Number(e.target.value))}
/>
<input
type="number"
placeholder="Max Price"
value={maxPrice === undefined ? '' : maxPrice}
onChange={handleMaxPriceChange}
/>
</div>
)}
{loading ? (
<div className="loading-message">Loading products...</div>
) : products.length === 0 ? (
<div className="noproduct-message">
<div className='noproduct-card'>
<div className="noproduct-card">
<h3>No available products in our stock</h3>
</div>
</div>
) : (
<>
<div className="product-list">
{products.map((product) => (
<Product
<Product
key={product.productId}
productId={product.productId}
name={product.name}
category={product.category}
price={product.price}
images={product.images}
discount={product.discount}
productId={product.productId} />
/>
))}
</div>
<Pagination
productsPerPage={productsPerPage}
totalProducts={totalPages * productsPerPage}
totalProducts={totalPages * productsPerPage}
onPageChange={handlePageChange}
currentPage={currentPage}
/>
Expand Down
6 changes: 5 additions & 1 deletion src/components/NavBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ const Navbar: React.FC<NavbarProps> = ({
setClicked(!clicked);
};

const handleShopClick = () => {
window.location.hash = 'product-page';
};

useEffect(() => {
dispatch(fetchProductsInCart())

Expand Down Expand Up @@ -82,7 +86,7 @@ const Navbar: React.FC<NavbarProps> = ({
</Link>
</li>
<li>
<Link to="/shop" onClick={() => setClicked(false)}>
<Link to="" onClick={handleShopClick}>
Shop
</Link>
</li>
Expand Down
Loading

0 comments on commit d62eb35

Please sign in to comment.