import React, { useEffect, useState } from 'react'; import { useNavigate } from 'react-router-dom'; import { Trash2, ShoppingCart, Heart } from 'lucide-react'; import { useWishlist } from '@/hooks/useWishlist'; import { useCartStore } from '@/lib/cart/store'; import { Button } from '@/components/ui/button'; import { toast } from 'sonner'; import { apiClient } from '@/lib/api/client'; import { formatPrice } from '@/lib/currency'; interface ProductData { id: number; name: string; slug: string; price: string; regular_price?: string; sale_price?: string; image?: string; on_sale?: boolean; stock_status?: string; } /** * Public Wishlist Page - Accessible to both guests and logged-in users * Guests: Shows items from localStorage * Logged-in: Shows items from database via API */ export default function Wishlist() { const navigate = useNavigate(); const { items, isLoading, isLoggedIn, removeFromWishlist, productIds } = useWishlist(); const { addItem } = useCartStore(); const [guestProducts, setGuestProducts] = useState([]); const [loadingGuest, setLoadingGuest] = useState(false); // Fetch product details for guest wishlist useEffect(() => { const fetchGuestProducts = async () => { if (!isLoggedIn && productIds.size > 0) { setLoadingGuest(true); try { const ids = Array.from(productIds).join(','); const response = await apiClient.get(`/shop/products?include=${ids}`); setGuestProducts(response.products || []); } catch (error) { console.error('Failed to fetch guest wishlist products:', error); } finally { setLoadingGuest(false); } } }; fetchGuestProducts(); }, [isLoggedIn, productIds]); const handleRemove = async (productId: number) => { await removeFromWishlist(productId); // Remove from guest products list setGuestProducts(prev => prev.filter(p => p.id !== productId)); }; const handleAddToCart = (product: ProductData) => { addItem({ key: `product-${product.id}`, product_id: product.id, name: product.name, price: parseFloat(product.sale_price || product.regular_price || product.price.replace(/[^0-9.]/g, '')), quantity: 1, image: product.image, }); toast.success(`${product.name} added to cart`); }; if (isLoading || loadingGuest) { return (

Loading wishlist...

); } // Guest mode: have product details fetched from API const hasGuestItems = !isLoggedIn && guestProducts.length > 0; const hasLoggedInItems = isLoggedIn && items.length > 0; if (!hasGuestItems && !hasLoggedInItems) { return (

My Wishlist

Your wishlist is empty

Start adding products you love to your wishlist

); } return (

My Wishlist

{isLoggedIn ? `${items.length} items` : `${guestProducts.length} items`}

{/* Guest Mode: Show full product details */} {!isLoggedIn && hasGuestItems && (

Guest Wishlist: You have {guestProducts.length} items saved locally. Login to sync your wishlist to your account.

)} {/* Guest Wishlist Items (with full product details) */} {!isLoggedIn && hasGuestItems && (
{guestProducts.map((product) => (
{product.image ? ( {product.name} ) : (
)}

{product.name}

{formatPrice(parseFloat(product.sale_price || product.regular_price || product.price.replace(/[^0-9.]/g, '')))}

{product.stock_status === 'outofstock' && (

Out of stock

)}
))}
)} {/* Logged-in Wishlist Items (full details from API) */} {isLoggedIn && hasLoggedInItems && (
{items.map((item) => (
{item.image ? ( {item.name} ) : (
)}

{item.name}

{formatPrice(parseFloat(item.price.replace(/[^0-9.]/g, '')))}

{item.stock_status === 'outofstock' && (

Out of stock

)}
))}
)}
); }