-
Notifications
You must be signed in to change notification settings - Fork 0
/
Product.jsx
64 lines (54 loc) · 1.95 KB
/
Product.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
import { toast } from "react-hot-toast";
import { useDispatch, useSelector } from "react-redux";
import {add ,remove} from "../redux/Slices/CartSlice";
const Product = ({post}) => {
const {cart} = useSelector((state) => state);
const dispatch = useDispatch();
const addToCart = () => {
dispatch(add(post));
toast.success("Item added to Cart");
}
const removeFromCart = () => {
dispatch(remove(post.id));
toast.error("Item removed from Cart");
}
return (
<div className="flex flex-col items-center justify-between
hover:scale-110 transition duration-300 ease-in gap-3 p-4 mt-10 ml-5 rounded-xl outline">
<div>
<p className="text-gray-700 font-semibold text-lg text-left truncate w-40 mt-1">{post.title}</p>
</div>
<div>
<p className="w-40 text-gray-400 font-normal text-[10px] text-left">{post.description.split(" ").slice(0,10).join(" ") + "..."}</p>
</div>
<div className="h-[180px]">
<img src={post.image} className="h-full w-full " />
</div>
<div className="flex justify-between gap-12 items-center w-full mt-5">
<div>
<p className="text-green-600 font-semibold">${post.price}</p>
</div>
{
cart.some((p) => p.id == post.id) ?
(<button
className="text-gray-700 border-2 border-gray-700 rounded-full font-semibold
text-[12px] p-1 px-3 uppercase
hover:bg-gray-700
hover:text-white transition duration-300 ease-in"
onClick={removeFromCart}>
Remove Item
</button>) :
(<button
className="text-gray-700 border-2 border-gray-700 rounded-full font-semibold
text-[12px] p-1 px-3 uppercase
hover:bg-gray-700
hover:text-white transition duration-300 ease-in"
onClick={addToCart}>
Add to Cart
</button>)
}
</div>
</div>
);
};
export default Product;