50 lines
919 B
TypeScript
Raw Normal View History

2018-04-02 07:34:16 +02:00
import Diff from "./Diff"
2018-03-29 12:14:46 +02:00
2018-04-02 07:34:16 +02:00
export default class NotificationManager {
2018-02-28 16:26:49 +01:00
unseen: number
2018-03-29 12:14:46 +02:00
icon: HTMLElement
counter: HTMLElement
2019-04-19 22:50:52 +09:00
constructor(icon: HTMLElement, counter: HTMLElement) {
this.icon = icon
this.counter = counter
2018-03-29 12:14:46 +02:00
}
2018-02-28 16:26:49 +01:00
async update() {
2019-11-17 18:25:14 +09:00
const response = await fetch("/api/count/notifications/unseen", {
2018-02-28 16:26:49 +01:00
credentials: "same-origin"
})
2019-11-17 18:25:14 +09:00
const body = await response.text()
this.setCounter(parseInt(body))
}
setCounter(unseen: number) {
this.unseen = unseen
2018-03-01 00:06:03 +01:00
2018-10-28 12:52:20 +09:00
if(isNaN(this.unseen)) {
this.unseen = 0
}
2018-03-01 00:06:03 +01:00
if(this.unseen > 99) {
this.unseen = 99
}
2018-02-28 16:26:49 +01:00
this.render()
}
render() {
2018-03-29 12:14:46 +02:00
Diff.mutations.queue(() => {
2018-06-28 15:30:24 +09:00
this.counter.textContent = this.unseen.toString()
2018-02-28 16:26:49 +01:00
2018-03-29 12:14:46 +02: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 16:26:49 +01:00
}
2019-11-17 18:44:30 +09:00
}