-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProductCard.tsx
286 lines (268 loc) · 8.21 KB
/
ProductCard.tsx
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
274
275
276
277
278
279
280
281
282
283
284
285
286
import { IconButton, Rating, Typography } from "@mui/material";
import { FaEye } from "react-icons/fa";
import { IoHeartSharp } from "react-icons/io5";
import React, { useEffect, useState } from "react";
import {
Link,
useLocation,
useNavigate,
useNavigation,
} from "react-router-dom";
import { ToastContainer, toast } from "react-toastify";
import { AxiosError } from "axios";
import { useSelector } from "react-redux";
import Spinner from "../dashboard/Spinner";
import {
addWish,
fetchWishes,
deleteWish,
} from "../../redux/reducers/wishListSlice";
import { IProduct } from "../../types";
import { useAppDispatch } from "../../redux/hooks";
import {
addToCart,
cartManage,
removeFromCart,
} from "../../redux/reducers/cartSlice";
import Warning from "../common/notify/Warning";
import { RootState } from "../../redux/store";
import { RegisterError, Review } from "../../../type";
import api from "../../redux/api/api";
interface IProductCardProps {
product: IProduct;
}
const ProductCard: React.FC<IProductCardProps> = ({ product }) => {
const [isHovered, setIsHovered] = useState(false);
const [isLoading, setIsLoading] = useState(false);
const [loadWishes, setLoadWishes] = useState(false);
const [reviews, setReviews] = useState<Review[]>([]);
const { wishes } = useSelector((state: RootState) => state.wishes);
const dispatch = useAppDispatch();
const navigate = useNavigate();
const loggedInUserToken = localStorage.getItem("accessToken");
let loggedInUser;
if (loggedInUserToken) {
// @ts-ignore
loggedInUser = JSON.parse(atob(loggedInUserToken.split(".")[1]));
}
const formatPrice = (price: number) => {
if (price < 1000) {
return price.toString();
}
if (price >= 1000 && price < 1000000) {
return `${(price / 1000).toFixed(1)}k`;
}
return `${(price / 1000000).toFixed(1)}M`;
};
useEffect(() => {
const fetchReviews = async () => {
try {
const response = await api.get(`/products/${product.id}/reviews`);
if (!response) {
throw new Error("Failed to fetch reviews");
}
const data = await response.data;
setReviews(data.reviewProduct);
} catch (error) {
console.error("Error fetching reviews:", error);
}
};
fetchReviews();
}, [product.id]);
useEffect(() => {
setLoadWishes(true);
const fetchData = async () => {
try {
await dispatch(fetchWishes());
setLoadWishes(false);
} catch (error) {
console.error(error);
}
};
if (localStorage.getItem("accessToken")) {
fetchData();
}
}, [dispatch]);
const total = reviews
? reviews.reduce((sum, review) => sum + (review.rating, 10), 0)
/ reviews.length
: 0;
const handleRemove = async (productId: number) => {
try {
// @ts-ignore
await dispatch(removeFromCart(productId));
dispatch(cartManage());
} catch (err) {
const error = err as AxiosError<RegisterError>;
toast.error(`${error.message}`);
}
};
const loading = useSelector(
(state: RootState) => state.cart.remove.isLoading,
);
const userCart = useSelector((state: RootState) => state.cart.data);
const alreadyInCart = userCart?.some(
(item) =>
// @ts-ignore
item.product?.id === product.id,
);
const alreadyWished = wishes?.some((item) => item.product?.id === product.id);
const handleAddToCart = async () => {
if (!localStorage.getItem("accessToken")) {
toast.error("Please Log in to add to cart.");
setTimeout(() => {
navigate("/login");
}, 4000);
return;
}
setIsLoading(true);
try {
await dispatch(
addToCart({ productId: product.id, quantity: 1 }),
).unwrap();
dispatch(cartManage());
} catch (err) {
const error = err as AxiosError;
// toast.error(`Failed to add product to cart: ${error.message}`);
} finally {
setIsLoading(false);
}
};
const handleAddWish = async () => {
try {
setLoadWishes(true);
if (product.id) {
const response = await dispatch(addWish({ productId: product.id }));
if (response.payload === "product already exists in your wishlist") {
handleDeleteWish();
setLoadWishes(false);
await dispatch(fetchWishes());
} else {
await dispatch(fetchWishes());
setLoadWishes(false);
}
}
} catch (err) {
const error = err as AxiosError;
toast.error(error.message);
}
};
const handleDeleteWish = async () => {
try {
setLoadWishes(true);
if (product.id) {
dispatch(deleteWish({ productId: product.id }));
await dispatch(fetchWishes());
setLoadWishes(false);
}
} catch (err) {
const error = err as AxiosError;
toast.error(error.message);
}
};
const name = product.name.length > 20
? `${product.name.substring(0, 12)}...`
: product.name;
const date = new Date(product.createdAt).getDate();
const currentDate = new Date().getDate();
const diff = currentDate - date;
return (
<div className="max-h-[270px] bg-[#F5F5F5] mb-2 relative" data-testid="tbt">
<ToastContainer />
<div
className="relative min-h-[200px]"
onMouseOver={() => setIsHovered(true)}
onMouseLeave={() => setIsHovered(false)}
>
<img
src={product.images[0]}
alt="product"
className="w-[100%] h-[200px] hover:scale-[1.05] transition duration-200 cursor-pointer"
data-testid="prod-image"
onClick={() => navigate(`/products/${product.id}`)}
/>
{isHovered && (
<div className="">
{!alreadyInCart ? (
<button
className="bg-black text-white w-full py-2 text-center absolute bottom-0"
data-testid="add-to-cart"
onClick={handleAddToCart}
>
{isLoading ? "Adding..." : "Add To Cart"}
</button>
) : (
<button
className="bg-red-500 text-white w-full py-2 text-center absolute bottom-0"
onClick={() => handleRemove(product.id)}
>
{loading ? "Loading" : "Remove Cart"}
</button>
)}
</div>
)}
</div>
<div className="absolute top-0 right-0 p-0" data-testid="new">
<div className="w-full flex flex-col gap-0">
<IconButton
className="bg-white"
sx={{ paddingY: 0.5, paddingX: 0.5 }}
>
{!loggedInUserToken || loggedInUser.roleId !== 1 ? (
""
) : loadWishes ? (
<Spinner />
) : alreadyWished ? (
<IoHeartSharp
className="text-[#DB4444] bg-white p-2 rounded-full text-[30px]"
data-testid="like-btn"
onClick={handleAddWish}
/>
) : (
<IoHeartSharp
className="text-black bg-white p-2 rounded-full text-[30px]"
data-testid="like-btn"
onClick={handleAddWish}
/>
)}
</IconButton>
<IconButton
sx={{ paddingY: 0.5, paddingX: 0.5 }}
data-testid="dprod-detailbtn"
>
<Link to={`/products/${product.id}`}>
<FaEye className="text-black bg-white p-2 rounded-full text-[30px]" />
</Link>
</IconButton>
</div>
</div>
<Typography
className="font-bold text-[14px] sm:text-[18px] text-nowrap p-1"
data-testid="product-name"
>
{name}
</Typography>
<div className="flex items-center gap-1 p-1">
<p className="text-red-700 font-bold text-[14px]" data-testid="price">
{/* ${formatPrice(product.price)} */}
{product.price}
{' '}
Rwf
</p>
<Rating
value={total}
color="red"
// disabled
size="small"
data-testid="rating"
/>
<p className="text-[10px]" data-testid="review">
(
{reviews ? reviews.length : 0}
)
</p>
</div>
</div>
);
};
export default ProductCard;