ProductList.jsx
6.1 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import React, { useState, useEffect } from 'react';
import { Link } from 'react-router-dom';
import { useCart } from '../context/CartContext';
const ProductList = () => {
const [products, setProducts] = useState([]);
const [loading, setLoading] = useState(true);
const [filter, setFilter] = useState('all');
const { addToCart } = useCart();
useEffect(() => {
// 模拟获取产品数据
const mockProducts = [
{
id: 1,
name: 'Wireless Bluetooth Headphones',
price: 99.99,
image: '/api/placeholder/300/300',
category: 'electronics',
description: 'High-quality wireless headphones with noise cancellation.'
},
{
id: 2,
name: 'Smart Watch',
price: 199.99,
image: '/api/placeholder/300/300',
category: 'electronics',
description: 'Feature-rich smartwatch with health monitoring.'
},
{
id: 3,
name: 'Cotton T-Shirt',
price: 24.99,
image: '/api/placeholder/300/300',
category: 'clothing',
description: 'Comfortable cotton t-shirt available in multiple colors.'
},
{
id: 4,
name: 'Running Shoes',
price: 129.99,
image: '/api/placeholder/300/300',
category: 'sports',
description: 'Professional running shoes with cushion technology.'
},
{
id: 5,
name: 'Coffee Maker',
price: 79.99,
image: '/api/placeholder/300/300',
category: 'home',
description: 'Automatic coffee maker for perfect brew every time.'
},
{
id: 6,
name: 'Laptop Backpack',
price: 49.99,
image: '/api/placeholder/300/300',
category: 'electronics',
description: 'Durable laptop backpack with multiple compartments.'
}
];
setTimeout(() => {
setProducts(mockProducts);
setLoading(false);
}, 1000);
}, []);
const filteredProducts = filter === 'all'
? products
: products.filter(product => product.category === filter);
const handleAddToCart = (product) => {
addToCart(product);
alert(`${product.name} added to cart!`);
};
if (loading) {
return (
<div style={loadingStyle}>
<div>Loading products...</div>
</div>
);
}
return (
<div style={containerStyle}>
<div style={headerStyle}>
<h1>Our Products</h1>
<div style={filterStyle}>
<select
value={filter}
onChange={(e) => setFilter(e.target.value)}
style={selectStyle}
>
<option value="all">All Categories</option>
<option value="electronics">Electronics</option>
<option value="clothing">Clothing</option>
<option value="home">Home & Garden</option>
<option value="sports">Sports</option>
</select>
</div>
</div>
<div style={gridStyle}>
{filteredProducts.map(product => (
<div key={product.id} style={cardStyle}>
<div style={imagePlaceholderStyle}>
{product.name}
</div>
<div style={cardContentStyle}>
<h3 style={productNameStyle}>{product.name}</h3>
<p style={descriptionStyle}>{product.description}</p>
<div style={priceStyle}>${product.price}</div>
<div style={buttonGroupStyle}>
<Link to={`/products/${product.id}`} style={detailButtonStyle}>
View Details
</Link>
<button
onClick={() => handleAddToCart(product)}
style={addToCartButtonStyle}
>
Add to Cart
</button>
</div>
</div>
</div>
))}
</div>
{filteredProducts.length === 0 && (
<div style={emptyStyle}>
<p>No products found in this category.</p>
</div>
)}
</div>
);
};
const containerStyle = {
maxWidth: '1200px',
margin: '0 auto',
padding: '2rem 1rem',
};
const headerStyle = {
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
marginBottom: '2rem',
flexWrap: 'wrap',
gap: '1rem',
};
const filterStyle = {
display: 'flex',
gap: '1rem',
alignItems: 'center',
};
const selectStyle = {
padding: '0.5rem 1rem',
border: '1px solid #ddd',
borderRadius: '4px',
fontSize: '1rem',
};
const gridStyle = {
display: 'grid',
gridTemplateColumns: 'repeat(auto-fill, minmax(280px, 1fr))',
gap: '2rem',
};
const cardStyle = {
backgroundColor: 'white',
borderRadius: '8px',
boxShadow: '0 2px 8px rgba(0, 0, 0, 0.1)',
overflow: 'hidden',
transition: 'transform 0.3s, box-shadow 0.3s',
};
const imagePlaceholderStyle = {
height: '200px',
backgroundColor: '#f5f5f5',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
color: '#666',
fontSize: '0.9rem',
};
const cardContentStyle = {
padding: '1.5rem',
};
const productNameStyle = {
fontSize: '1.2rem',
marginBottom: '0.5rem',
color: '#333',
};
const descriptionStyle = {
color: '#666',
fontSize: '0.9rem',
marginBottom: '1rem',
lineHeight: '1.4',
};
const priceStyle = {
fontSize: '1.3rem',
fontWeight: 'bold',
color: '#2c5aa0',
marginBottom: '1rem',
};
const buttonGroupStyle = {
display: 'flex',
gap: '0.5rem',
};
const detailButtonStyle = {
flex: 1,
padding: '0.5rem 1rem',
backgroundColor: 'transparent',
color: '#2c5aa0',
border: '1px solid #2c5aa0',
borderRadius: '4px',
textDecoration: 'none',
textAlign: 'center',
fontSize: '0.9rem',
transition: 'background-color 0.3s',
};
const addToCartButtonStyle = {
flex: 1,
padding: '0.5rem 1rem',
backgroundColor: '#2c5aa0',
color: 'white',
border: 'none',
borderRadius: '4px',
cursor: 'pointer',
fontSize: '0.9rem',
transition: 'background-color 0.3s',
};
const loadingStyle = {
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
height: '50vh',
fontSize: '1.2rem',
};
const emptyStyle = {
textAlign: 'center',
padding: '4rem 1rem',
color: '#666',
};
export default ProductList;