This commit is contained in:
Eduard Urbach 2018-03-29 12:14:46 +02:00
parent 91dce1895f
commit ee1460aef7
4 changed files with 46 additions and 34 deletions

View File

@ -89,19 +89,19 @@ component SettingsNotifications(user *arn.User)
.widget.mountable .widget.mountable
h3.widget-title h3.widget-title
Icon("bell") Icon("bell")
span Push notifications span Instant notifications
#enable-notifications.widget-section #enable-notifications.widget-section
label Enable: label Instant notifications:
button.action(data-action="enableNotifications", data-trigger="click") button.action(data-action="enableNotifications", data-trigger="click")
Icon("toggle-off") Icon("toggle-off")
span Enable push notifications span OFF
#disable-notifications.widget-section.hidden #disable-notifications.widget-section.hidden
label Disable: label Instant notifications:
button.action(data-action="disableNotifications", data-trigger="click") button.action(data-action="disableNotifications", data-trigger="click")
Icon("toggle-on") Icon("toggle-on")
span Disable push notifications span ON
#test-notification.widget-section #test-notification.widget-section
label Test: label Test:

View File

@ -1,7 +1,8 @@
import { AnimeNotifier } from "../AnimeNotifier" import { AnimeNotifier } from "../AnimeNotifier"
import { Diff } from "../Diff"
// Load more // Load more
export function loadMore(arn: AnimeNotifier, button: HTMLButtonElement) { export async function loadMore(arn: AnimeNotifier, button: HTMLButtonElement) {
// Prevent firing this event multiple times // Prevent firing this event multiple times
if(arn.isLoading || button.disabled || button.classList.contains("hidden")) { if(arn.isLoading || button.disabled || button.classList.contains("hidden")) {
return return
@ -13,10 +14,15 @@ export function loadMore(arn: AnimeNotifier, button: HTMLButtonElement) {
let target = arn.app.find("load-more-target") let target = arn.app.find("load-more-target")
let index = button.dataset.index let index = button.dataset.index
fetch("/_" + arn.app.currentPath + "/from/" + index, { try {
credentials: "same-origin" let response = await fetch("/_" + arn.app.currentPath + "/from/" + index, {
}) credentials: "same-origin"
.then(response => { })
if(!response.ok) {
throw response.statusText
}
let newIndex = response.headers.get("X-LoadMore-Index") let newIndex = response.headers.get("X-LoadMore-Index")
// End of data? // End of data?
@ -27,26 +33,24 @@ export function loadMore(arn: AnimeNotifier, button: HTMLButtonElement) {
button.dataset.index = newIndex button.dataset.index = newIndex
} }
return response let body = await response.text()
})
.then(response => response.text())
.then(body => {
let tmp = document.createElement(target.tagName) let tmp = document.createElement(target.tagName)
tmp.innerHTML = body tmp.innerHTML = body
let children = [...tmp.childNodes] let children = [...tmp.childNodes]
window.requestAnimationFrame(() => { Diff.mutations.queue(() => {
for(let child of children) { for(let child of children) {
target.appendChild(child) target.appendChild(child)
} }
arn.app.emit("DOMContentLoaded") arn.app.emit("DOMContentLoaded")
}) })
}) } catch(err) {
.catch(err => arn.statusMessage.showError(err)) arn.statusMessage.showError(err)
.then(() => { } finally {
arn.loading(false) arn.loading(false)
button.disabled = false button.disabled = false
}) }
} }

View File

@ -2,18 +2,18 @@ import { AnimeNotifier } from "../AnimeNotifier"
// Enable notifications // Enable notifications
export async function enableNotifications(arn: AnimeNotifier, button: HTMLElement) { export async function enableNotifications(arn: AnimeNotifier, button: HTMLElement) {
arn.statusMessage.showInfo("Enabling push notifications...") arn.statusMessage.showInfo("Enabling instant notifications...")
await arn.pushManager.subscribe(arn.user.dataset.id) await arn.pushManager.subscribe(arn.user.dataset.id)
arn.updatePushUI() arn.updatePushUI()
arn.statusMessage.showInfo("Enabled push notifications for this device.") arn.statusMessage.showInfo("Enabled instant notifications for this device.")
} }
// Disable notifications // Disable notifications
export async function disableNotifications(arn: AnimeNotifier, button: HTMLElement) { export async function disableNotifications(arn: AnimeNotifier, button: HTMLElement) {
arn.statusMessage.showInfo("Disabling push notifications...") arn.statusMessage.showInfo("Disabling instant notifications...")
await arn.pushManager.unsubscribe(arn.user.dataset.id) await arn.pushManager.unsubscribe(arn.user.dataset.id)
arn.updatePushUI() arn.updatePushUI()
arn.statusMessage.showInfo("Disabled push notifications for this device.") arn.statusMessage.showInfo("Disabled instant notifications for this device.")
} }
// Test notification // Test notification

View File

@ -1,5 +1,14 @@
import { Diff } from "./Diff"
export class NotificationManager { export class NotificationManager {
unseen: number unseen: number
icon: HTMLElement
counter: HTMLElement
constructor() {
this.icon = document.getElementById("notification-icon")
this.counter = document.getElementById("notification-count")
}
async update() { async update() {
let response = await fetch("/api/count/notifications/unseen", { let response = await fetch("/api/count/notifications/unseen", {
@ -17,17 +26,16 @@ export class NotificationManager {
} }
render() { render() {
let notificationIcon = document.getElementById("notification-icon") Diff.mutations.queue(() => {
let notificationCount = document.getElementById("notification-count") this.counter.innerText = this.unseen.toString()
notificationCount.innerText = this.unseen.toString() if(this.unseen === 0) {
this.counter.classList.add("hidden")
if(this.unseen === 0) { this.icon.classList.remove("hidden")
notificationCount.classList.add("hidden") } else {
notificationIcon.classList.remove("hidden") this.icon.classList.add("hidden")
} else { this.counter.classList.remove("hidden")
notificationIcon.classList.add("hidden") }
notificationCount.classList.remove("hidden") })
}
} }
} }