reactshop-bazaar / src /components /ProductCard.jsx
Abanee's picture
create a e-commerce website using reactjs ,bootsrap
d39feec verified
Raw History Blame Contribute Delete
1.69 kB
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