"use client"; import { useState, MouseEvent } from "react"; import Link from "next/link"; import { Button } from "@/components/ui/button"; import { Card, CardContent } from "@/components/ui/card"; import { Badge } from "@/components/ui/badge"; import { Heart, Minus, Plus } from "lucide-react"; import Image, { StaticImageData } from "next/image"; import { useAddToFavorites, useRemoveFromFavorites } from "@/lib/hooks"; import { useToast } from "@/hooks/use-toast"; type ProductCardProps = { id: number; name: string; price: number | null; struct_price_text: string; discount?: number | null; discount_text?: string | null; images: (StaticImageData | string)[]; is_favorite: boolean; labels?: { text: string; bg_color: string }[]; price_color?: string; height?: number; width?: number; button?: boolean; }; export default function ProductCard({ id, name, price, struct_price_text, discount, discount_text, images, is_favorite, labels = [], price_color = "#005bff", height = 360, width = 280, button = true, }: ProductCardProps) { const [favorite, setFavorite] = useState(is_favorite); const [cart, setCart] = useState(false); const [count, setCount] = useState(1); const { toast } = useToast(); const { mutate: addToFavorites, isPending: isAdding } = useAddToFavorites(); const { mutate: removeFromFavorites, isPending: isRemoving } = useRemoveFromFavorites(); const handleFavorite = (e: MouseEvent) => { e.preventDefault(); e.stopPropagation(); const newFavoriteState = !favorite; if (newFavoriteState) { // Добавляем в избранное addToFavorites(id, { onSuccess: () => { setFavorite(true); toast({ title: "Товар добавлен в избранное", }); }, onError: (error) => { toast({ title: "Ошибка", description: error.message, variant: "destructive", }); }, }); } else { // Удаляем из избранного removeFromFavorites(id, { onSuccess: () => { setFavorite(false); toast({ title: "Товар удален из избранного", }); }, onError: (error) => { toast({ title: "Ошибка", description: error.message, variant: "destructive", }); }, }); } }; const handleAddToCart = (e: MouseEvent) => { e.preventDefault(); e.stopPropagation(); setCart(true); }; const handleIncrement = (e: MouseEvent) => { e.preventDefault(); e.stopPropagation(); setCount((c) => c + 1); }; const handleDecrement = (e: MouseEvent) => { e.preventDefault(); e.stopPropagation(); setCount((c) => (c > 1 ? c - 1 : c)); }; const isPending = isAdding || isRemoving; return ( {/* Image Section */}
{images?.[0] && ( {name} )} {/* Favorite Button */} {/* Labels */} {labels?.length > 0 && (
{labels.map((label) => ( {label.text} ))}
)}
{/* Content */}

{struct_price_text}

{name}

{/* Buttons - закомментированы в оригинале */} {/* {button && (
{!cart ? ( ) : (
{count}
)}
)} */}
); }