-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHomePage_dashBoard.js
More file actions
70 lines (59 loc) · 2.75 KB
/
HomePage_dashBoard.js
File metadata and controls
70 lines (59 loc) · 2.75 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
document.addEventListener("DOMContentLoaded", () => {
// Get the user data from localStorage
const userData = localStorage.getItem("user");
if (!userData) {
console.log("No user data found, redirecting to login");
window.location.href = "SignIn.html";
return;
}
try {
const user = JSON.parse(userData);
console.log("Dashboard JS is working, user data:", user);
// Get the username display element
const usernameDisplay = document.getElementById("username-display");
if (usernameDisplay) {
// Display the actual username if available, otherwise fallback to email
const displayName = user.username || user.email.split('@')[0];
usernameDisplay.textContent = `Welcome, ${displayName}!`;
usernameDisplay.style.color = '#fff'; // Make it white to match navbar
usernameDisplay.style.marginLeft = '10px'; // Add some spacing
} else {
console.error("username-display element not found");
}
// Set username in both places
document.getElementById("username-display").textContent = user.username;
document.getElementById("sidebar-username").textContent = user.username;
// Profile and Sidebar functionality
const profileContainer = document.getElementById("profileContainer");
const sidebar = document.getElementById("sidebar");
const overlay = document.getElementById("overlay");
const logoutBtn = document.getElementById("logoutBtn");
// Toggle sidebar when clicking profile
profileContainer.addEventListener("click", () => {
sidebar.classList.add("active");
overlay.classList.add("active");
});
// Close sidebar when clicking overlay
overlay.addEventListener("click", () => {
sidebar.classList.remove("active");
overlay.classList.remove("active");
});
// Handle logout
logoutBtn.addEventListener("click", (e) => {
e.preventDefault();
localStorage.removeItem("user");
window.location.href = "HomePage.html";
});
// Close sidebar when clicking outside
document.addEventListener("click", (e) => {
if (!sidebar.contains(e.target) && !profileContainer.contains(e.target)) {
sidebar.classList.remove("active");
overlay.classList.remove("active");
}
});
} catch (error) {
console.error("Error parsing user data:", error);
localStorage.removeItem("user"); // Clear invalid data
window.location.href = "SignIn.html";
}
});