import React, { useState, useEffect } from 'react'; import { useNavigate } from 'react-router-dom'; import { Heart, ShoppingCart, Trash2, X } from 'lucide-react'; import { api } from '@/lib/api/client'; import { Button } from '@/components/ui/button'; import { formatPrice } from '@/lib/currency'; import { toast } from 'sonner'; import { useModules } from '@/hooks/useModules'; import { useModuleSettings } from '@/hooks/useModuleSettings'; import SEOHead from '@/components/SEOHead'; interface WishlistItem { product_id: number; name: string; slug: string; price: string; regular_price?: string; sale_price?: string; image?: string; on_sale?: boolean; stock_status?: string; type?: string; added_at: string; } export default function Wishlist() { const navigate = useNavigate(); const [items, setItems] = useState([]); const [loading, setLoading] = useState(true); const { isEnabled, isLoading: modulesLoading } = useModules(); const { settings: wishlistSettings } = useModuleSettings('wishlist'); useEffect(() => { if (isEnabled('wishlist')) { loadWishlist(); } }, [isEnabled]); if (modulesLoading) { return (
); } if (!isEnabled('wishlist')) { return (

Wishlist Not Available

The wishlist feature is currently disabled.

); } const loadWishlist = async () => { try { const data = await api.get('/account/wishlist'); setItems(data); } catch (error) { console.error('Failed to load wishlist:', error); toast.error('Failed to load wishlist'); } finally { setLoading(false); } }; const handleRemove = async (productId: number) => { try { await api.delete(`/account/wishlist/${productId}`); setItems(items.filter(item => item.product_id !== productId)); toast.success('Removed from wishlist'); } catch (error) { console.error('Failed to remove from wishlist:', error); toast.error('Failed to remove from wishlist'); } }; const handleAddToCart = async (item: WishlistItem) => { if (item.type === 'variable') { navigate(`/product/${item.slug}`); return; } try { await api.post('/cart/add', { product_id: item.product_id, quantity: 1, }); toast.success('Added to cart'); } catch (error) { console.error('Failed to add to cart:', error); toast.error('Failed to add to cart'); } }; const handleClearAll = async () => { if (!confirm('Are you sure you want to clear your entire wishlist?')) { return; } try { await api.post('/account/wishlist/clear', {}); setItems([]); toast.success('Wishlist cleared'); } catch (error) { console.error('Failed to clear wishlist:', error); toast.error('Failed to clear wishlist'); } }; if (loading) { return (
); } return (
{/* Header */}

My Wishlist

{items.length} {items.length === 1 ? 'item' : 'items'}

{items.length > 0 && ( )}
{/* Empty State */} {items.length === 0 ? (

Your wishlist is empty

Save your favorite products to buy them later

) : ( /* Wishlist Grid */
{items.map((item) => (
{/* Image */}
{item.name} navigate(`/product/${item.slug}`)} /> {item.on_sale && (
SALE
)}
{/* Content */}

navigate(`/product/${item.slug}`)} > {item.name}

{/* Price */}
{item.on_sale && item.regular_price ? ( <> {formatPrice(item.sale_price || item.price)} {formatPrice(item.regular_price)} ) : ( {formatPrice(item.price)} )}
{/* Actions */} {(wishlistSettings.show_add_to_cart_button ?? true) && ( )}
))}
)}
); }