OrderHistory.jsx
5.99 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
import React, { useState, useEffect } from 'react'
import { Link } from 'react-router-dom'
import { orderService } from '../services/api/orderService'
import LoadingSpinner from '../components/common/LoadingSpinner'
import { formatDate, formatPrice } from '../utils/formatters'
const OrderHistory = () => {
const [orders, setOrders] = useState([])
const [loading, setLoading] = useState(true)
const [error, setError] = useState(null)
const [filters, setFilters] = useState({
status: '',
date: ''
})
useEffect(() => {
loadOrders()
}, [filters])
const loadOrders = async () => {
try {
setLoading(true)
const response = await orderService.getOrders(filters)
setOrders(response.orders || [])
} catch (err) {
setError('Failed to load orders')
console.error('Order history error:', err)
} finally {
setLoading(false)
}
}
const getStatusColor = (status) => {
const statusColors = {
pending: 'status-pending',
confirmed: 'status-confirmed',
shipped: 'status-shipped',
delivered: 'status-delivered',
cancelled: 'status-cancelled'
}
return statusColors[status] || 'status-default'
}
const handleFilterChange = (key, value) => {
setFilters(prev => ({
...prev,
[key]: value
}))
}
const clearFilters = () => {
setFilters({
status: '',
date: ''
})
}
if (loading) {
return <LoadingSpinner />
}
if (error) {
return (
<div className="order-history">
<div className="container">
<div className="error-state">
<h2>Error Loading Orders</h2>
<p>{error}</p>
<button onClick={loadOrders} className="retry-button">
Try Again
</button>
</div>
</div>
</div>
)
}
return (
<div className="order-history">
<div className="container">
<div className="page-header">
<h1>Order History</h1>
<p>View your past orders and their status</p>
</div>
<div className="order-filters">
<div className="filter-group">
<label htmlFor="status">Status</label>
<select
id="status"
value={filters.status}
onChange={(e) => handleFilterChange('status', e.target.value)}
>
<option value="">All Status</option>
<option value="pending">Pending</option>
<option value="confirmed">Confirmed</option>
<option value="shipped">Shipped</option>
<option value="delivered">Delivered</option>
<option value="cancelled">Cancelled</option>
</select>
</div>
<div className="filter-group">
<label htmlFor="date">Date</label>
<select
id="date"
value={filters.date}
onChange={(e) => handleFilterChange('date', e.target.value)}
>
<option value="">All Time</option>
<option value="30">Last 30 days</option>
<option value="90">Last 90 days</option>
<option value="365">Last year</option>
</select>
</div>
<button onClick={clearFilters} className="clear-filters">
Clear Filters
</button>
</div>
{orders.length === 0 ? (
<div className="empty-orders">
<h2>No orders found</h2>
<p>You haven't placed any orders yet.</p>
<Link to="/products" className="shop-now-button">
Start Shopping
</Link>
</div>
) : (
<div className="orders-list">
{orders.map(order => (
<div key={order._id} className="order-card">
<div className="order-header">
<div className="order-info">
<h3>
<Link to={`/orders/${order._id}`}>
Order #{order.orderNumber}
</Link>
</h3>
<p className="order-date">
Placed on {formatDate(order.createdAt)}
</p>
</div>
<div className="order-status">
<span className={`status-badge ${getStatusColor(order.status)}`}>
{order.status.charAt(0).toUpperCase() + order.status.slice(1)}
</span>
</div>
</div>
<div className="order-items-preview">
{order.items.slice(0, 3).map((item, index) => (
<div key={item._id} className="preview-item">
<img
src={item.product.image}
alt={item.product.name}
onError={(e) => {
e.target.src = '/placeholder-image.jpg'
}}
/>
{index === 2 && order.items.length > 3 && (
<div className="more-items">+{order.items.length - 3} more</div>
)}
</div>
))}
</div>
<div className="order-footer">
<div className="order-total">
<strong>Total: {formatPrice(order.total)}</strong>
</div>
<div className="order-actions">
<Link
to={`/orders/${order._id}`}
className="view-order-button"
>
View Details
</Link>
{order.status === 'pending' && (
<button className="cancel-order-button">
Cancel Order
</button>
)}
</div>
</div>
</div>
))}
</div>
)}
</div>
</div>
)
}
export default OrderHistory