import { useParams } from "react-router-dom"; import { useEffect } from "react"; import { useDispatch, useSelector } from "react-redux"; import Breadcrumb from "../../components/ui/Breadcrumb"; import ProductImages from "./ProductImages"; import ProductInfo from "./ProductInfo"; import ScrollToTop from "../ui/ScrollToTop"; import { RatingPage } from "../../pages/RatingPage"; import { useGetProductBySlugQuery } from "../../features/products/productsAPI"; import { useAddToWishlistMutation, useRemoveFromWishlistMutation, useGetWishlistQuery, } from "../../features/wishlist/wishlistApi"; import { addRecentProduct } from "../../features/recentlyViewed/recentlyViewedApi"; import RecommendedProducts from "./RecommendedProducts"; import RecommendedForYou from "../home/RecommendedForYou/RecommendedForYou"; const ProductDetails = () => { const { slug } = useParams(); const dispatch = useDispatch(); const token = useSelector((state) => state.auth.token); const { data, isLoading, isError } = useGetProductBySlugQuery(slug); const { data: wishlistData } = useGetWishlistQuery(undefined, { skip: !token }); const [addToWishlist] = useAddToWishlistMutation(); const [removeFromWishlist] = useRemoveFromWishlistMutation(); // const product = data?.data?.product; // ✅ Track product view with proper error handling useEffect(() => { if (data?.data?.product) { const product = data.data.product; // ✅ Ensure all required fields exist and are valid const productToSave = { id: product._id, name: product.name || "Unnamed Product", image: product.images?.primary || (Array.isArray(product.images?.gallery) && product.images.gallery[0]) || product.image || "/placeholder.jpg", price: product.basePrice != null && !isNaN(product.basePrice) ? parseFloat(product.basePrice) : 0, url: `/product/${slug}`, }; console.log("Recently viewed product:", productToSave); // ✅ Debug log to see what's being saved console.log("Saving product to recently viewed:", productToSave); dispatch(addRecentProduct(productToSave)); } }, [data, dispatch, slug]); if (isLoading) return

Loading product...

; if (isError || !data?.data?.product) return

Product not found

; const product = data.data.product; const wishlistIds = wishlistData?.data?.wishlist?.map( (item) => item.product?._id || item.productId, ) || []; const isWishlisted = wishlistIds.includes(product._id); const toggleWishlist = async () => { try { if (isWishlisted) { await removeFromWishlist(product._id).unwrap(); } else { await addToWishlist(product._id).unwrap(); } } catch (err) { console.error("Wishlist error:", err); } }; return (
); }; export default ProductDetails;