45 lines
893 B
TypeScript
Raw Normal View History

2018-04-02 05:34:16 +00:00
import Diff from "./Diff"
2018-03-29 10:14:46 +00:00
2018-04-02 05:34:16 +00:00
export default class NotificationManager {
2018-02-28 15:26:49 +00:00
unseen: number
2018-03-29 10:14:46 +00:00
icon: HTMLElement
counter: HTMLElement
constructor() {
this.icon = document.getElementById("notification-icon")
this.counter = document.getElementById("notification-count")
}
2018-02-28 15:26:49 +00:00
async update() {
let response = await fetch("/api/count/notifications/unseen", {
credentials: "same-origin"
})
let body = await response.text()
this.unseen = parseInt(body)
2018-02-28 23:06:03 +00:00
2018-10-28 03:52:20 +00:00
if(isNaN(this.unseen)) {
this.unseen = 0
}
2018-02-28 23:06:03 +00:00
if(this.unseen > 99) {
this.unseen = 99
}
2018-02-28 15:26:49 +00:00
this.render()
}
render() {
2018-03-29 10:14:46 +00:00
Diff.mutations.queue(() => {
2018-06-28 06:30:24 +00:00
this.counter.textContent = this.unseen.toString()
2018-02-28 15:26:49 +00:00
2018-03-29 10:14:46 +00:00
if(this.unseen === 0) {
this.counter.classList.add("hidden")
this.icon.classList.remove("hidden")
} else {
this.icon.classList.add("hidden")
this.counter.classList.remove("hidden")
}
})
2018-02-28 15:26:49 +00:00
}
}