forked from taranjeetsingh9/PetConnectBackend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile.html
More file actions
174 lines (155 loc) · 6.28 KB
/
profile.html
File metadata and controls
174 lines (155 loc) · 6.28 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>User Profile</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-50 min-h-screen flex items-center justify-center p-6">
<div class="w-full max-w-2xl bg-white shadow-xl rounded-xl p-6">
<h1 class="text-3xl font-bold text-indigo-700 mb-6 text-center">My Profile</h1>
<!-- Profile View -->
<div id="profileView" class="space-y-4">
<p><b>Name:</b> <span id="viewName"></span></p>
<p><b>Email:</b> <span id="viewEmail"></span></p>
<p><b>Location:</b> <span id="viewLocation"></span></p>
<p><b>Role:</b> <span id="viewRole"></span></p>
<div id="viewExtras"></div>
<button id="editBtn" class="bg-indigo-600 text-white px-4 py-2 rounded-lg hover:bg-indigo-700">Edit Profile</button>
</div>
<!-- Profile Edit -->
<form id="profileEdit" class="hidden space-y-4">
<input type="text" id="editName" placeholder="Name" class="w-full p-3 border rounded-lg" />
<input type="email" id="editEmail" readonly class="w-full p-3 border rounded-lg bg-gray-100" />
<input type="text" id="editLocation" placeholder="Location" class="w-full p-3 border rounded-lg" />
<select id="editRole" class="w-full p-3 border rounded-lg">
<option value="adopter">Adopter</option>
<option value="staff">Staff</option>
<option value="vet">Vet</option>
<option value="trainer">Trainer</option>
</select>
<!-- Role-specific extra fields -->
<div id="editExtras"></div>
<div class="flex justify-between">
<button type="submit" class="bg-green-600 text-white px-4 py-2 rounded-lg hover:bg-green-700">Save</button>
<button type="button" id="cancelBtn" class="bg-gray-400 text-white px-4 py-2 rounded-lg hover:bg-gray-500">Cancel</button>
</div>
</form>
<div class="mt-6 text-center">
<button
onclick="window.location.href='auth.html'"
class="bg-gray-500 text-white px-4 py-2 rounded hover:bg-gray-600">
Back to Dashboard
</button>
</div>
</div>
<script>
const USER_API_URL = "http://localhost:5001/api/users";
let token = localStorage.getItem("token");
let currentUser = null;
const fetchWithAuth = (url, opt={}) =>
fetch(url, {
...opt,
headers: {
"Content-Type": "application/json",
"x-auth-token": token,
...opt.headers
}
});
// Load profile
async function loadProfile() {
if (!token) return window.location.href = "auth.html";
try {
const res = await fetchWithAuth(`${USER_API_URL}/me`);
const user = await res.json();
if (res.ok) {
currentUser = user;
renderProfile(user);
} else {
alert(user.msg || "Session expired");
localStorage.removeItem("token");
window.location.href = "auth.html";
}
} catch (err) {
console.error(err);
alert("Error loading profile");
}
}
function renderProfile(user) {
document.getElementById("viewName").innerText = user.name || "N/A";
document.getElementById("viewEmail").innerText = user.email;
document.getElementById("viewLocation").innerText = user.location || "N/A";
document.getElementById("viewRole").innerText = user.role;
// Role-specific view extras
const extras = document.getElementById("viewExtras");
if (user.role === "adopter" && user.lifestyle) {
extras.innerHTML = `<p><b>Activity Level:</b> ${user.lifestyle.activityLevel || "N/A"}</p>`;
} else {
extras.innerHTML = "";
}
}
function populateEditForm(user) {
document.getElementById("editName").value = user.name || "";
document.getElementById("editEmail").value = user.email;
document.getElementById("editLocation").value = user.location || "";
document.getElementById("editRole").value = user.role || "adopter";
const extras = document.getElementById("editExtras");
if (user.role === "adopter") {
extras.innerHTML = `
<select id="editActivity" class="w-full p-3 border rounded-lg">
<option value="">Select Activity Level</option>
<option value="high">High</option>
<option value="medium">Medium</option>
<option value="low">Low</option>
</select>
`;
if (user.lifestyle?.activityLevel) {
document.getElementById("editActivity").value = user.lifestyle.activityLevel;
}
} else {
extras.innerHTML = "";
}
}
// Toggle edit mode
document.getElementById("editBtn").addEventListener("click", () => {
populateEditForm(currentUser);
document.getElementById("profileView").classList.add("hidden");
document.getElementById("profileEdit").classList.remove("hidden");
});
document.getElementById("cancelBtn").addEventListener("click", () => {
document.getElementById("profileEdit").classList.add("hidden");
document.getElementById("profileView").classList.remove("hidden");
});
// Save edits
document.getElementById("profileEdit").addEventListener("submit", async e => {
e.preventDefault();
const name = document.getElementById("editName").value;
const location = document.getElementById("editLocation").value;
const role = document.getElementById("editRole").value;
let lifestyle = {};
if (role === "adopter") {
lifestyle.activityLevel = document.getElementById("editActivity").value;
}
try {
const res = await fetchWithAuth(`${USER_API_URL}/profile`, {
method: "PATCH",
body: JSON.stringify({ name, location, role, lifestyle })
});
const updated = await res.json();
if (res.ok) {
currentUser = updated;
renderProfile(updated);
document.getElementById("profileEdit").classList.add("hidden");
document.getElementById("profileView").classList.remove("hidden");
} else {
alert(updated.msg || "Profile update failed");
}
} catch (err) {
console.error(err);
alert("Error updating profile");
}
});
document.addEventListener("DOMContentLoaded", loadProfile);
</script>
</body>
</html>