-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotifications.js
More file actions
82 lines (65 loc) · 2.6 KB
/
notifications.js
File metadata and controls
82 lines (65 loc) · 2.6 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', function() {
// Keeps track of whether or not a request for updating notifications has been sent to the server
let is_checking_for_notifications = false;
// Notification counter
const notification_count = document.getElementById("unread_notification_count");
// Mark notification as read
window.mark_as_read = async function(notification_id) {
try {
const url = '/api/mark_notification_as_read';
const data = {'id': notification_id};
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Requested-With': 'XMLHttpRequest'
},
body: JSON.stringify(data)
});
if (!response.ok) {
throw new Error('Error processing data!');
}
const responseData = await response.json();
if (!responseData.result) {
throw new Error('Error retrieving data!');
}
// Decrease unread notification count
notification_count.innerHTML = parseInt(notification_count.innerHTML) - 1;
// Remove highlight
document.getElementById(notification_id).classList.remove('notification-unread');
} catch(error) {
console.log(error);
}
};
// Mark all notifications as read
window.mark_all_as_read = async function() {
try {
const url = '/api/mark_notification_as_read';
const data = {'optional': 'all'};
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Requested-With': 'XMLHttpRequest'
},
body: JSON.stringify(data)
});
if (!response.ok) {
throw new Error('Error processing data!');
}
const responseData = await response.json();
if (!responseData.result) {
throw new Error('Error retrieving data!');
}
// Set notification count to 0
notification_count.innerHTML = 0;
// Remove highlights
const notifications = document.querySelectorAll('.notification-unread');
for (const notification of notifications) {
notification.classList.remove('notification-unread');
}
} catch(error) {
console.log(error);
}
};
});