This repository has been archived by the owner on Nov 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
notification-ui.js
51 lines (46 loc) · 1.8 KB
/
notification-ui.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class Notification extends HTMLElement {
constructor() {
super();
let data = window.ftd.component_data(this);
let marginLeft = data.margin_left.get();
let marginTop = data.margin_top.get();
let remove = data.remove.get();
const shadow = this.attachShadow({ mode: 'open' });
const div = document.createElement('div');
div.style.backgroundColor = 'red';
div.style.padding = '10px';
div.style.borderRadius = '50%';
div.style.padding = '5px';
div.style.marginLeft = marginLeft;
div.style.marginTop = marginTop;
let [checkNotification, lastPublished] = showNotificationAndGetLastPublish(data.notifications);
if (checkNotification) {
shadow.appendChild(div);
}
data.remove.on_change(() => {
remove = data.remove.get();
if (remove) {
div.remove();
setLastPublished(lastPublished)
}
})
}
}
customElements.define('notification-ui', Notification);
function showNotificationAndGetLastPublish(notification) {
let notificationList = notification.get()
if (notificationList.length > 0) {
let lastPublished = notificationList[0].item.getAllFields().published_on.get();
let lastNotificationReadTime = localStorage.getItem("fastn_com_last_notification_read_time");
if (!lastNotificationReadTime || (!!lastNotificationReadTime && new Date(lastNotificationReadTime) < new Date(lastPublished))) {
return [true, lastPublished];
}
return [false, lastPublished]
}
return [false, undefined];
}
function setLastPublished(lastPublished) {
if (!!lastPublished) {
localStorage.setItem("fastn_com_last_notification_read_time", lastPublished);
}
}