Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 83x 83x 83x 83x 83x 83x 83x 83x 83x 83x 83x 159x 159x 83x 83x 83x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 83x 83x 83x 83x 83x 83x 1x 1x 1x 1x 83x 83x 83x 83x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 83x 83x 83x 83x 83x 83x 83x 83x 83x 83x 83x 83x 83x 1x 1x | 'use client';
import { useCallback } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { AppDispatch, RootState } from '@/redux/store';
import {
addToWishlistAsync,
removeFromWishlistAsync,
addItemToWishlistOptimistic,
removeItemFromWishlistOptimistic } from '@/redux/features/wishlistSlice';
import { IconButton } from '@/components/ui';
import { Icon } from '@/components/ui/icons';
import { eventTracking } from '@/lib/event-tracking';
import { useToast } from '@/hooks/useToast';
import { clientLogger } from '@/lib/logging/clientLogger';
import { cn } from '@/lib/core';
import { useFunnelSteps } from '@/hooks/useFunnelTracking';
/**
* WishlistButton Component Props
*/
export interface WishlistButtonProps {
/** Product data (minimum required fields) */
product: {
id: number;
title: string;
price: number;
discountedPrice: number;
imgs?: {
thumbnails: string[];
previews: string[];
};
};
/** Button variant */
variant?: 'icon' | 'icon-large' | 'button';
/** Additional CSS classes */
className?: string;
/** Show toast notifications */
showToast?: boolean;
}
/**
* WishlistButton - Reusable wishlist toggle button component
*
* Features:
* - Automatic state management (checks if product is in wishlist)
* - Optimistic UI updates
* - API persistence to database
* - Toast notifications
* - Filled/unfilled heart icon based on state
* - Multiple variants (icon, icon-large, button)
*
* @example
* ```tsx
* // Icon variant (default) - used in ProductCard
* <WishlistButton product={product} />
*
* // Large icon variant - used in product details page
* <WishlistButton product={product} variant="icon-large" />
*
* // Button variant with text
* <WishlistButton product={product} variant="button" />
* ```
*/
export function WishlistButton({
product,
variant = 'icon',
className,
showToast = true}: WishlistButtonProps) {
const dispatch = useDispatch<AppDispatch>();
const { addToast } = useToast();
const { trackStep } = useFunnelSteps();
// Check if product is in wishlist (with defensive check)
const isInWishlist = useSelector((state: RootState) => {
const items = state.wishlistReducer?.items;
return Array.isArray(items) && items.some((item) => item.id === product.id);
});
const handleAddToWishlist = useCallback(async () => {
// Optimistically add to local state
dispatch(
addItemToWishlistOptimistic({
id: product.id,
title: product.title,
price: product.price,
discountedPrice: product.discountedPrice,
imgs: product.imgs,
status: 'in_stock',
quantity: 1})
);
// Track event (legacy)
eventTracking.addToWishlist(product.id, product.title);
// Track add_to_wishlist for engagement funnel
trackStep('engagement', 'add_to_wishlist', {
product_id: product.id,
product_name: product.title});
// Show success toast immediately (optimistic)
if (showToast) {
addToast({
message: `${product.title} added to wishlist`,
variant: 'success',
duration: 3000});
}
try {
// Call API to persist to database
const result = await dispatch(addToWishlistAsync(product.id));
if (addToWishlistAsync.rejected.match(result)) {
// API call failed - check if login required
if (result.payload === 'LOGIN_REQUIRED') {
// Remove from local state since user isn't logged in
dispatch(removeItemFromWishlistOptimistic(product.id));
if (showToast) {
addToast({
message: 'Please sign in to save your wishlist',
variant: 'warning',
duration: 4000});
}
} else {
// Other error - remove from local state
dispatch(removeItemFromWishlistOptimistic(product.id));
if (showToast) {
addToast({
message: 'Failed to save to wishlist',
variant: 'error',
duration: 3000});
}
}
}
} catch (error) {
clientLogger.error('Error adding to wishlist', error instanceof Error ? error : new Error(String(error)));
// Remove optimistically added item on error
dispatch(removeItemFromWishlistOptimistic(product.id));
if (showToast) {
addToast({
message: 'Failed to add item to wishlist',
variant: 'error',
duration: 3000});
}
}
}, [product, dispatch, addToast, showToast, trackStep]);
const handleRemoveFromWishlist = useCallback(async () => {
// Optimistically remove from local state
dispatch(removeItemFromWishlistOptimistic(product.id));
// Show success toast immediately (optimistic)
if (showToast) {
addToast({
message: `${product.title} removed from wishlist`,
variant: 'success',
duration: 3000});
}
try {
// Call API to remove from database
const result = await dispatch(removeFromWishlistAsync(product.id));
if (removeFromWishlistAsync.rejected.match(result)) {
// API call failed - re-add to local state
dispatch(
addItemToWishlistOptimistic({
id: product.id,
title: product.title,
price: product.price,
discountedPrice: product.discountedPrice,
imgs: product.imgs,
status: 'in_stock',
quantity: 1})
);
if (showToast) {
addToast({
message: 'Failed to remove from wishlist',
variant: 'error',
duration: 3000});
}
}
} catch (error) {
clientLogger.error('Error removing from wishlist', error instanceof Error ? error : new Error(String(error)));
// Re-add optimistically removed item on error
dispatch(
addItemToWishlistOptimistic({
id: product.id,
title: product.title,
price: product.price,
discountedPrice: product.discountedPrice,
imgs: product.imgs,
status: 'in_stock',
quantity: 1})
);
if (showToast) {
addToast({
message: 'Failed to remove from wishlist',
variant: 'error',
duration: 3000});
}
}
}, [product, dispatch, addToast, showToast]);
const handleToggle = useCallback(() => {
if (isInWishlist) {
handleRemoveFromWishlist();
} else {
handleAddToWishlist();
}
}, [isInWishlist, handleAddToWishlist, handleRemoveFromWishlist]);
// Icon variant (default) - used in ProductCard
if (variant === 'icon') {
return (
<IconButton
icon={isInWishlist ? 'heart-filled' : 'heart'}
onClick={handleToggle}
aria-label={isInWishlist ? `Remove ${product.title} from wishlist` : `Add ${product.title} to wishlist`}
aria-pressed={isInWishlist}
className={cn(
isInWishlist ? 'text-red-500 hover:text-red-600' : '',
className
)}
/>
);
}
// Large icon variant - used in product details page
if (variant === 'icon-large') {
return (
<button
type="button"
onClick={handleToggle}
aria-label={isInWishlist ? `Remove ${product.title} from wishlist` : `Add ${product.title} to wishlist`}
aria-pressed={isInWishlist}
className={cn(
'flex items-center justify-center w-12 h-12 rounded-md border ease-out duration-200',
isInWishlist
? 'text-red-500 border-red-300 bg-red-50 hover:bg-red-100'
: 'border-gray-3 hover:text-white hover:bg-dark hover:border-transparent',
className
)}
>
<Icon
name={isInWishlist ? 'heart-filled' : 'heart'}
size={24}
className="fill-current"
/>
</button>
);
}
// Button variant with text
return (
<button
type="button"
onClick={handleToggle}
className={cn(
'inline-flex items-center gap-2 px-4 py-2 rounded-md border ease-out duration-200',
isInWishlist
? 'text-red-500 border-red-300 bg-red-50 hover:bg-red-100'
: 'border-gray-3 hover:text-white hover:bg-dark hover:border-transparent',
className
)}
>
<Icon
name={isInWishlist ? 'heart-filled' : 'heart'}
size={20}
className="fill-current"
/>
<span>{isInWishlist ? 'In Wishlist' : 'Add to Wishlist'}</span>
</button>
);
}
export default WishlistButton;
|