This repository has been archived on 2025-07-07. You can view files and clone it, but cannot push or open issues or pull requests.
Files
eternos/frontend/style/components/product-card.tsx
2025-01-11 09:54:09 +03:00

94 lines
2.8 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use client"
import Image from "next/image"
import Link from "next/link"
import { Heart, ShoppingCart } from 'lucide-react'
import { Button } from "./ui/button"
import { useCart } from "@/contexts/cart-context"
import { useFavorites } from "@/contexts/favorites-context"
import { Badge } from "./ui/badge"
interface ProductCardProps {
product: {
id: number
title: string
price: number
image: string
category?: string
licenseType?: string
}
}
export function ProductCard({ product }: ProductCardProps) {
const { addToCart, removeFromCart } = useCart()
const { addToFavorites, removeFromFavorites, isFavorite } = useFavorites()
const handleAddToCart = (e: React.MouseEvent) => {
e.preventDefault()
addToCart({
id: product.id,
title: product.title,
price: product.price,
})
}
const handleRemoveFromCart = (e: React.MouseEvent) => {
e.preventDefault()
removeFromCart(product.id)
}
const handleToggleFavorite = (e: React.MouseEvent) => {
e.preventDefault()
if (isFavorite(product.id)) {
removeFromFavorites(product.id)
} else {
addToFavorites({
id: product.id,
title: product.title,
price: product.price,
})
}
}
return (
<Link href={`/product/${product.id}`} className="group bg-white rounded-lg p-4 transition-shadow hover:shadow-lg block flex flex-col h-full">
<div className="relative aspect-square mb-4">
<Image
src={product.image}
alt={product.title}
fill
className="object-cover rounded-lg"
/>
<Button
variant="ghost"
size="icon"
className="absolute top-2 right-2 opacity-0 group-hover:opacity-100 transition-opacity"
onClick={handleToggleFavorite}
>
<Heart className={`h-5 w-5 ${isFavorite(product.id) ? 'fill-red-500 text-red-500' : ''}`} />
</Button>
</div>
<div className="flex flex-col flex-grow">
<h3 className="text-sm font-medium line-clamp-2 mb-2 flex-grow">{product.title}</h3>
<div className="flex items-center justify-between mb-4">
<span className="text-xl font-bold">{product.price} </span>
{product.category === 'software' && (
<Badge variant="secondary">
{product.licenseType === 'subscription' ? 'Подписка' : 'Бессрочная'}
</Badge>
)}
</div>
<div className="flex gap-2 mt-auto">
<Button onClick={handleAddToCart} className="flex-1">
<ShoppingCart className="mr-2 h-4 w-4" /> В корзину
</Button>
<Button onClick={handleRemoveFromCart} variant="outline" size="icon">
-
</Button>
</div>
</div>
</Link>
)
}