|
| 1 | +/* eslint-disable */ |
| 2 | +import React, { useEffect, useState } from "react"; |
| 3 | +import { useAppDispatch, useAppSelector } from "../store/store"; |
| 4 | +import { fetchProducts } from "../store/features/product/productSlice"; |
| 5 | +import Product from "../components/product/Product"; |
| 6 | +import Sample from "../components/layout/Sample"; |
| 7 | +import { PuffLoader } from "react-spinners"; |
| 8 | +import { Meta } from "../components/Meta"; |
| 9 | +import { toast } from "react-toastify"; |
| 10 | +import { Navigate, useNavigate } from "react-router-dom"; |
| 11 | +import { createCart, getUserCarts } from "../store/features/carts/cartSlice"; |
| 12 | + |
| 13 | +const ProductsPage: React.FC = () => { |
| 14 | + const dispatch = useAppDispatch(); |
| 15 | + const navigate = useNavigate() |
| 16 | + const [cartResponseData, setCartResponseData] = useState<any>(null) |
| 17 | + const { products, isError, isSuccess, isLoading, message } = useAppSelector( |
| 18 | + (state: any) => state.products |
| 19 | + ); |
| 20 | + useEffect(() => { |
| 21 | + dispatch(fetchProducts()); |
| 22 | + }, [dispatch]); |
| 23 | + |
| 24 | + const handleAddProductToCart = async (productId: string, quantity = 1) => { |
| 25 | + try { |
| 26 | + const response = await dispatch( |
| 27 | + createCart({ productId, quantity }) |
| 28 | + ).unwrap(); |
| 29 | + |
| 30 | + if (response.data) { |
| 31 | + toast.success(response.message); |
| 32 | + const updatedResponse = await dispatch(getUserCarts()).unwrap(); |
| 33 | + setCartResponseData(updatedResponse.data); |
| 34 | + } else { |
| 35 | + toast.error(response.message); |
| 36 | + } |
| 37 | + } catch (error: any) { |
| 38 | + if (error === "Not authorized") { |
| 39 | + localStorage.setItem("pendingCartProduct", productId); |
| 40 | + toast.error("Please login first"); |
| 41 | + navigate("/login"); |
| 42 | + } else { |
| 43 | + toast.error("Something went wrong. Please try again later."); |
| 44 | + } |
| 45 | + } |
| 46 | + }; |
| 47 | + useEffect(() => { |
| 48 | + const checkProductToCartPending = async () => { |
| 49 | + const pendingProduct = localStorage.getItem("pendingCartProduct"); |
| 50 | + if (pendingProduct) { |
| 51 | + try { |
| 52 | + await handleAddProductToCart(pendingProduct, 1); |
| 53 | + localStorage.removeItem("pendingCartProduct"); |
| 54 | + } catch (error) { |
| 55 | + console.error("Failed to add product to cart:", error); |
| 56 | + } |
| 57 | + } |
| 58 | + }; |
| 59 | + |
| 60 | + checkProductToCartPending(); |
| 61 | + }, []); |
| 62 | + |
| 63 | + return ( |
| 64 | + <> |
| 65 | + <Meta title="Home - E-Commerce Ninjas" /> |
| 66 | + <Sample /> |
| 67 | + <div className="landing-container"> |
| 68 | + {isLoading ? ( |
| 69 | + <div className="loader"> |
| 70 | + <PuffLoader color="#ff6d18" size={300} loading={isLoading} /> |
| 71 | + </div> |
| 72 | + ) : isError ? ( |
| 73 | + <div className="error-message"> |
| 74 | + <p>{message || "Something went wrong. Please try again later."}</p> |
| 75 | + </div> |
| 76 | + ) : ( |
| 77 | + <div> |
| 78 | + <div className="head"> |
| 79 | + <h1>All products</h1> |
| 80 | + </div> |
| 81 | + <div className="product-list"> |
| 82 | + {isSuccess && |
| 83 | + Array.isArray(products) && |
| 84 | + products?.map((product: any) => ( |
| 85 | + <Product |
| 86 | + key={product.id} |
| 87 | + id={product.id} |
| 88 | + images={product.images} |
| 89 | + name={product.name} |
| 90 | + price={`$${product.price}`} |
| 91 | + stock={Number(product.quantity)} |
| 92 | + description={product.description} |
| 93 | + discount={Number(product.discount.replace("%", ""))} |
| 94 | + /> |
| 95 | + ))} |
| 96 | + </div> |
| 97 | + </div> |
| 98 | + )} |
| 99 | + </div> |
| 100 | + </> |
| 101 | + ); |
| 102 | +}; |
| 103 | + |
| 104 | +export default ProductsPage; |
0 commit comments