From e84441e7f14ff32afa401289e85a576b323d6fd2 Mon Sep 17 00:00:00 2001 From: Karl Date: Fri, 18 Jul 2025 17:36:25 +0100 Subject: [PATCH] add and hide button --- templates/base.html | 27 +++++++++++++++++++++++---- templates/home.html | 3 +++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/templates/base.html b/templates/base.html index 18fdf51..ee7ef96 100644 --- a/templates/base.html +++ b/templates/base.html @@ -59,10 +59,25 @@ const isLoggedIn = document.getElementById('is-logged-in').value === 'true'; navigator.serviceWorker.register('{{ url_for("static", filename="service-worker.js") }}').then(function(registration) { console.log('ServiceWorker registration successful with scope: ', registration.scope); - // Check for the 'loggedin' query parameter to trigger the prompt - const urlParams = new URLSearchParams(window.location.search); - if (isLoggedIn && urlParams.has('loggedin')) { - askPermission(registration); + const enableNotificationsBtn = document.getElementById('enable-notifications-btn'); + + function setupNotificationButton() { + registration.pushManager.getSubscription().then(function(subscription) { + if (enableNotificationsBtn) { + if (subscription) { + enableNotificationsBtn.style.display = 'none'; + } else { + enableNotificationsBtn.style.display = 'block'; + enableNotificationsBtn.addEventListener('click', function() { + askPermission(registration); + }); + } + } + }); + } + + if (enableNotificationsBtn) { + setupNotificationButton(); } }, function(err) { console.log('ServiceWorker registration failed: ', err); @@ -143,6 +158,10 @@ .then(response => { if (response.ok) { console.log('Subscription saved successfully.'); + const enableNotificationsBtn = document.getElementById('enable-notifications-btn'); + if (enableNotificationsBtn) { + enableNotificationsBtn.style.display = 'none'; + } return response.json(); } else { console.error('Failed to save subscription. Status:', response.status); diff --git a/templates/home.html b/templates/home.html index 8755fbc..03eee7c 100644 --- a/templates/home.html +++ b/templates/home.html @@ -5,6 +5,9 @@ {% block content %}

Welcome {{ username }}!


+ +
+

You have {{ accounts }} active accounts