-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlive_notifications.js
27 lines (25 loc) · 1.2 KB
/
live_notifications.js
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
import formatMysqlDatetime from "./dateFormat.js";
// update notifications bill in the top bar, called by script.js
export default function updateNotifications() {
fetch('api/newNotifications.php')
.then(response => response.json())
.then(data => {
let notificationList = document.querySelector('#topBarNotifications');
notificationList.innerHTML = ''; // Clear the current list
data.forEach(notification => {
let listItem = document.createElement('a');
listItem.href = `${notification.object_type}?id=${notification.object_id}`;
listItem.classList.add('dropdown-item', 'p-0', 'notify-item', 'card', 'unread-noti', 'text-wrap', 'shadow-none', 'mb-2', 'p-2');
listItem.innerHTML = `${notification.text} <br><span class="small text-uppercase text-muted">${formatMysqlDatetime(notification.time)}</span>`;
notificationList.appendChild(listItem);
});
// Show the badge if the list of notifications isn't empty
let badge = document.querySelector('#notifications-badge');
if (data.length > 0) {
badge.style.display = 'block';
} else {
badge.style.display = 'none';
}
})
.catch(error => console.error('Error:', error));
}