Spaces:
Running
Running
File size: 1,694 Bytes
d39feec | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | import React from 'react'
const ProductCard = ({ product, onAddToCart }) => {
const renderStars = (rating) => {
const stars = []
const fullStars = Math.floor(rating)
const hasHalfStar = rating % 1 !== 0
for (let i = 0; i < fullStars; i++) {
stars.push(<i key={i} className="bi bi-star-fill"></i>)
}
if (hasHalfStar) {
stars.push(<i key="half" className="bi bi-star-half"></i>)
}
const emptyStars = 5 - Math.ceil(rating)
for (let i = 0; i < emptyStars; i++) {
stars.push(<i key={`empty-${i}`} className="bi bi-star"></i>)
}
return stars
}
return (
<div className="card product-card h-100">
<img
src={product.image}
className="card-img-top product-image"
alt={product.title}
/>
<div className="card-body d-flex flex-column">
<h6 className="card-title text-truncate">{product.title}</h6>
<p className="card-text text-muted small flex-grow-1">
{product.description.substring(0, 100)}...
</p>
<div className="d-flex align-items-center mb-2">
<div className="rating me-2">
{renderStars(product.rating.rate)}
</div>
<small className="text-muted">({product.rating.count})</small>
</div>
<div className="d-flex justify-content-between align-items-center mt-auto">
<span className="h5 text-primary mb-0">${product.price}</span>
<button
className="btn btn-primary btn-sm"
onClick={() => onAddToCart(product)}
>
<i className="bi bi-cart-plus"></i>
</button>
</div>
</div>
</div>
)
}
export default ProductCard |