49 lines
918 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
2019-04-19 13:50:52 +00:00
constructor(icon: HTMLElement, counter: HTMLElement) {
this.icon = icon
this.counter = counter
2018-03-29 10:14:46 +00:00
}
2018-02-28 15:26:49 +00:00
async update() {
2019-11-17 09:25:14 +00:00
const response = await fetch("/api/count/notifications/unseen", {
2018-02-28 15:26:49 +00:00
credentials: "same-origin"
})
2019-11-17 09:25:14 +00:00
const body = await response.text()
this.setCounter(parseInt(body))
}
setCounter(unseen: number) {
this.unseen = unseen
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
}
}