-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.html
More file actions
210 lines (181 loc) · 7.41 KB
/
user.html
File metadata and controls
210 lines (181 loc) · 7.41 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>User Dashboard</title>
<style>
body {
font-family: 'Poppins', sans-serif;
background: url('https://i2.wp.com/www.onegreenplanet.org/wp-content/uploads/2018/01/vegan-grocery.jpg?resize=1600%2C1000') no-repeat center center fixed;
background-size: cover;
margin: 0;
padding: 0;
}
.dashboard {
max-width: 500px;
margin: 5% auto;
background: rgba(255, 255, 255, 0.8);
padding: 2rem;
border-radius: 15px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
text-align: center;
}
.avatar {
width: 100px;
height: 100px;
background: #D7263D;
color: white;
font-size: 2rem;
display: flex;
justify-content: center;
align-items: center;
border-radius: 50%;
margin: 0 auto 1rem;
}
input, textarea {
width: 100%;
padding: 12px;
margin: 10px 0;
border-radius: 8px;
border: 1px solid #ccc;
}
button {
background: #D7263D;
color: white;
padding: 12px;
border: none;
border-radius: 8px;
cursor: pointer;
}
button:hover {
background: #E76F51;
}
.logout {
margin-top: 1rem;
display: block;
color: #555;
text-decoration: underline;
cursor: pointer;
}
.saved-address {
margin-top: 20px;
padding: 15px;
background: rgba(255, 255, 255, 0.9);
border-radius: 8px;
text-align: left;
}
</style>
</head>
<body>
<div class="dashboard">
<div class="avatar" id="user-avatar">👤</div>
<h2 id="username-display">Welcome, User!</h2>
<form id="address-form">
<label for="address">Update Your Address:</label>
<textarea id="address" rows="4" placeholder="Enter your delivery address..."></textarea>
<button type="submit">Save Address</button>
</form>
<div class="saved-address" id="saved-address-display">
<h3>Your Saved Address:</h3>
<p id="current-address">No address saved yet.</p>
</div>
<p class="logout" onclick="logout()">🔓 Logout</p>
</div>
<script>
// Simple debugging function to log localStorage state
function debugLocalStorage(message) {
console.log(message);
console.log('LoggedInUser:', localStorage.getItem('loggedInUser'));
console.log('UserAddresses:', localStorage.getItem('userAddresses'));
console.log('Users:', localStorage.getItem('users'));
}
// Check if user is logged in - retrieve from localStorage
const user = JSON.parse(localStorage.getItem('loggedInUser'));
debugLocalStorage('On page load:');
if (!user) {
window.location.href = 'login.html'; // Redirect to login if no user is logged in
} else {
// Display username and avatar
document.getElementById('username-display').textContent = `Welcome, ${user.username}!`;
document.getElementById('user-avatar').textContent = user.username[0].toUpperCase();
// Load and display saved address
loadSavedAddress();
}
// Function to load the saved address
function loadSavedAddress() {
// First try to get from the dedicated addresses storage
const addressesStr = localStorage.getItem('userAddresses');
if (addressesStr) {
const addresses = JSON.parse(addressesStr);
if (addresses[user.username]) {
document.getElementById('current-address').textContent = addresses[user.username];
document.getElementById('address').value = addresses[user.username];
return;
}
}
// If not found there, check if it's in the user object directly
if (user.address) {
document.getElementById('current-address').textContent = user.address;
document.getElementById('address').value = user.address;
// Also save it to the dedicated storage for consistency
saveToAddressStorage(user.address);
}
}
// Helper function to save address to the dedicated storage
function saveToAddressStorage(address) {
let addresses = {};
const addressesStr = localStorage.getItem('userAddresses');
if (addressesStr) {
try {
addresses = JSON.parse(addressesStr);
} catch (e) {
console.error('Error parsing addresses:', e);
addresses = {};
}
}
addresses[user.username] = address;
localStorage.setItem('userAddresses', JSON.stringify(addresses));
}
// Address Update Form Submission
document.getElementById('address-form').addEventListener('submit', function (e) {
e.preventDefault();
const newAddress = document.getElementById('address').value.trim();
if (newAddress === '') {
alert('Please enter an address before saving.');
return;
}
debugLocalStorage('Before saving:');
// 1. Update the user object in memory and localStorage
user.address = newAddress;
localStorage.setItem('loggedInUser', JSON.stringify(user));
// 2. Save to dedicated address storage
saveToAddressStorage(newAddress);
// 3. Update the users array with the new address
const usersStr = localStorage.getItem('users');
if (usersStr) {
try {
const users = JSON.parse(usersStr);
const updatedUsers = users.map(u => {
if (u.username === user.username) {
return { ...u, address: newAddress };
}
return u;
});
localStorage.setItem('users', JSON.stringify(updatedUsers));
} catch (e) {
console.error('Error updating users array:', e);
}
}
// 4. Update the displayed address
document.getElementById('current-address').textContent = newAddress;
debugLocalStorage('After saving:');
alert('✅ Address updated successfully!');
});
// Logout function
function logout() {
localStorage.removeItem('loggedInUser');
window.location.href = 'login.html'; // Redirect to login page
}
</script>
</body>
</html>