Skip to content

Commit 6beacdd

Browse files
[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
1 parent 981929d commit 6beacdd

File tree

7 files changed

+482
-34
lines changed

7 files changed

+482
-34
lines changed

.vscode/settings.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
{
22
"cSpell.words": [
3-
"noresults"
3+
"flexer",
4+
"noresults",
5+
"setarray",
6+
"setcheckout",
7+
"toastify"
48
]
59
}

src/assets/styles/ViewCart.scss

+30-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
// button {
2-
// background-color: transparent;
3-
// border: 0;
4-
// }
51
FaCheckSquare {
62
color: $primary-color;
73
}
84
.cart-section {
95
display: flex;
106
flex-direction: row;
11-
padding: 20px;
127
.cart-products {
138
flex: 2;
149
margin: 10px;
@@ -35,7 +30,7 @@ FaCheckSquare {
3530
.checkout-btn {
3631
background: $primary-color;
3732
cursor: pointer;
38-
width: 12%;
33+
width: 8%;
3934
color: $white;
4035
border-radius: 5px;
4136
border: none;
@@ -116,12 +111,14 @@ FaCheckSquare {
116111
width: 20px;
117112
height: 20px;
118113
font-size: 12px;
114+
cursor:pointer;
119115
}
120116
input {
121117
background: transparent;
122118
border: 0;
123119
width: 20px;
124120
padding-left: 10px;
121+
padding-right: 10px;
125122
text-align: center;
126123
align-items: center;
127124
}
@@ -133,6 +130,7 @@ FaCheckSquare {
133130
background: transparent;
134131
border: 0;
135132
font-size: 20px;
133+
margin-right: 3rem;
136134
cursor: pointer;
137135
}
138136
.delete {
@@ -206,6 +204,7 @@ FaCheckSquare {
206204
border: none;
207205
color: $white;
208206
padding: 10px;
207+
209208
}
210209
}
211210
}
@@ -391,8 +390,33 @@ FaCheckSquare {
391390
border-radius: 5px;
392391
color: $white;
393392
padding: 10px;
393+
394394
}
395395
}
396396
}
397397
}
398398
}
399+
.clear-cart {
400+
margin-top: 1rem;
401+
margin-left: 26.5rem;
402+
border: none;
403+
justify-content: center;
404+
align-items: center;
405+
display: flex;
406+
width: 100%;
407+
.delete {
408+
border: none;
409+
background-color: $primary-color;
410+
color: $white;
411+
font-size: 1.5rem;
412+
padding: 1rem;
413+
border-radius: 5px;
414+
display: flex;
415+
cursor: pointer;
416+
}
417+
.deleteIcon {
418+
font-size: 2rem;
419+
margin-right: 1rem;
420+
cursor: pointer;
421+
}
422+
}

src/pages/Products.tsx

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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

Comments
 (0)