From eaf95ece7dfb4d4d591b2f501183b2706653742e Mon Sep 17 00:00:00 2001 From: Kit La Touche Date: Sun, 3 Aug 2025 23:12:09 -0400 Subject: Add roughed-in UI for setting up notifications --- ui/lib/components/NotificationSettings.svelte | 84 +++++++++++++++++++++++-- ui/routes/+layout.svelte | 88 +-------------------------- ui/styles/forms.css | 3 + 3 files changed, 84 insertions(+), 91 deletions(-) diff --git a/ui/lib/components/NotificationSettings.svelte b/ui/lib/components/NotificationSettings.svelte index c690b21..81c3708 100644 --- a/ui/lib/components/NotificationSettings.svelte +++ b/ui/lib/components/NotificationSettings.svelte @@ -1,13 +1,89 @@ +

Enable notifications

+ +

+ {#if subscriptionJson === null} + Notifications are currently disabled. + {:else} + Notifications are currently enabled. + {/if} +

+ +

Notify me when:

- import { setContext, onMount } from "svelte"; + import { setContext } from "svelte"; import { onNavigate } from '$app/navigation'; import { page } from '$app/state'; import '../app.css'; import logo from '$lib/assets/logo.png'; - /* ==================== Start subscription library-esque ==================== */ - let subscriptionJson = $state(null); - - function doSubscribe() { - navigator.serviceWorker.ready - .then(async (registration) => { - const response = await fetch("/api/vapid"); - // and if we fail to get it? - const vapidPublicKey = await response.text(); - const convertedVapidKey = vapidPublicKey; - return registration.pushManager.subscribe({ - userVisibleOnly: true, - applicationServerKey: convertedVapidKey - }); - }).then((subscription) => { - const subJson = subscription.toJSON(); - subscriptionJson = { - endpoint: subJson.endpoint, - p256dh: subJson.keys.p256dh, - auth: subJson.keys.auth, - }; - return fetch("/api/push", { - method: "post", - headers: { "Content-type": "application/json" }, - body: JSON.stringify(subscriptionJson), - }); - }); - } - - async function doUnsubscribe() { - navigator.serviceWorker.ready - .then((registration) => { - return registration.pushManager.getSubscription(); - }).then((subscription) => { - const { endpoint } = subscription.toJSON(); - return subscription.unsubscribe() - .then(() => { - fetch("/api/push", { - method: "delete", - headers: { "Content-type": "application/json" }, - body: JSON.stringify({ endpoint }), - }); - subscriptionJson = null; - }); - }); - } - - function toggleSub() { - if (subscriptionJson !== null) { - doUnsubscribe(); - } else { - doSubscribe(); - } - } - - function notifyMe() { - fetch("/api/echo", { - method: "post", - headers: { "Content-type": "application/json" }, - body: JSON.stringify({ - endpoint: subscriptionJson.endpoint, - msg: JSON.stringify({"msg": "oople doople"}), - }) - }); - } - - onMount(() => { - navigator.serviceWorker.ready.then((registration) => { - return registration.pushManager.getSubscription(); - }).then((subscription) => { - if (subscription) { - subscriptionJson = JSON.parse(JSON.stringify(subscription)); - } - }); - }); - /* ==================== END ==================== */ - const session = $derived(page.data.session); let pageContext = $state({ showMenu: false, @@ -107,15 +30,6 @@ pilcrow - - -
- hasSub: {subscriptionJson !== null} -
{#if session}
diff --git a/ui/styles/forms.css b/ui/styles/forms.css index f4d218f..1cc44a3 100644 --- a/ui/styles/forms.css +++ b/ui/styles/forms.css @@ -13,6 +13,7 @@ label input { border: 1px solid var(--colour-input-border); } +.btn, form.form > button { font-family: 'Overlock', cursive; background-color: var(--colour-input-bg); @@ -23,6 +24,8 @@ form.form > button { margin: 0.25rem; } +button[disabled], .disabled { + opacity: 0.6; color: var(--light-text); } -- cgit v1.2.3