diff options
Diffstat (limited to 'ui/routes')
| -rw-r--r-- | ui/routes/+layout.svelte | 26 |
1 files changed, 15 insertions, 11 deletions
diff --git a/ui/routes/+layout.svelte b/ui/routes/+layout.svelte index a1f8d84..9089c7e 100644 --- a/ui/routes/+layout.svelte +++ b/ui/routes/+layout.svelte @@ -11,15 +11,10 @@ function doSubscribe() { navigator.serviceWorker.ready .then(async(registration) => { - console.debug("Beginning subscription"); const response = await fetch("/api/vapid"); // and if we fail to get it? const vapidPublicKey = await response.text(); - // Chrome doesn't accept the base64-encoded (string) vapidPublicKey yet - // urlBase64ToUint8Array() is defined in /tools.js - // const convertedVapidKey = urlBase64ToUint8Array(vapidPublicKey); const convertedVapidKey = vapidPublicKey; - // Subscribe the user return registration.pushManager.subscribe({ userVisibleOnly: true, applicationServerKey: convertedVapidKey @@ -41,7 +36,7 @@ }).then((subscription) => { return subscription.unsubscribe() .then(function() { - console.log("Unsubscribed", subscription.endpoint); + subscriptionJson = null; return fetch("/api/unregister", { method: "post", headers: { "Content-type": "application/json" }, @@ -51,6 +46,14 @@ }); } + function toggleSub() { + if (subscriptionJson !== null) { + doUnsubscribe(); + } else { + doSubscribe(); + } + } + function notifyMe() { fetch("/api/echo", { method: "post", @@ -67,9 +70,7 @@ return registration.pushManager.getSubscription(); }).then((subscription) => { if (subscription) { - console.debug("Already subscribed", subscription.endpoint); - } else { - doSubscribe(); + subscriptionJson = JSON.parse(JSON.stringify(subscription)); } }); }); @@ -103,9 +104,12 @@ <button onclick="{notifyMe}"> notify </button> - <button onclick="{doUnsubscribe}"> - unsub + <button onclick="{toggleSub}"> + toggle sub </button> + <div> + hasSub: {subscriptionJson !== null} + </div> <div class="trail"> {#if session} <div> |
