ProductDetail.jsx
5.45 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import React, { useState } from 'react'
import { useParams } from 'react-router-dom'
import { useCart } from '../../context/CartContext'
import LoadingSpinner from '../common/LoadingSpinner'
import toast from 'react-hot-toast'
const ProductDetail = ({ product }) => {
const { id } = useParams()
const { addToCart } = useCart()
const [selectedImage, setSelectedImage] = useState(0)
const [quantity, setQuantity] = useState(1)
const [loading, setLoading] = useState(false)
if (!product) {
return (
<div className="product-detail">
<div className="container">
<div className="product-not-found">
<h2>Product not found</h2>
</div>
</div>
</div>
)
}
const handleAddToCart = async () => {
setLoading(true)
try {
await addToCart(product._id, quantity)
toast.success('Product added to cart!')
} catch (error) {
toast.error('Failed to add product to cart')
} finally {
setLoading(false)
}
}
const images = product.images && product.images.length > 0
? product.images
: [product.image || '/placeholder-image.jpg']
return (
<div className="product-detail">
<div className="container">
<div className="product-detail-content">
<div className="product-gallery">
<div className="main-image">
<img
src={images[selectedImage]}
alt={product.name}
onError={(e) => {
e.target.src = '/placeholder-image.jpg'
}}
/>
</div>
{images.length > 1 && (
<div className="image-thumbnails">
{images.map((image, index) => (
<button
key={index}
onClick={() => setSelectedImage(index)}
className={`thumbnail ${selectedImage === index ? 'active' : ''}`}
>
<img
src={image}
alt={`${product.name} ${index + 1}`}
onError={(e) => {
e.target.src = '/placeholder-image.jpg'
}}
/>
</button>
))}
</div>
)}
</div>
<div className="product-info">
<h1>{product.name}</h1>
<div className="product-meta">
<div className="product-rating">
{Array.from({ length: 5 }).map((_, index) => (
<span
key={index}
className={`star ${index < (product.rating || 0) ? 'filled' : ''}`}
>
★
</span>
))}
<span className="rating-text">
({product.reviewCount || 0} reviews)
</span>
</div>
<div className="product-sku">
SKU: {product.sku || 'N/A'}
</div>
</div>
<div className="product-price">
<span className="current-price">${product.price}</span>
{product.originalPrice && product.originalPrice > product.price && (
<span className="original-price">${product.originalPrice}</span>
)}
</div>
<div className="product-description">
<p>{product.description}</p>
</div>
<div className="product-features">
<h3>Features</h3>
<ul>
{product.features && product.features.map((feature, index) => (
<li key={index}>{feature}</li>
))}
</ul>
</div>
<div className="product-actions">
<div className="quantity-selector">
<label htmlFor="quantity">Quantity:</label>
<select
id="quantity"
value={quantity}
onChange={(e) => setQuantity(parseInt(e.target.value))}
>
{Array.from({ length: Math.min(10, product.stock || 10) }, (_, i) => (
<option key={i + 1} value={i + 1}>
{i + 1}
</option>
))}
</select>
</div>
<button
onClick={handleAddToCart}
disabled={!product.inStock || loading}
className="add-to-cart-btn"
>
{loading ? 'Adding...' : product.inStock ? 'Add to Cart' : 'Out of Stock'}
</button>
<button className="buy-now-btn">
Buy Now
</button>
</div>
<div className="product-specs">
<div className="spec-item">
<strong>Category:</strong> {product.category}
</div>
<div className="spec-item">
<strong>Brand:</strong> {product.brand || 'N/A'}
</div>
<div className="spec-item">
<strong>Stock:</strong> {product.inStock ? `${product.stock} available` : 'Out of Stock'}
</div>
<div className="spec-item">
<strong>Shipping:</strong> Free shipping on orders over $50
</div>
</div>
</div>
</div>
</div>
</div>
)
}
export default ProductDetail