Files
7am/web/summary.js

129 lines
4.5 KiB
JavaScript
Raw Normal View History

2025-05-10 13:04:39 +01:00
const KEY_SUBSCRIPTION = "subscription"
2025-05-10 00:36:38 +01:00
2025-05-10 14:58:41 +01:00
const canReceiveUpdates = "serviceWorker" in navigator
2025-05-10 00:36:38 +01:00
const getSummaryButton = document.getElementById("get-summary-btn")
const loc = getSummaryButton.dataset.loc
2025-05-10 13:04:39 +01:00
async function main() {
2025-05-10 14:58:41 +01:00
getSummaryButton.style.display = "none"
2025-05-10 00:36:38 +01:00
window.addEventListener("load", () => {
navigator.serviceWorker.register("/sw.js")
})
2025-05-10 13:04:39 +01:00
const reg = await navigator.serviceWorker.ready
const existingSubscriptionJson = localStorage.getItem(KEY_SUBSCRIPTION)
const existingSubscription = existingSubscriptionJson ? JSON.parse(existingSubscriptionJson) : null
if (existingSubscription?.locations?.includes(loc) ?? false) {
getSummaryButton.innerText = "Stop updates"
reg.active.postMessage(loc)
} else {
getSummaryButton.innerText = "Get daily updates at 7am"
}
getSummaryButton.addEventListener("click", onButtonClick)
getSummaryButton.style.display = "block"
}
async function onButtonClick() {
const reg = await navigator.serviceWorker.ready
2025-05-10 14:58:41 +01:00
const pushSub = await reg.pushManager.getSubscription()
2025-05-10 13:04:39 +01:00
const existingSubscriptionJson = localStorage.getItem(KEY_SUBSCRIPTION)
2025-05-10 14:58:41 +01:00
const registeredSubscription = existingSubscriptionJson ? JSON.parse(existingSubscriptionJson) : null
const currentlyEnabled = (registeredSubscription?.locations?.includes(loc) ?? false) && pushSub !== null
2025-05-10 00:36:38 +01:00
2025-05-10 13:04:39 +01:00
if (currentlyEnabled) {
2025-05-10 14:58:41 +01:00
registeredSubscription.locations.splice(
registeredSubscription.locations.indexOf(loc),
1
)
if (registeredSubscription.locations.length === 0) {
await reg.pushManager.getSubscription().then((sub) => sub?.unsubscribe())
await fetch(`/registrations/${registeredSubscription.id}`, { method: "DELETE" })
localStorage.removeItem(KEY_SUBSCRIPTION)
} else {
const newReg = await fetch(`/registrations/${registeredSubscription.id}`, {
method: "PATCH",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
subscription: pushSub,
2025-05-10 18:27:29 +01:00
removeLocations: [loc],
2025-05-10 14:58:41 +01:00
})
}).then(jsonOrThrow)
2025-05-10 18:27:29 +01:00
localStorage.setItem(KEY_SUBSCRIPTION, JSON.stringify(newReg))
2025-05-10 14:58:41 +01:00
}
2025-05-10 13:04:39 +01:00
getSummaryButton.innerText = "Get daily updates at 7am"
} else {
2025-05-10 17:57:29 +01:00
getSummaryButton.innerText = "Subscribing"
getSummaryButton.disabled = true
2025-05-10 13:04:39 +01:00
const worker = await navigator.serviceWorker.ready
try {
const publicKey = await fetch("/vapid").then((res) => {
if (res.status === 200) {
return res.text()
}
throw new Error(`${res.status}`)
})
const pushSub = await worker.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: publicKey
})
let newSubscription
2025-05-10 14:58:41 +01:00
if (registeredSubscription) {
newSubscription = await fetch(`/registrations/${registeredSubscription.id}`, {
2025-05-10 13:04:39 +01:00
method: "PATCH",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
subscription: pushSub,
2025-05-10 17:57:29 +01:00
locations: [loc],
2025-05-10 13:04:39 +01:00
})
2025-05-10 14:58:41 +01:00
}).then(jsonOrThrow)
2025-05-10 00:36:38 +01:00
} else {
2025-05-10 13:04:39 +01:00
newSubscription = await fetch("/registrations", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
subscription: pushSub,
locations: [loc]
})
2025-05-10 14:58:41 +01:00
}).then(jsonOrThrow)
2025-05-10 00:36:38 +01:00
}
2025-05-10 13:04:39 +01:00
localStorage.setItem(KEY_SUBSCRIPTION, JSON.stringify(newSubscription))
getSummaryButton.innerText = "Stop updates"
} catch (error) {
2025-05-10 15:22:07 +01:00
console.error(error)
2025-05-10 14:58:41 +01:00
alert(`Error when trying to subscribe to updates: ${error}`)
2025-05-10 17:57:29 +01:00
getSummaryButton.innerText = "Get daily updates at 7am"
} finally {
getSummaryButton.disabled = false
2025-05-10 13:04:39 +01:00
}
}
2025-05-10 14:58:41 +01:00
}
2025-05-10 13:04:39 +01:00
2025-05-10 14:58:41 +01:00
function jsonOrThrow(res) {
if (res.status === 200) {
return res.json()
}
throw new Error(`server returned status ${res.status}`)
2025-05-10 13:04:39 +01:00
}
if (canReceiveUpdates) {
main()
2025-05-10 22:45:10 +01:00
} else {
getSummaryButton.style.display = "none"
2025-05-10 00:36:38 +01:00
}