UserProfile.jsx
5.61 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
import React, { useState } from 'react'
import { useAuth } from '../context/AuthContext'
import { userService } from '../services/api/userService'
import LoadingSpinner from '../components/common/LoadingSpinner'
import toast from 'react-hot-toast'
const UserProfile = () => {
const { user, updateProfile } = useAuth()
const [activeTab, setActiveTab] = useState('profile')
const [loading, setLoading] = useState(false)
const [formData, setFormData] = useState({
name: user?.name || '',
email: user?.email || '',
phone: user?.phone || '',
address: user?.address || ''
})
const handleChange = (e) => {
setFormData({
...formData,
[e.target.name]: e.target.value
})
}
const handleProfileSubmit = async (e) => {
e.preventDefault()
setLoading(true)
try {
const result = await updateProfile(formData)
if (result.success) {
toast.success('Profile updated successfully!')
} else {
toast.error(result.message)
}
} catch (error) {
toast.error('Failed to update profile')
} finally {
setLoading(false)
}
}
if (!user) {
return <LoadingSpinner />
}
return (
<div className="user-profile">
<div className="container">
<div className="profile-header">
<h1>My Account</h1>
<p>Welcome back, {user.name}!</p>
</div>
<div className="profile-content">
<div className="profile-sidebar">
<nav className="profile-nav">
<button
onClick={() => setActiveTab('profile')}
className={`nav-item ${activeTab === 'profile' ? 'active' : ''}`}
>
Profile Information
</button>
<button
onClick={() => setActiveTab('orders')}
className={`nav-item ${activeTab === 'orders' ? 'active' : ''}`}
>
Order History
</button>
<button
onClick={() => setActiveTab('addresses')}
className={`nav-item ${activeTab === 'addresses' ? 'active' : ''}`}
>
Address Book
</button>
<button
onClick={() => setActiveTab('security')}
className={`nav-item ${activeTab === 'security' ? 'active' : ''}`}
>
Security
</button>
</nav>
</div>
<div className="profile-main">
{activeTab === 'profile' && (
<div className="profile-section">
<h2>Profile Information</h2>
<form onSubmit={handleProfileSubmit}>
<div className="form-row">
<div className="form-group">
<label htmlFor="name">Full Name</label>
<input
type="text"
id="name"
name="name"
value={formData.name}
onChange={handleChange}
required
/>
</div>
<div className="form-group">
<label htmlFor="email">Email Address</label>
<input
type="email"
id="email"
name="email"
value={formData.email}
onChange={handleChange}
required
/>
</div>
</div>
<div className="form-group">
<label htmlFor="phone">Phone Number</label>
<input
type="tel"
id="phone"
name="phone"
value={formData.phone}
onChange={handleChange}
/>
</div>
<div className="form-group">
<label htmlFor="address">Address</label>
<textarea
id="address"
name="address"
value={formData.address}
onChange={handleChange}
rows="3"
/>
</div>
<button
type="submit"
className="save-button"
disabled={loading}
>
{loading ? 'Saving...' : 'Save Changes'}
</button>
</form>
</div>
)}
{activeTab === 'orders' && (
<div className="profile-section">
<h2>Order History</h2>
<p>View and manage your recent orders.</p>
<a href="/orders" className="view-orders-button">
View All Orders
</a>
</div>
)}
{activeTab === 'addresses' && (
<div className="profile-section">
<h2>Address Book</h2>
<p>Manage your shipping addresses.</p>
{/* Address management component would go here */}
</div>
)}
{activeTab === 'security' && (
<div className="profile-section">
<h2>Security Settings</h2>
<p>Update your password and security preferences.</p>
{/* Password change component would go here */}
</div>
)}
</div>
</div>
</div>
</div>
)
}
export default UserProfile