import React, { useState } from "react"; import { useCart } from "@/contexts/CartContext"; interface ProductListCardProps { id?: string; productId?: string; offerKey?: string; image: string; title: string; brand: string; price: string; oldPrice?: string; discount?: string; rating?: number; stock?: string; delivery?: string; address?: string; recommended?: boolean; isExternal?: boolean; currency?: string; deliveryTime?: string; warehouse?: string; supplier?: string; } const ProductListCard: React.FC = ({ id, productId, offerKey, image, title, brand, price, oldPrice, discount, rating = 4.8, stock = "444 шт", delivery = "Сегодня с 18:00", address = "Москва ЦС (Новая Рига)", recommended = false, isExternal = false, currency = "RUB", deliveryTime, warehouse, supplier, }) => { const [count, setCount] = useState(1); const { addItem } = useCart(); // Функция для парсинга цены из строки const parsePrice = (priceStr: string): number => { const cleanPrice = priceStr.replace(/[^\d.,]/g, '').replace(',', '.'); return parseFloat(cleanPrice) || 0; }; // Функция для парсинга количества в наличии const parseStock = (stockStr: string): number => { const match = stockStr.match(/\d+/); return match ? parseInt(match[0]) : 0; }; const handleAddToCart = () => { const availableStock = parseStock(stock); // Проверяем наличие if (count > availableStock) { alert(`Недостаточно товара в наличии. Доступно: ${availableStock} шт.`); return; } const numericPrice = parsePrice(price); const numericOldPrice = oldPrice ? parsePrice(oldPrice) : undefined; addItem({ productId: productId, offerKey: offerKey, name: title, description: `${brand} - ${title}`, brand: brand, price: numericPrice, originalPrice: numericOldPrice, currency: currency, quantity: count, deliveryTime: deliveryTime || delivery, warehouse: warehouse || address, supplier: supplier, isExternal: isExternal, image: image, }); // Показываем уведомление о добавлении alert(`Товар "${title}" добавлен в корзину (${count} шт.)`); }; return (
Рейтинг
{rating}
{stock}
{delivery}
{recommended && ( <>
Рекомендуем
Рекомендуем
)}
{price}
setCount(Math.max(1, count - 1))}> -
{count}
{ const availableStock = parseStock(stock); if (count < availableStock) { setCount(count + 1); } else { alert(`Максимальное количество: ${availableStock} шт.`); } }}> +
); }; export default ProductListCard;