anycoder-044de774 / components /ProductCard.js
mouris030's picture
Upload components/ProductCard.js with huggingface_hub
d80db3f verified
Raw History Blame Contribute Delete
4 kB
import { useState } from 'react';
export default function ProductCard({ product, onAddToCart }) {
const [isHovered, setIsHovered] = useState(false);
const [isAdded, setIsAdded] = useState(false);
const handleAddToCart = () => {
onAddToCart(product);
setIsAdded(true);
setTimeout(() => setIsAdded(false), 2000);
};
const renderStars = (rating) => {
const stars = [];
for (let i = 0; i < 5; i++) {
if (i < Math.floor(rating)) {
stars.push(
<svg key={i} className="w-4 h-4 text-yellow-400" fill="currentColor" viewBox="0 0 20 20">
<path d="M9.049 2.927c.3-.921 1.603-.921 1.902 0l1.07 3.292a1 1 0 00.95.69h3.462c.969 0 1.371 1.24.588 1.81l-2.8 2.034a1 1 0 00-.364 1.118l1.07 3.292c.3.921-.755 1.688-1.54 1.118l-2.8-2.034a1 1 0 00-1.175 0l-2.8 2.034c-.784.57-1.838-.197-1.539-1.118l1.07-3.292a1 1 0 00-.364-1.118L2.98 8.72c-.783-.57-.38-1.81.588-1.81h3.461a1 1 0 00.951-.69l1.07-3.292z"/>
</svg>
);
} else {
stars.push(
<svg key={i} className="w-4 h-4 text-gray-300" fill="currentColor" viewBox="0 0 20 20">
<path d="M9.049 2.927c.3-.921 1.603-.921 1.902 0l1.07 3.292a1 1 0 00.95.69h3.462c.969 0 1.371 1.24.588 1.81l-2.8 2.034a1 1 0 00-.364 1.118l1.07 3.292c.3.921-.755 1.688-1.54 1.118l-2.8-2.034a1 1 0 00-1.175 0l-2.8 2.034c-.784.57-1.838-.197-1.539-1.118l1.07-3.292a1 1 0 00-.364-1.118L2.98 8.72c-.783-.57-.38-1.81.588-1.81h3.461a1 1 0 00.951-.69l1.07-3.292z"/>
</svg>
);
}
}
return stars;
};
return (
<div
className={`bg-white rounded-lg shadow-md hover:shadow-xl transition-all duration-300 overflow-hidden ${
isHovered ? 'transform -translate-y-1' : ''
}`}
onMouseEnter={() => setIsHovered(true)}
onMouseLeave={() => setIsHovered(false)}
>
{/* Product Image */}
<div className="relative h-48 bg-gray-100 flex items-center justify-center p-4">
<img
src={product.image}
alt={product.name}
className="max-h-full max-w-full object-contain hover:scale-105 transition-transform duration-300"
/>
{product.isPrime && (
<div className="absolute top-2 left-2 bg-white px-2 py-1 rounded shadow">
<span className="text-blue-500 font-bold text-xs">prime</span>
</div>
)}
</div>
{/* Product Info */}
<div className="p-4">
<h3 className="text-sm font-medium text-gray-800 line-clamp-2 mb-2 hover:text-orange-600 cursor-pointer">
{product.name}
</h3>
{/* Rating */}
<div className="flex items-center space-x-1 mb-2">
{renderStars(product.rating)}
<span className="text-xs text-blue-500 ml-1 hover:underline cursor-pointer">
{product.reviews}
</span>
</div>
{/* Price */}
<div className="flex items-baseline space-x-1 mb-3">
<span className="text-xs text-gray-500">$</span>
<span className="text-2xl font-bold text-gray-900">{product.price}</span>
{product.originalPrice && (
<>
<span className="text-xs text-gray-400 line-through">{product.originalPrice}</span>
<span className="text-xs text-red-500">({Math.round((1 - product.price / product.originalPrice) * 100)}% off)</span>
</>
)}
</div>
{/* Delivery */}
<p className="text-xs text-gray-600 mb-3">
{product.delivery || 'FREE delivery'}
</p>
{/* Add to Cart Button */}
<button
onClick={handleAddToCart}
className={`w-full py-2 px-4 rounded text-sm font-medium transition-all duration-200 ${
isAdded
? 'bg-green-500 text-white'
: 'bg-amazon-yellow hover:bg-yellow-500 text-gray-900'
}`}
>
{isAdded ? '✓ Added to Cart' : 'Add to Cart'}
</button>
</div>
</div>
);
}