Blame view

components/detail/WIshlistComponent.js 1.81 KB
jaymehta committed
1 2 3 4 5 6
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 }) => {
jaymehta committed
7
  const { wishlists, loading } = useSelector(state => state.wishlists);
jaymehta committed
8 9 10
  const [isActive, setisActive] = useState(false);
  const [wishlistId, setwishlistId] = useState();
  const dispatch = useDispatch();
jaymehta committed
11
  //   console.log("wishlists", wishlists);
jaymehta committed
12 13 14 15
  useEffect(() => {
    wishlists &&
      wishlists.length > 0 &&
      wishlists.map(item => {
16
        if (item?.attributes?.experience?.data?.id == activityId) {
jaymehta committed
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
          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;