-
-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathnotifications.js
More file actions
129 lines (108 loc) · 4.29 KB
/
notifications.js
File metadata and controls
129 lines (108 loc) · 4.29 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
$(() => {
/**
* @param {QPixelNotification} notification
*/
const makeNotification = (notification) => {
const template = `<div class="js-notification widget h-m-0 h-m-b-2 ${notification.is_read ? 'read' : 'is-teal'}">
<div class="widget--body h-p-2">
<div class="h-c-tertiary-600 h-fs-caption">
${notification.community_name} ·
<span class="js-notif-state">${notification.is_read ? 'read' : `<strong>unread</strong>`}</span> ·
<span data-livestamp="${notification.created_at}">${notification.created_at}</span>
</div>
<p><a href="${notification.link}" data-id="${notification.id}"
class="h-fw-bold is-not-underlined ${notification.is_read ? 'read' : ''} notification-link">${notification.content}</a></p>
<p class="has-font-size-caption"><a href="javascript:void(0)" data-notif-id="${notification.id}" class="js-notification-toggle">
<i class="fas fa-${notification.is_read ? 'envelope' : 'envelope-open'}"></i>
mark ${notification.is_read ? 'unread' : 'read'}
</a></p>
</div>
</div>`;
return template;
};
/**
* @param {number} change
*/
const changeInboxCount = (change) => {
const counter = $('.inbox-count');
let count;
if (counter.length !== 0) {
count = parseInt(counter.text(), 10);
}
else {
count = 0;
}
count = Math.max(0, count + change);
if (count === 0 && counter.length !== 0) {
counter.remove();
}
else {
if (counter.length === 0) {
$(`<span class="header--alert inbox-count">${count}</span>`).prependTo($('.inbox-toggle'));
}
else {
counter.text(count);
}
}
};
$('.inbox-toggle').on('click', async (ev) => {
ev.preventDefault();
const $inbox = $('.inbox');
if($inbox.hasClass("is-active")) {
const resp = await QPixel.getJSON(`/users/me/notifications`);
const data = await resp.json();
const $inboxContainer = $inbox.find(".inbox--container");
$inboxContainer.html('');
const unread = data.filter((n) => !n.is_read);
const read = data.filter((n) => n.is_read);
unread.forEach((notification) => {
const item = $(makeNotification(notification));
$inboxContainer.append(item);
});
$inboxContainer.append(`<div role="separator" class="header-slide--separator"></div>`);
read.forEach((notification) => {
const item = $(makeNotification(notification));
$inboxContainer.append(item);
});
}
});
$('.js-read-all-notifs').on('click', async (ev) => {
ev.preventDefault();
await QPixel.fetchJSON('/notifications/read_all', {}, {
headers: { 'Accept': 'application/json' }
});
$('.inbox-count').remove();
$('.js-notification').removeClass('is-teal').addClass('read');
$('.js-notif-state').text('read');
$('.js-notification-toggle').html(`<i class="fas fa-envelope"></i> mark unread`);
});
$(document).on('click', '.inbox a:not(.no-unread):not(.read):not(.js-notification-toggle)', async (evt) => {
const $tgt = $(evt.target);
const id = $tgt.data('id');
const resp = await QPixel.fetchJSON(`/notifications/${id}/read`, {}, {
headers: { 'Accept': 'application/json' }
});
const data = await resp.json();
$tgt.parents('.js-notification')[0].outerHTML = makeNotification(data.notification);
changeInboxCount(-1);
});
$(document).on('click', '.js-notification-toggle', async (ev) => {
ev.stopPropagation();
const $tgt = $(ev.target).is('a') ? $(ev.target) : $(ev.target).parents('a');
const id = $tgt.attr('data-notif-id');
const resp = await QPixel.fetchJSON(`/notifications/${id}/read`, {}, {
headers: { 'Accept': 'application/json' }
});
const data = await resp.json();
if (data.status !== 'success') {
console.error('Failed to toggle notification read state. Wat?');
return;
}
$tgt.parents('.js-notification')[0].outerHTML = makeNotification(data.notification);
const change = data.notification.is_read ? -1 : +1;
changeInboxCount(change);
});
$(document).on('click', '.notification-link', async (ev) => {
$(ev.target).parents('.inbox').removeClass('is-active');
});
});