import React, { useState } from "react"; import { useFavorites } from "@/contexts/FavoritesContext"; interface CatalogProductCardProps { image: string; discount: string; price: string; oldPrice: string; title: string; brand: string; articleNumber?: string; brandName?: string; artId?: string; productId?: string; offerKey?: string; currency?: string; priceElement?: React.ReactNode; onAddToCart?: (e: React.MouseEvent) => void | Promise; isInCart?: boolean; } const CatalogProductCard: React.FC = ({ image, discount, price, oldPrice, title, brand, articleNumber, brandName, artId, productId, offerKey, currency = 'RUB', priceElement, onAddToCart, isInCart = false, }) => { const { addToFavorites, removeFromFavorites, isFavorite, favorites } = useFavorites(); const [localInCart, setLocalInCart] = useState(false); const displayImage = image || 'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjEwIiBoZWlnaHQ9IjE5MCIgdmlld0JveD0iMCAwIDIxMCAxOTAiIGZpbGw9Im5vbmUiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+CjxyZWN0IHdpZHRoPSIyMTAiIGhlaWdodD0iMTkwIiBmaWxsPSIjRjNGNEY2Ii8+CjxwYXRoIGQ9Ik04NSA5NUw5NSA4NUwxMjUgMTE1TDE0MCA5NUwxNjUgMTIwSDE2NVY5MEg0NVY5MEw4NSA5NVoiIGZpbGw9IiNEMUQ1REIiLz4KPGNpcmNsZSBjeD0iNzUiIGN5PSI3NSIgcj0iMTAiIGZpbGw9IiNEMUQ1REIiLz4KPHRleHQgeD0iMTA1IiB5PSIxNTAiIGZvbnQtZmFtaWx5PSJBcmlhbCIgZm9udC1zaXplPSIxMiIgZmlsbD0iIzlDQTNBRiIgdGV4dC1hbmNob3I9Im1pZGRsZSI+Tm8gaW1hZ2U8L3RleHQ+Cjwvc3ZnPgo='; const cardUrl = articleNumber && brandName ? `/card?article=${encodeURIComponent(articleNumber)}&brand=${encodeURIComponent(brandName)}${artId ? `&artId=${artId}` : ''}` : '/card'; const isItemFavorite = isFavorite(productId, offerKey, articleNumber, brandName || brand); const handleFavoriteClick = (e: React.MouseEvent) => { e.preventDefault(); e.stopPropagation(); const numericPrice = parseFloat(price.replace(/[^\d.,]/g, '').replace(',', '.')) || 0; if (isItemFavorite) { const favoriteItem = favorites.find((fav: any) => { if (productId && fav.productId === productId) return true; if (offerKey && fav.offerKey === offerKey) return true; if (fav.article === articleNumber && fav.brand === (brandName || brand)) return true; return false; }); if (favoriteItem) { removeFromFavorites(favoriteItem.id); } } else { addToFavorites({ productId, offerKey, name: title, brand: brandName || brand, article: articleNumber || '', price: numericPrice, currency, image }); } }; const handleBuyClick = (e: React.MouseEvent) => { e.preventDefault(); e.stopPropagation(); if (!isInCart && !localInCart) { setLocalInCart(true); } if (onAddToCart) { onAddToCart(e); } else { window.location.href = cardUrl; } }; return (
{title}
{discount || ''}
{priceElement ? (
{priceElement}
) : (
{price}
)}
{oldPrice}
{title}
{brand}
); }; export default CatalogProductCard;