Spaces:
Sleeping
Sleeping
File size: 16,868 Bytes
7c726af d3d89a9 b1505ef 7c726af d3d89a9 66d5688 d3d89a9 7c726af d3d89a9 66d5688 d3d89a9 66d5688 7c726af d3d89a9 7c726af d3d89a9 7c726af d3d89a9 66d5688 d3d89a9 66d5688 d3d89a9 66d5688 d3d89a9 66d5688 5c51021 66d5688 6267b9c 66d5688 5c51021 66d5688 7c726af 66d5688 7c726af 66d5688 7cc79ff 66d5688 6267b9c 66d5688 6267b9c 7c726af 66d5688 7c726af 6267b9c 7c726af 6267b9c d3d89a9 66d5688 6267b9c 33d90ab 66d5688 6267b9c d3d89a9 66d5688 d3d89a9 b1505ef af06eb9 b1505ef 82b462b b1505ef af06eb9 b1505ef e0dbea4 b1505ef 66d5688 b1505ef 6b5e222 82b462b b1505ef d3d89a9 6267b9c d3d89a9 6267b9c d3d89a9 6267b9c 66d5688 6267b9c d3d89a9 66d5688 6267b9c 66d5688 6267b9c 66d5688 6267b9c 66d5688 6267b9c d3d89a9 66d5688 6267b9c 66d5688 b1505ef 6267b9c 66d5688 6267b9c b1505ef 66d5688 6267b9c d3d89a9 6267b9c 66d5688 6267b9c 66d5688 c1bf20e 7c726af b1505ef |
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 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>Profile - CTRL + ALT + HEAL</title>
<script src="https://cdn.tailwindcss.com"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body class="bg-[#F7F8F9] min-h-screen">
<!-- NAVBAR -->
<nav
class="fixed top-0 left-0 w-full z-50 backdrop-blur-md bg-white/20 border-b border-white/30 shadow-md"
>
<div class="flex justify-between items-center w-full px-6 py-4">
<a
href="index.html"
class="text-2xl font-bold text-black hover:text-[var(--tropical-indigo)] transition"
>
CTRL + ALT + HEAL
</a>
<ul class="hidden md:flex space-x-6 font-medium text-gray-800">
<li>
<a href="index.html" class="nav-link" style="display: none">Home</a>
</li>
<li><a href="profile.html" class="nav-link">Profile</a></li>
<li><a href="analyzer.html" class="nav-link">Analyzer</a></li>
<li><a href="past_data.html" class="nav-link">Past Reports</a></li>
<li id="authNavItem">
<a href="login.html" class="nav-link">Login</a>
</li>
</ul>
<button
id="hamburger"
class="md:hidden text-[var(--latte-cream)] text-2xl"
>
☰
</button>
</div>
<ul
id="mobile-menu"
class="hidden flex-col space-y-4 bg-white/30 backdrop-blur-lg border border-white/20 rounded-xl shadow-lg mt-2 p-4 mx-6 md:hidden"
>
<li>
<a
href="index.html"
class="block text-gray-800 hover:text-[var(--tropical-indigo)]"
>Home</a
>
</li>
<li>
<a
href="analyzer.html"
class="block text-gray-800 hover:text-[var(--tropical-indigo)]"
>Analyzer</a
>
</li>
<li>
<a
href="profile.html"
class="block text-gray-800 hover:text-[var(--tropical-indigo)]"
>Profile</a
>
</li>
<li>
<a
href="login.html"
class="block text-gray-800 hover:text-[var(--tropical-indigo)]"
>Login</a
>
</li>
<li>
<a
href="about.html"
class="block text-gray-800 hover:text-[var(--tropical-indigo)]"
>About</a
>
</li>
</ul>
</nav>
<script>
const hamburger = document.getElementById("hamburger");
const mobileMenu = document.getElementById("mobile-menu");
hamburger.addEventListener("click", () =>
mobileMenu.classList.toggle("hidden")
);
const currentPath = window.location.pathname.split("/").pop();
document.querySelectorAll(".nav-link").forEach((link) => {
if (link.getAttribute("href") === currentPath)
link.classList.add("active-page");
});
document.querySelectorAll("#mobile-menu a").forEach((link) => {
if (link.getAttribute("href") === currentPath)
link.classList.add("active-page");
});
</script>
<div class="container mx-auto px-6 pt-24">
<div class="bg-white border border-gray-200 rounded-lg p-6 shadow mb-6">
<!-- PROFILE VIEW -->
<div id="profileViewSection">
<div class="flex items-start space-x-6 mb-10">
<div class="flex flex-col items-center">
<img src="https://cdn.mos.cms.futurecdn.net/ARWms77gyVZ5u2MZkQqcKc-1256-80.jpg.webp"
alt="Profile Tree"
class="w-32 h-32 object-cover rounded-full border border-gray-300">
<button id="editProfileBtn" class="mt-2 px-4 py-2 rounded">
Edit Profile
</button>
<a href="analyzer.html">
<button id="analyzerBtn" class="mt-2 px-4 py-2 rounded">
Go To Analyzer
</button>
</a>
</div>
<div>
<h2 class="text-xl font-semibold mb-4">Your Profile</h2>
<p class="mb-2">
Name: <span id="userName" class="font-medium">Loading...</span>
</p>
<p class="mb-2">
DOB: <span id="userDOB" class="font-medium">Loading...</span>
</p>
</div>
</div>
</div>
<!-- PROFILE EDIT -->
<div id="profileEditSection" class="hidden mb-10">
<div class="flex flex-col sm:flex-row items-start space-x-6">
<div class="flex flex-col items-center mb-4 sm:mb-0">
<img src="https://cdn.mos.cms.futurecdn.net/ARWms77gyVZ5u2MZkQqcKc-1256-80.jpg.webp"
alt="Profile Tree"
class="w-32 h-32 object-cover rounded-full border border-gray-300">
</div>
<div class="flex-1">
<h1 class="text-3xl font-semibold mb-4">Edit Profile</h1>
<div class="space-y-4">
<div>
<label for="inputName" class="block text-sm font-medium"
>Name</label
>
<input
type="text"
id="inputName"
class="w-full border rounded px-3 py-2 focus:ring-2 focus:ring-[#6B9080] focus:outline-none"
/>
</div>
<div>
<label for="inputDOB" class="block text-sm font-medium"
>Date of Birth</label
>
<input
type="date"
id="inputDOB"
class="w-full border rounded px-3 py-2 focus:ring-2 focus:ring-[#6B9080] focus:outline-none"
/>
</div>
</div>
<div class="flex space-x-2 mt-4">
<button id="saveProfileBtn" class="px-4 py-2 rounded">
Save
</button>
<button id="cancelProfileBtn" class="px-4 py-2 rounded">
Cancel
</button>
</div>
</div>
</div>
</div>
<!-- RANGE BARS -->
<h2 class="text-xl font-semibold mb-4">Your Current Health Summary</h2>
<div class="tabs">
<div class="tab active" data-tab="out-range">
Out of Range <span class="count-badge">0</span>
</div>
<div class="tab" data-tab="in-range">
In Range <span class="count-badge">0</span>
</div>
</div>
<div class="tab-content active" id="out-range"></div>
<div class="tab-content" id="in-range"></div>
</div>
</div>
<script type="module">
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.22.0/firebase-app.js";
import {
getAuth,
onAuthStateChanged,
signOut,
} from "https://www.gstatic.com/firebasejs/9.22.0/firebase-auth.js";
import {
getFirestore,
doc,
getDoc,
updateDoc,
} from "https://www.gstatic.com/firebasejs/9.22.0/firebase-firestore.js";
const firebaseConfig = {
apiKey: "AIzaSyAPhM_Ee7cLzyKHs5zyFy8g5ZOk9-pubRI",
authDomain: "login-tutorial-7a9e1.firebaseapp.com",
projectId: "login-tutorial-7a9e1",
storageBucket: "login-tutorial-7a9e1.firebasestorage.app",
messagingSenderId: "491093197824",
appId: "1:491093197824:web:9f86659034af7e6a8244e5",
measurementId: "G-JM7T9N6ZLZ"
};
const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
const db = getFirestore(app);
let currentUser = null;
const homeNavDesktop = document.querySelector(
'ul.md\\:flex li a[href="index.html"]'
)?.parentElement;
const homeNavMobile = document.querySelector(
'#mobile-menu a[href="index.html"]'
)?.parentElement;
// Fetch measurements from backend API
async function getUserMeasurements(userId) {
try {
const url = api("user_measurements/", { user_id: userId });
const res = await fetch(url);
if (!res.ok) throw new Error("Failed to fetch measurements");
const data = await res.json();
return (data.measurements || []).map((m) => ({
name: m.measurement_type,
value: Number(m.value),
min: Number(m.min),
max: Number(m.max),
tab: m.status === "HIGH" || m.status === "LOW" ? "out-range" : "in-range",
impacts: m.impacts || [],
}));
} catch (e) {
console.error("Error fetching user measurements:", e);
return [];
}
}
function getLatestByType(measurements) {
const latest = {};
measurements.forEach(m => {
if (!latest[m.name] || new Date(m.timestamp) > new Date(latest[m.name].timestamp)) {
latest[m.name] = m;
}
});
return Object.values(latest);
}
// Render bars in the UI based on measurement array
function renderBars(measurements) {
console.log("measurements",measurements);
const counts = { "in-range": 0, "out-range": 0 };
const tabs = document.querySelectorAll(".tab");
const contents = document.querySelectorAll(".tab-content");
// Clear existing content
contents.forEach((c) => (c.innerHTML = ""));
measurements.forEach((m) => {
counts[m.tab]++;
const container = document.getElementById(m.tab);
const card = document.createElement("div");
card.className = "range-card";
const title = document.createElement("h3");
title.className = "measurement-title";
title.textContent = m.name;
card.appendChild(title);
if (m.impacts?.length) {
const impactLabel = document.createElement("div");
impactLabel.className = "impact-label";
impactLabel.textContent = "Impacts: " + m.impacts.join(", ");
card.appendChild(impactLabel);
}
const barContainer = document.createElement("div");
barContainer.className = "range-bar-container";
const bar = document.createElement("div");
bar.className = "range-bar";
let normalPercent = 100;
let overflowPercent = 0;
if (m.tab === "out-range" && m.value > m.max) {
normalPercent = ((m.max - m.min) / (m.value - m.min)) * 100;
overflowPercent = 100 - normalPercent;
}
const normalDiv = document.createElement("div");
normalDiv.className = "normal-range";
normalDiv.style.width = normalPercent + "%";
bar.appendChild(normalDiv);
if (overflowPercent > 0) {
const overflowDiv = document.createElement("div");
overflowDiv.className = "overflow-range";
overflowDiv.style.left = normalPercent + "%";
overflowDiv.style.width = overflowPercent + "%";
bar.appendChild(overflowDiv);
}
let valuePercent = ((m.value - m.min) / (m.max - m.min)) * 100;
valuePercent = Math.min(Math.max(valuePercent, 0), 100);
const marker = document.createElement("div");
marker.className = "marker";
marker.style.left = valuePercent + "%";
bar.appendChild(marker);
const valueLabel = document.createElement("div");
valueLabel.className = "value-label";
valueLabel.style.left = valuePercent + "%";
valueLabel.textContent = m.value;
bar.appendChild(valueLabel);
barContainer.appendChild(bar);
const minMaxDiv = document.createElement("div");
minMaxDiv.className = "min-max-labels relative w-full";
const minLabel = document.createElement("span");
minLabel.textContent = "Min: " + m.min;
minLabel.style.position = "absolute";
minLabel.style.left = "0";
const maxLabel = document.createElement("span");
maxLabel.textContent = "Max: " + m.max;
maxLabel.style.position = "absolute";
let maxPercent = 100;
if (m.value > m.max) maxPercent = ((m.max - m.min) / (m.value - m.min)) * 100;
maxLabel.style.left = maxPercent + "%";
maxLabel.style.transform = "translateX(-100%)";
minMaxDiv.appendChild(minLabel);
minMaxDiv.appendChild(maxLabel);
barContainer.appendChild(minMaxDiv);
card.appendChild(barContainer);
container.appendChild(card);
});
// Update tab badges
tabs.forEach((tab) => {
const t = tab.dataset.tab;
const badge = tab.querySelector(".count-badge");
badge.textContent = counts[t];
tab.addEventListener("click", () => {
tabs.forEach((t) => t.classList.remove("active"));
tab.classList.add("active");
contents.forEach((c) =>
c.id === t ? c.classList.add("active") : c.classList.remove("active")
);
});
});
}
// ✅ Auth State Handling
onAuthStateChanged(auth, async (user) => {
const authNavItem = document.getElementById("authNavItem");
if (user) {
currentUser = user;
await loadUserProfile();
// ✅ fetch and render bars after user is ready
console.log("currentuser data",currentUser);
// const measurements = await getUserMeasurements(currentUser.uid);
let measurements = await getUserMeasurements(currentUser.email);
measurements = getLatestByType(measurements);
renderBars(measurements);
authNavItem.innerHTML = '<button onclick="logout()" style="color:red">Logout</button>';
if (homeNavDesktop) homeNavDesktop.style.display = "none";
if (homeNavMobile) homeNavMobile.style.display = "none";
} else {
authNavItem.innerHTML = '<a href="login.html">Login</a>';
if (homeNavDesktop) homeNavDesktop.style.display = "";
if (homeNavMobile) homeNavMobile.style.display = "";
window.location.href = "login.html";
}
});
async function loadUserProfile() {
try {
const userDoc = await getDoc(doc(db, "users", currentUser.uid));
if (userDoc.exists()) {
const userData = userDoc.data();
document.getElementById("userName").textContent =
userData.name || "Name not set";
document.getElementById("userDOB").textContent =
userData.dob || "DOB not set";
localStorage.setItem("userName", userData.name || "");
localStorage.setItem("userDOB", userData.dob || "");
}
} catch (e) {
console.error("Error loading user profile:", e);
}
}
window.saveProfile = async (name, dob) => {
try {
await updateDoc(doc(db, "users", currentUser.uid), { name, dob });
localStorage.setItem("userName", name);
localStorage.setItem("userDOB", dob);
return true;
} catch (e) {
console.error("Error updating profile:", e);
return false;
}
};
window.logout = async () => {
try {
await signOut(auth);
localStorage.clear();
window.location.href = "login.html";
} catch (e) {
console.error("Error signing out:", e);
}
};
document.addEventListener("DOMContentLoaded", async () => {
const profileView = document.getElementById("profileViewSection");
const profileEdit = document.getElementById("profileEditSection");
const editBtn = document.getElementById("editProfileBtn");
const saveBtn = document.getElementById("saveProfileBtn");
const cancelBtn = document.getElementById("cancelProfileBtn");
const inputName = document.getElementById("inputName");
const inputDOB = document.getElementById("inputDOB");
editBtn.addEventListener("click", () => {
inputName.value = document.getElementById("userName").textContent;
inputDOB.value = document.getElementById("userDOB").textContent;
profileView.classList.add("hidden");
profileEdit.classList.remove("hidden");
});
saveBtn.addEventListener("click", async () => {
const nameVal = inputName.value.trim();
const dobVal = inputDOB.value;
const success = await window.saveProfile(nameVal, dobVal);
if (success) {
document.getElementById("userName").textContent =
nameVal || "Name not set";
document.getElementById("userDOB").textContent =
dobVal || "DOB not set";
profileEdit.classList.add("hidden");
profileView.classList.remove("hidden");
} else {
alert("Error saving profile. Please try again.");
}
});
cancelBtn.addEventListener("click", () => {
profileEdit.classList.add("hidden");
profileView.classList.remove("hidden");
});
// RANGE BARS LOGIC
const tabs = document.querySelectorAll(".tab");
const contents = document.querySelectorAll(".tab-content");
});
</script>
</body>
</html> |