forked from Aditya-269/BloggingSite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdashboard.js
More file actions
82 lines (73 loc) · 3.23 KB
/
dashboard.js
File metadata and controls
82 lines (73 loc) · 3.23 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
document.addEventListener('DOMContentLoaded', () => {
try {
// Fetch data from localStorage
const blogs = JSON.parse(localStorage.getItem('blogs')) || [];
const currentUser = JSON.parse(localStorage.getItem('currentUser')) || null;
// Set current date
const currentDate = new Date().toLocaleDateString('en-US', {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric',
});
document.getElementById('current-date').textContent = currentDate;
// Display current user's username
const usernameElement = document.getElementById('header-username');
const logoutLink = document.getElementById('logout-link');
const signupLink = document.getElementById('signup-link');
const loginLink = document.getElementById('login-link');
if (currentUser && currentUser.username) {
usernameElement.textContent = currentUser.username;
logoutLink.style.display = 'block'; // Show logout
signupLink.style.display = 'none'; // Hide sign up
if (loginLink) loginLink.style.display = 'none'; // Hide login if present
} else {
usernameElement.textContent = 'Guest';
logoutLink.style.display = 'none'; // Hide logout
signupLink.style.display = 'block'; // Show sign up
if (loginLink) loginLink.style.display = 'block'; // Show login if present
}
// Log out functionality
if (logoutLink) {
logoutLink.addEventListener('click', () => {
localStorage.removeItem('currentUser'); // Clear current user data
window.location.href = 'login.html'; // Redirect to login page
});
}
// Display user name
document.getElementById('user-name').textContent = currentUser ? currentUser.username : 'Guest';
// Update total posts
const totalPostsElement = document.getElementById('total-posts');
const totalPostsCard = document.getElementById('total-posts-card');
totalPostsElement.textContent = `${blogs.length} Posts`;
totalPostsCard.textContent = blogs.length;
// Calculate and display total likes
const totalLikes = blogs.reduce((sum, blog) => {
if (blog.reactions) {
return (
sum +
Object.values(blog.reactions).filter(reaction => reaction === 'like').length
);
}
return sum;
}, 0);
document.getElementById('likes').textContent = `${totalLikes} Likes`;
// Calculate and display total comments
const totalComments = blogs.reduce((sum, blog) => sum + (blog.comments?.length || 0), 0);
document.getElementById('comments').textContent = `${totalComments} Comments`;
// Display the three most recent blogs
const blogsList = document.getElementById('blogs-list');
blogsList.innerHTML = ''; // Clear existing content
blogs.slice(-3).reverse().forEach(blog => {
const listItem = document.createElement('li');
listItem.innerHTML = `
<a href="#">
<strong>${blog.title}</strong>
<span style="font-size: 0.9rem; color: #555;">(${blog.comments?.length || 0} Comments)</span>
</a>`;
blogsList.appendChild(listItem);
});
} catch (error) {
console.error('Error initializing dashboard:', error);
}
});