WIshlistComponent.js
1.81 KB
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
import Image from "next/image";
import React, { useEffect, useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import { checkWishlist, deleteWishlist, getWishlists, toggleWishlist } from "../../redux/actions/activityAction";
const WishlistComponent = ({ activityId, userId }) => {
const { wishlists, loading } = useSelector(state => state.wishlists);
const [isActive, setisActive] = useState(false);
const [wishlistId, setwishlistId] = useState();
const dispatch = useDispatch();
// console.log("wishlists", wishlists);
useEffect(() => {
wishlists &&
wishlists.length > 0 &&
wishlists.map(item => {
if (item.attributes.experience.data.id == activityId) {
setisActive(true);
setwishlistId(item.id);
}
});
}, [wishlists]);
return (
<div>
{activityId && userId && (
<div className="wishlist">
<span
onClick={async e => {
e.preventDefault();
if (isActive) {
setisActive(false);
const response = await deleteWishlist({ wishlistId });
dispatch(getWishlists({ endUser: userId }));
// console.log("delete", response);
} else {
setisActive(true);
const response = await toggleWishlist({ activityId, userId });
dispatch(getWishlists({ endUser: userId }));
}
// console.log("response wishlist ", response);
}}
className="image-container"
>
<Image layout="fill" alt="" className="image img-fluid" src={isActive ? "/images/icons/wishlist-01-active.svg" : "/images/icons/wishlist-01.svg"} />
</span>
</div>
)}
</div>
);
};
export default WishlistComponent;