Skip to content

Commit 9cd133a

Browse files
committed
added icons on nav bar
1 parent 6a9a73d commit 9cd133a

File tree

3 files changed

+64
-31
lines changed

3 files changed

+64
-31
lines changed

controlers/page.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
const { findUserById, getAllUsers } = require("../db/user");
22
const { getFriendshipByUsersId } = require("../db/friendship");
33
const { getPostByUserId } = require("../db/post");
4+
const { getUnSeenNotificationbyUserId } = require("../db/notification");
45
const pageController = {
56
home: async (req, res) => {
67
if (req.isAuthenticated()) {
78
const user = await findUserById(req.user._id);
9+
810
let posts = [];
11+
12+
const notifications = await getUnSeenNotificationbyUserId(user._id);
913

1014
posts = [...user.posts];
11-
12-
for(let i=0; i<user.friends.length; i++){
13-
posts = [...posts ,...await getPostByUserId(user.friends[i])]
14-
}
1515

16-
console.log(posts);
16+
for (let i = 0; i < user.friends.length; i++) {
17+
posts = [...posts, ...(await getPostByUserId(user.friends[i]))];
18+
}
1719

18-
return res.render("home", { user: user, posts });
20+
return res.render("home", { user: user, posts, notifications });
1921
} else {
2022
return res.redirect("/login");
2123
}
@@ -30,6 +32,8 @@ const pageController = {
3032
const id = req.params.userid;
3133
const user = await findUserById(req.user._id);
3234
const profileUser = await findUserById(id);
35+
36+
const notifications = await getUnSeenNotificationbyUserId(user._id);
3337

3438
let requestFriendship = await getFriendshipByUsersId(user._id, id);
3539
let requestedFriendship = await getFriendshipByUsersId(id, user._id);
@@ -39,11 +43,13 @@ const pageController = {
3943
user,
4044
requestFriendship,
4145
requestedFriendship,
46+
notifications
4247
});
4348
},
4449
all: async (req, res) => {
4550
const users = await getAllUsers(req.user._id);
46-
res.render("all", { user: req.user, allUsers: users });
51+
const notifications = await getUnSeenNotificationbyUserId(req.user._id);
52+
res.render("all", { user: req.user, allUsers: users, notifications });
4753
},
4854
};
4955

db/notification.js

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const User = require("../models/user");
22
const Notification = require("../models/notification");
33

4-
54
const createNotification = async (author, receiverId, postId, type) => {
65
switch (type) {
76
case "friend-request":
@@ -18,19 +17,26 @@ const createNotification = async (author, receiverId, postId, type) => {
1817
receiver: receiverId,
1918
type,
2019
}).save();
21-
20+
2221
if (!notification) {
2322
return null;
2423
}
25-
24+
2625
const { createFriendship } = require("./friendship");
27-
const friendship = await createFriendship(author, receiverId, "pending", notification._id);
28-
29-
if(!friendship){
26+
const friendship = await createFriendship(
27+
author,
28+
receiverId,
29+
"pending",
30+
notification._id
31+
);
32+
33+
if (!friendship) {
3034
return null;
3135
}
3236

33-
await Notification.findByIdAndUpdate(notification._id, {friendship: friendship._id });
37+
await Notification.findByIdAndUpdate(notification._id, {
38+
friendship: friendship._id,
39+
});
3440

3541
await User.findOneAndUpdate(
3642
{ _id: receiverId },
@@ -57,7 +63,21 @@ const deleteNotification = async (id) => {
5763
{ _id: notification.receiver },
5864
{ $pull: { notifications: notification._id } }
5965
);
60-
6166
};
6267

63-
module.exports = { createNotification, deleteNotification };
68+
const getUnSeenNotificationbyUserId = async (id) => {
69+
const notifications = await Notification.find({
70+
receiver: id,
71+
seen: false,
72+
}).populate({ path: "author", select: ["name", "profileUrl"] });
73+
if (!notifications) {
74+
return [];
75+
}
76+
return notifications;
77+
};
78+
79+
module.exports = {
80+
createNotification,
81+
deleteNotification,
82+
getUnSeenNotificationbyUserId,
83+
};

views/partials/nav.ejs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
2020
<% if (user) { %>
2121
<li class="nav-item">
22-
<a class="nav-link active" aria-current="page" href="/">Home</a>
22+
<a class="nav-link active" aria-current="page" href="/"><i class="fas fa-home"></i> Home</a>
2323
</li>
2424
<li class="nav-item">
2525
<a class="nav-link" href="/profile/<%= user._id %>"
@@ -40,26 +40,33 @@
4040
role="button"
4141
data-bs-toggle="dropdown"
4242
aria-expanded="false"
43-
>
44-
Νotifications <!-- <span class="badge bg-danger">4</span>-->
43+
><i class="fas fa-bell"></i>
44+
Νotifications <% if (notifications.length !== 0) { %>
45+
<span class="badge bg-danger"><%= notifications.length %></span>
46+
<% } %>
4547
</a>
4648
<ul class="dropdown-menu p-1" aria-labelledby="navbarDropdown">
47-
<% user.notifications.forEach(notification => { %>
48-
<% if (notification && !notification.seen) { %>
49+
<% if (notifications.length === 0) { %>
50+
<li class="dropdown-item disabled">You have 0 notifications</li>
51+
<% } else {%>
52+
<% notifications.forEach(notification => { %>
4953
<% if (notification.author && notification.type === "friend-request") { %>
50-
<div class="d-flex justify-content-start align-items-center">
51-
<li><a class="dropdown-item" href="/profile/<%= notification.author._id %>"><%= notification.author.name %> wants to be your friend</a></li>
52-
<button type="button" onclick="acceptFriendRequest('<%= notification.friendship %>')" class = "btn btn-outline-primary btn-sm m-1 "> Accept</button>
53-
</div>
54-
<% } %>
55-
<% } %>
56-
<% }) %>
57-
</ul>
54+
<div class="d-flex justify-content-start align-items-center">
55+
<a href="/profile/<%= notification.author._id %>">
56+
<img style="width: 100%; height: auto; max-width: 30px; border-radius: 50%;" class="img-thumbnail img-fluid" src="<%= notification.author.profileUrl %>" alt="user profile">
57+
</a>
58+
<li><a class="dropdown-item" href="/profile/<%= notification.author._id %>"><%= notification.author.name %> wants to be your friend</a></li>
59+
<button type="button" onclick="acceptFriendRequest('<%= notification.friendship %>')" class = "btn btn-outline-primary btn-sm m-1 "> Accept</button>
60+
</div>
61+
<% } %>
62+
<% }) %>
63+
<% } %>
64+
</ul>
5865
</li>
59-
<li class="nav-item"><a class="nav-link" href="/all">Members</a></li>
66+
<li class="nav-item"><a class="nav-link" href="/all"><i class="fas fa-user-friends"></i> Members</a></li>
6067
<li class="nav-item">
6168
<a class="nav-link active" aria-current="page" href="/auth/logout"
62-
>Log out</a
69+
><i class="fas fa-sign-out-alt"></i> Log out</a
6370
>
6471
</li>
6572
<% } %>

0 commit comments

Comments
 (0)