|
| 1 | +/* eslint-disable */ |
| 2 | +import React, { useEffect, useState } from "react"; |
| 3 | +import { useParams, useNavigate } from "react-router-dom"; |
| 4 | +import { useAppDispatch, useAppSelector } from "../store/store"; |
| 5 | +import { fetchProductsByShopId } from "../store/features/product/shopSlice"; |
| 6 | +import Product from "../components/product/Product"; |
| 7 | +import { PuffLoader } from "react-spinners"; |
| 8 | +import { Meta } from "../components/Meta"; |
| 9 | +import { toast } from "react-toastify"; |
| 10 | +import { createCart, getUserCarts } from "../store/features/carts/cartSlice"; |
| 11 | + |
| 12 | +const ProductsByShopPage: React.FC = () => { |
| 13 | + const { shopId } = useParams<{ shopId: string }>(); |
| 14 | + const dispatch = useAppDispatch(); |
| 15 | + const navigate = useNavigate(); |
| 16 | + const [cartResponseData, setCartResponseData] = useState<any>(null); |
| 17 | + |
| 18 | + const { shops, shopProductsByShop, isLoadingProducts, isErrorProducts, isSuccessProducts, message } = useAppSelector( |
| 19 | + (state: any) => state.shop |
| 20 | + ); |
| 21 | + |
| 22 | + const [priceRange, setPriceRange] = useState([0, 0]); |
| 23 | + const [maxPrice, setMaxPrice] = useState(0); |
| 24 | + const [minPrice, setMinPrice] = useState(0); |
| 25 | + const [visibleProducts, setVisibleProducts] = useState<number>(20); |
| 26 | + |
| 27 | + useEffect(() => { |
| 28 | + if (shopId) { |
| 29 | + dispatch(fetchProductsByShopId(shopId)); |
| 30 | + } |
| 31 | + }, [dispatch, shopId]); |
| 32 | + |
| 33 | + const shop = shops.find((shop: any) => shop.id === shopId); |
| 34 | + const products = shopProductsByShop ? shopProductsByShop[shopId] : []; |
| 35 | + |
| 36 | + useEffect(() => { |
| 37 | + if (products && products.length > 0) { |
| 38 | + const calculatedMaxPrice = products.reduce((max, product) => Math.max(max, product.price), 0); |
| 39 | + const calculatedMinPrice = products.reduce((min, product) => Math.min(min, product.price), calculatedMaxPrice); |
| 40 | + |
| 41 | + setMaxPrice(calculatedMaxPrice); |
| 42 | + setMinPrice(calculatedMinPrice); |
| 43 | + setPriceRange([calculatedMinPrice, calculatedMaxPrice]); |
| 44 | + } |
| 45 | + }, [products]); |
| 46 | + |
| 47 | + const handleAddProductToCart = async (productId: string, quantity = 1) => { |
| 48 | + try { |
| 49 | + const response = await dispatch( |
| 50 | + createCart({ productId, quantity }) |
| 51 | + ).unwrap(); |
| 52 | + |
| 53 | + if (response.data) { |
| 54 | + toast.success(response.message); |
| 55 | + const updatedResponse = await dispatch(getUserCarts()).unwrap(); |
| 56 | + setCartResponseData(updatedResponse.data); |
| 57 | + } else { |
| 58 | + toast.error(response.message); |
| 59 | + } |
| 60 | + } catch (error: any) { |
| 61 | + if (error === "Not authorized") { |
| 62 | + localStorage.setItem("pendingCartProduct", productId); |
| 63 | + toast.error("Please login first"); |
| 64 | + navigate("/login"); |
| 65 | + } else { |
| 66 | + toast.error("Something went wrong. Please try again later."); |
| 67 | + } |
| 68 | + } |
| 69 | + }; |
| 70 | + |
| 71 | + const filteredProducts = products.filter((product: any) => { |
| 72 | + const price = parseFloat(product.price); |
| 73 | + return price >= priceRange[0] && price <= priceRange[1]; |
| 74 | + }); |
| 75 | + |
| 76 | + const handleLoadMore = () => { |
| 77 | + setVisibleProducts((prevVisibleProducts) => prevVisibleProducts + 20); |
| 78 | + }; |
| 79 | + |
| 80 | + return ( |
| 81 | + <> |
| 82 | + <Meta title={`Products - Shop ${shop?.name || 'Shop'}`} /> |
| 83 | + <div className="landing-container"> |
| 84 | + {isLoadingProducts ? ( |
| 85 | + <div className="loader"> |
| 86 | + <PuffLoader color="#ff6d18" size={300} loading={isLoadingProducts} /> |
| 87 | + </div> |
| 88 | + ) : isErrorProducts ? ( |
| 89 | + <div className="error-message"> |
| 90 | + <p>{message || "Something went wrong. Please try again later."}</p> |
| 91 | + </div> |
| 92 | + ) : ( |
| 93 | + <div> |
| 94 | + <div className="head"> |
| 95 | + <h1>{shop?.name || 'Shop'}</h1> |
| 96 | + </div> |
| 97 | + <div className="filters"> |
| 98 | + <div> |
| 99 | + <label>Price Range: </label> |
| 100 | + <input |
| 101 | + type="range" |
| 102 | + min={minPrice} |
| 103 | + max={maxPrice} |
| 104 | + value={priceRange[1]} |
| 105 | + onChange={(e) => |
| 106 | + setPriceRange([priceRange[0], Number(e.target.value)]) |
| 107 | + } |
| 108 | + /> |
| 109 | + <span className="span">{priceRange[0]}RWF - {priceRange[1]}RWF</span> |
| 110 | + </div> |
| 111 | + </div> |
| 112 | + <div className="product-list"> |
| 113 | + {isSuccessProducts && |
| 114 | + Array.isArray(filteredProducts) && |
| 115 | + filteredProducts |
| 116 | + .slice(0,visibleProducts) |
| 117 | + .map((product: any) => ( |
| 118 | + <Product |
| 119 | + key={product.id} |
| 120 | + id={product.id} |
| 121 | + images={product.images} |
| 122 | + name={product.name} |
| 123 | + price={product.price} |
| 124 | + stock={Number(product.quantity)} |
| 125 | + description={product.description} |
| 126 | + discount={Number(product.discount.replace("%", ""))} |
| 127 | + /> |
| 128 | + ))} |
| 129 | + </div> |
| 130 | + {visibleProducts < products.length && ( |
| 131 | + <div className="load-more"> |
| 132 | + <button onClick={handleLoadMore}>Load More</button> |
| 133 | + </div> |
| 134 | + )} |
| 135 | + </div> |
| 136 | + )} |
| 137 | + </div> |
| 138 | + </> |
| 139 | + ); |
| 140 | +}; |
| 141 | + |
| 142 | +export default ProductsByShopPage; |
0 commit comments