-
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.
[Deliver -187584887 & 187584886] A buyer should update delete carts (#57
) * [Deliver -187584887 & 187584886] A buyer should update delete carts * fix bugs * fix bug in deleting cart
- Loading branch information
1 parent
981929d
commit 6beacdd
Showing
7 changed files
with
482 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
{ | ||
"cSpell.words": [ | ||
"noresults" | ||
"flexer", | ||
"noresults", | ||
"setarray", | ||
"setcheckout", | ||
"toastify" | ||
] | ||
} |
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 |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* eslint-disable */ | ||
import React, { useEffect, useState } from "react"; | ||
import { useAppDispatch, useAppSelector } from "../store/store"; | ||
import { fetchProducts } from "../store/features/product/productSlice"; | ||
import Product from "../components/product/Product"; | ||
import Sample from "../components/layout/Sample"; | ||
import { PuffLoader } from "react-spinners"; | ||
import { Meta } from "../components/Meta"; | ||
import { toast } from "react-toastify"; | ||
import { Navigate, useNavigate } from "react-router-dom"; | ||
import { createCart, getUserCarts } from "../store/features/carts/cartSlice"; | ||
|
||
const ProductsPage: React.FC = () => { | ||
const dispatch = useAppDispatch(); | ||
const navigate = useNavigate() | ||
const [cartResponseData, setCartResponseData] = useState<any>(null) | ||
const { products, isError, isSuccess, isLoading, message } = useAppSelector( | ||
(state: any) => state.products | ||
); | ||
useEffect(() => { | ||
dispatch(fetchProducts()); | ||
}, [dispatch]); | ||
|
||
const handleAddProductToCart = async (productId: string, quantity = 1) => { | ||
try { | ||
const response = await dispatch( | ||
createCart({ productId, quantity }) | ||
).unwrap(); | ||
|
||
if (response.data) { | ||
toast.success(response.message); | ||
const updatedResponse = await dispatch(getUserCarts()).unwrap(); | ||
setCartResponseData(updatedResponse.data); | ||
} else { | ||
toast.error(response.message); | ||
} | ||
} catch (error: any) { | ||
if (error === "Not authorized") { | ||
localStorage.setItem("pendingCartProduct", productId); | ||
toast.error("Please login first"); | ||
navigate("/login"); | ||
} else { | ||
toast.error("Something went wrong. Please try again later."); | ||
} | ||
} | ||
}; | ||
useEffect(() => { | ||
const checkProductToCartPending = async () => { | ||
const pendingProduct = localStorage.getItem("pendingCartProduct"); | ||
if (pendingProduct) { | ||
try { | ||
await handleAddProductToCart(pendingProduct, 1); | ||
localStorage.removeItem("pendingCartProduct"); | ||
} catch (error) { | ||
console.error("Failed to add product to cart:", error); | ||
} | ||
} | ||
}; | ||
|
||
checkProductToCartPending(); | ||
}, []); | ||
|
||
return ( | ||
<> | ||
<Meta title="Home - E-Commerce Ninjas" /> | ||
<Sample /> | ||
<div className="landing-container"> | ||
{isLoading ? ( | ||
<div className="loader"> | ||
<PuffLoader color="#ff6d18" size={300} loading={isLoading} /> | ||
</div> | ||
) : isError ? ( | ||
<div className="error-message"> | ||
<p>{message || "Something went wrong. Please try again later."}</p> | ||
</div> | ||
) : ( | ||
<div> | ||
<div className="head"> | ||
<h1>All products</h1> | ||
</div> | ||
<div className="product-list"> | ||
{isSuccess && | ||
Array.isArray(products) && | ||
products?.map((product: any) => ( | ||
<Product | ||
key={product.id} | ||
id={product.id} | ||
images={product.images} | ||
name={product.name} | ||
price={`$${product.price}`} | ||
stock={Number(product.quantity)} | ||
description={product.description} | ||
discount={Number(product.discount.replace("%", ""))} | ||
/> | ||
))} | ||
</div> | ||
</div> | ||
)} | ||
</div> | ||
</> | ||
); | ||
}; | ||
|
||
export default ProductsPage; |
Oops, something went wrong.