-
Notifications
You must be signed in to change notification settings - Fork 227
/
Copy pathCart.jsx
273 lines (249 loc) · 8.43 KB
/
Cart.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import React, { useState, useEffect } from "react";
import Spinner from "./Spinner";
import {
fetchCartData,
addItemToCart,
removeItemFromCart,
} from "../api/api.js";
import "./Cart.css";
import Preloader from "../components/Preloader";
import { useToast } from "../Context/ToastContext";
import { useNavigate } from "react-router-dom";
import { useAuth } from "../Context/AuthContext";
function Cart() {
const [isLoading, setIsLoading] = useState(true);
const [data, setData] = useState(null);
const [error, setError] = useState(null);
const [retryCount, setRetryCount] = useState(0);
const navigate = useNavigate();
const { showToast } = useToast();
const isAuthenticated =!! localStorage.getItem('token');
if(!isAuthenticated){
navigate('/login')
}
const { userLoggedIn } = useAuth();
useEffect(() => {
fetchCartData()
.then((cartData) => {
setData(cartData);
setIsLoading(false);
setError(null); // Reset error state on successful data fetch
})
.catch((err) => {
setError("Failed to load cart data. Please try again later.");
setIsLoading(false);
console.error("Error fetching cart data:", err);
});
}, [retryCount]); // Retry whenever retryCount changes
useEffect(() => {
if (!userLoggedIn) {
showToast("error", "Please login to view your cart.", undefined, 7000);
setTimeout(() => {
navigate("/login");
}, 3000); // 3000 milliseconds = 3 seconds
}
// }, [userLoggedIn]);
}, [userLoggedIn, showToast, navigate]);
useEffect(() => {
fetchCartData()
.then((cartData) => {
setData(cartData);
setIsLoading(false);
setError(null);
})
.catch((err) => {
setError("Failed to load cart data. Please try again later.");
setIsLoading(false);
console.error("Error fetching cart data:", err);
});
}, [retryCount]);
const handleAddItem = (item) => {
setIsLoading(true);
addItemToCart(item)
.then((updatedCart) => {
setData(updatedCart);
setIsLoading(false);
})
.catch((err) => {
setError("Failed to add item to cart. Please try again later.");
setIsLoading(false);
console.error("Error adding item to cart:", err);
});
};
const handleRemoveItem = (itemId) => {
setIsLoading(true);
removeItemFromCart(itemId)
.then((updatedCart) => {
setData(updatedCart);
setIsLoading(false);
})
.catch((err) => {
setError("Failed to remove item from cart. Please try again later.");
setIsLoading(false);
console.error("Error removing item from cart:", err);
});
};
const handleRetry = () => setRetryCount(retryCount + 1);
const renderCartItems = () => {
return data.items.map((item) => (
<table className="cart-table">
<tr key={item.id}>
<td>
<img
src={item.image || "https://via.placeholder.com/150"}
alt={item.name}
/>
</td>
<td className="font-semibold">{item.name}</td>
<td>${item.price.toFixed(2)}</td>
<td>
<button
onClick={() => handleRemoveItem(item.id)}
className="quantity-button"
>
-
</button>
<span>{item.quantity}</span>
<button
onClick={() => handleAddItem(item)}
className="quantity-button"
>
+
</button>
</td>
<td>
<button
onClick={() => handleRemoveItem(item.id)}
className="remove-btn"
>
Remove
</button>
</td>
</tr>
</table>
));
};
const renderSuggestedProducts = () => (
<div className="suggested-products space-y-4">
<h2 className="dark:text-white font-semibold font-poppins underline underline-offset-4">Suggested Products</h2>
<ul className="flex gap-5">
<div className="dark:text-white border-2 pb-2 rounded-md overflow-hidden h-max shadow-lg w-[10vw]">
<div className="font-dmsans flex flex-col items-center">
<div className="p-4">
<img
src="https://images-na.ssl-images-amazon.com/images/S/compressed.photo.goodreads.com/books/1635260162i/58724923.jpg"
width={100}
className="h-[15vh] shadow-md"
alt="img"
/>
</div>
<h1 className="text-blue-950 font-semibold text-sm">
Hidden Pictures
</h1>
<p className="text-xs">Horror</p>
<p className="font-semibold">$10.99</p>
<button className="bg-yellow-500 text-xs px-4 py-1 rounded-md mt-2">Add to Cart</button>
</div>
</div>
<div className="dark:text-white border-2 pb-2 rounded-md overflow-hidden h-max shadow-lg w-[10vw]">
<div className="font-dmsans flex flex-col items-center">
<div className="p-4">
<img
src="https://images-na.ssl-images-amazon.com/images/S/compressed.photo.goodreads.com/books/1678378842i/60683957.jpg"
width={100}
className="h-[15vh] shadow-md"
alt="img"
/>
</div>
<h1 className="text-blue-950 font-semibold text-sm">
Check & Mate
</h1>
<p className="text-xs">Young Adult Fantasy</p>
<p className="font-semibold">$11.59</p>
<button className="bg-yellow-500 text-xs px-4 py-1 rounded-md mt-2">Add to Cart</button>
</div>
</div>{" "}
<div className="dark:text-white border-2 pb-2 rounded-md overflow-hidden h-max shadow-lg w-[10vw]">
<div className="font-dmsans flex flex-col items-center">
<div className="p-4">
<img
src="https://images-na.ssl-images-amazon.com/images/S/compressed.photo.goodreads.com/books/1643228739i/55196813.jpg"
width={100}
className="h-[15vh] shadow-md"
alt="img"
/>
</div>
<h1 className="text-blue-950 font-semibold text-sm">
The Maid
</h1>
<p className="text-xs">Fiction</p>
<p className="font-semibold">$8.99</p>
<button className="bg-yellow-500 text-xs px-4 py-1 rounded-md mt-2">Add to Cart</button>
</div>
</div>
</ul>
</div>
);
if (isLoading) return <Spinner />;
if (error) {
return (
<>
<Preloader />
<div className="cart-container dark:bg-[rgb(40,40,40)]">
<h1 className="cart-header dark:text-white">Shopping Cart</h1>
<div className="w-full justify-center items-center flex">
<img src="/error.png"/>
</div>
<p className="font-dmsans text-center text-red-400">{error}</p>
<div className="w-full justify-center flex mt-5">
<button onClick={handleRetry} className="retry-button dark:text-white">
Retry
</button>
</div>
</div>
</>
);
}
return (
<>
<Preloader />
<div className="cart-container dark:bg-[rgb(40,40,40)]">
<h1 className="cart-header dark:text-white">Shopping Cart</h1>
{data && data.items && data.items.length > 0 ? (
<>
<table className="cart-table">
<thead>
<tr>
<th>Image</th>
<th>Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Action</th>
</tr>
</thead>
<tbody>{renderCartItems()}</tbody>
</table>
<div className="cart-summary">
<div className="cart-total dark:text-white">
Total: <span className="font-medium">${data ? data.total.toFixed(2) : "0.00"}</span> </div>
<button className="checkout-button dark:text-white">
Proceed to Checkout
</button> </div>
</>
)
: (
<div className="flex text-3xl items-center justify-center font-poppins">
<div>
<img src="/cart.webp" width={200} />
</div>
<p className="dark:text-white text-[#4A628A] ">
Your cart is empty.
</p>
</div>
)}
{renderSuggestedProducts()}
</div>
</>
);
}
export default Cart;