Wallos: Open-source, self-hostable personal subscription tracker. Visualize your recurring expenses, manage your budget, and save money.
fix(push): stop reloading the page to subscribe or remove a device
Miguel Ribeirob308da007b05
diff --git a/endpoints/notifications/savepushsubscription.php b/endpoints/notifications/savepushsubscription.phpindex 916e88e..3fd12a9 100644--- a/endpoints/notifications/savepushsubscription.php+++ b/endpoints/notifications/savepushsubscription.php@@ -75,9 +75,10 @@ if ($existing) {// The same device subscribing again - a browser can rotate keys for an// existing endpoint, so the row is refreshed rather than left stale or// duplicated under the same endpoint.+ $subscriptionId = (int) $existing['id'];$query = "UPDATE push_subscriptions SET p256dh = :p256dh, auth = :auth, user_agent = :userAgent WHERE id = :id";$stmt = $db->prepare($query);- $stmt->bindValue(':id', $existing['id'], SQLITE3_INTEGER);+ $stmt->bindValue(':id', $subscriptionId, SQLITE3_INTEGER);} else {$query = "INSERT INTO push_subscriptions (user_id, endpoint, p256dh, auth, user_agent, created_at)VALUES (:userId, :endpoint, :p256dh, :auth, :userAgent, :createdAt)";@@ -92,9 +93,16 @@ $stmt->bindValue(':auth', $auth, SQLITE3_TEXT);$stmt->bindValue(':userAgent', $userAgent, SQLITE3_TEXT);if ($stmt->execute()) {+ // Handed back so the settings page can add or refresh this device's row+ // itself, rather than reloading the whole page to pick it up.echo json_encode(["success" => true,- "message" => translate('notifications_settings_saved', $i18n)+ "message" => translate('notifications_settings_saved', $i18n),+ "subscription" => [+ "id" => $existing ? $subscriptionId : (int) $db->lastInsertRowID(),+ "user_agent" => $userAgent !== '' ? $userAgent : translate('unknown_device', $i18n),+ "is_new" => !$existing,+ ],]);} else {echo json_encode([diff --git a/scripts/i18n/ar.js b/scripts/i18n/ar.jsindex 37b9d71..343af5f 100644--- a/scripts/i18n/ar.js+++ b/scripts/i18n/ar.js@@ -52,4 +52,6 @@ let i18n = {no_results_found: "لم يُعثر على نتائج",push_not_supported: "الإشعارات الفورية غير مدعومة في هذا المتصفح.",push_permission_denied: "لم يتم منح إذن الإشعارات.",+ delete: "حذف",+ no_devices_registered: "لم يتم تسجيل أي أجهزة بعد.",}diff --git a/scripts/i18n/az.js b/scripts/i18n/az.jsindex 6c4ce83..74f7444 100644--- a/scripts/i18n/az.js+++ b/scripts/i18n/az.js@@ -52,4 +52,6 @@ let i18n = {invalid_budget_anchor_date: "Baza tarixi etibarlı tarix olmalıdır",push_not_supported: "Bu brauzerdə push bildirişləri dəstəklənmir.",push_permission_denied: "Bildiriş icazəsi verilmədi.",+ delete: "Sil",+ no_devices_registered: "Hələ heç bir cihaz qeydiyyatdan keçməyib.",}diff --git a/scripts/i18n/ca.js b/scripts/i18n/ca.jsindex f5901a7..7ec54b4 100644--- a/scripts/i18n/ca.js+++ b/scripts/i18n/ca.js@@ -52,4 +52,6 @@ let i18n = {no_results_found: "No s'han trobat resultats",push_not_supported: "Les notificacions push no són compatibles amb aquest navegador.",push_permission_denied: "No s'ha concedit el permís de notificacions.",+ delete: "Eliminar",+ no_devices_registered: "Encara no hi ha dispositius registrats.",}diff --git a/scripts/i18n/cs.js b/scripts/i18n/cs.jsindex c0f2143..8d73ea7 100644--- a/scripts/i18n/cs.js+++ b/scripts/i18n/cs.js@@ -52,4 +52,6 @@ let i18n = {no_results_found: "Nebyly nalezeny žádné výsledky",push_not_supported: "Push notifikace nejsou v tomto prohlížeči podporovány.",push_permission_denied: "Oprávnění k oznámením nebylo uděleno.",+ delete: "Odstranit",+ no_devices_registered: "Zatím nejsou registrována žádná zařízení.",}diff --git a/scripts/i18n/da.js b/scripts/i18n/da.jsindex f4e3316..bb5bb17 100644--- a/scripts/i18n/da.js+++ b/scripts/i18n/da.js@@ -52,4 +52,6 @@ let i18n = {no_results_found: "Ingen resultater fundet",push_not_supported: "Push-notifikationer understøttes ikke i denne browser.",push_permission_denied: "Tilladelse til notifikationer blev ikke givet.",+ delete: "Slet",+ no_devices_registered: "Ingen enheder registreret endnu.",}diff --git a/scripts/i18n/de.js b/scripts/i18n/de.jsindex f04a9a7..fb695d4 100644--- a/scripts/i18n/de.js+++ b/scripts/i18n/de.js@@ -54,4 +54,6 @@ let i18n = {no_results_found: "Keine Ergebnisse gefunden",push_not_supported: "Push-Benachrichtigungen werden von diesem Browser nicht unterstützt.",push_permission_denied: "Die Berechtigung für Benachrichtigungen wurde nicht erteilt.",+ delete: "Löschen",+ no_devices_registered: "Noch keine Geräte registriert.",}diff --git a/scripts/i18n/el.js b/scripts/i18n/el.jsindex 73cf8e7..cdc28de 100644--- a/scripts/i18n/el.js+++ b/scripts/i18n/el.js@@ -52,4 +52,6 @@ let i18n = {no_results_found: "Δεν βρέθηκαν αποτελέσματα",push_not_supported: "Οι ειδοποιήσεις push δεν υποστηρίζονται σε αυτό το πρόγραμμα περιήγησης.",push_permission_denied: "Δεν χορηγήθηκε άδεια ειδοποιήσεων.",+ delete: "Διαγραφή",+ no_devices_registered: "Δεν έχουν καταχωρηθεί συσκευές ακόμα.",}diff --git a/scripts/i18n/en.js b/scripts/i18n/en.jsindex 61a4bba..7e9f0bf 100644--- a/scripts/i18n/en.js+++ b/scripts/i18n/en.js@@ -51,5 +51,7 @@ let i18n = {invalid_budget_period: "Invalid budget period selected",invalid_budget_anchor_date: "Anchor date must be a valid date",push_not_supported: "Push notifications are not supported in this browser.",- push_permission_denied: "Notification permission was not granted.",+ push_permission_denied: "Notification permission was not granted.",+ delete: "Delete",+ no_devices_registered: "No devices registered yet.",}diff --git a/scripts/i18n/es.js b/scripts/i18n/es.jsindex 4864461..1188612 100644--- a/scripts/i18n/es.js+++ b/scripts/i18n/es.js@@ -52,4 +52,6 @@ let i18n = {no_results_found: "No se encontraron resultados",push_not_supported: "Las notificaciones push no son compatibles con este navegador.",push_permission_denied: "No se concedió el permiso de notificaciones.",+ delete: "Eliminar",+ no_devices_registered: "Aún no hay dispositivos registrados.",}diff --git a/scripts/i18n/fr.js b/scripts/i18n/fr.jsindex 02f9da9..9e54d21 100644--- a/scripts/i18n/fr.js+++ b/scripts/i18n/fr.js@@ -52,4 +52,6 @@ let i18n = {no_results_found: "Aucun résultat trouvé",push_not_supported: "Les notifications push ne sont pas prises en charge par ce navigateur.",push_permission_denied: "L'autorisation de notification n'a pas été accordée.",+ delete: "Supprimer",+ no_devices_registered: "Aucun appareil enregistré pour le moment.",};diff --git a/scripts/i18n/hu.js b/scripts/i18n/hu.jsindex 444cad0..272daf1 100644--- a/scripts/i18n/hu.js+++ b/scripts/i18n/hu.js@@ -52,4 +52,6 @@ let i18n = {no_results_found: "Nincs találat",push_not_supported: "A böngésző nem támogatja a push értesítéseket.",push_permission_denied: "Az értesítési engedély nem lett megadva.",+ delete: "Törlés",+ no_devices_registered: "Még nincs regisztrált eszköz.",}diff --git a/scripts/i18n/id.js b/scripts/i18n/id.jsindex c22d0b3..60be31a 100644--- a/scripts/i18n/id.js+++ b/scripts/i18n/id.js@@ -52,4 +52,6 @@ let i18n = {no_results_found: "Tidak ada hasil ditemukan",push_not_supported: "Notifikasi push tidak didukung di browser ini.",push_permission_denied: "Izin notifikasi tidak diberikan.",+ delete: "Hapus",+ no_devices_registered: "Belum ada perangkat yang terdaftar.",}diff --git a/scripts/i18n/it.js b/scripts/i18n/it.jsindex 8b7dc7d..bf40d3f 100644--- a/scripts/i18n/it.js+++ b/scripts/i18n/it.js@@ -53,4 +53,6 @@ let i18n = {no_results_found: "Nessun risultato trovato",push_not_supported: "Le notifiche push non sono supportate in questo browser.",push_permission_denied: "L'autorizzazione per le notifiche non è stata concessa.",+ delete: "Cancella",+ no_devices_registered: "Nessun dispositivo ancora registrato.",}diff --git a/scripts/i18n/ja.js b/scripts/i18n/ja.jsindex ec2d917..040d1c2 100644--- a/scripts/i18n/ja.js+++ b/scripts/i18n/ja.js@@ -52,4 +52,6 @@ let i18n = {no_results_found: "結果が見つかりません",push_not_supported: "このブラウザではプッシュ通知に対応していません。",push_permission_denied: "通知の許可が得られませんでした。",+ delete: "削除",+ no_devices_registered: "まだ登録されたデバイスはありません。",}diff --git a/scripts/i18n/ko.js b/scripts/i18n/ko.jsindex ae2e23f..7035a93 100644--- a/scripts/i18n/ko.js+++ b/scripts/i18n/ko.js@@ -52,4 +52,6 @@ let i18n = {invalid_budget_anchor_date: "기준일은 유효한 날짜여야 합니다.",push_not_supported: "이 브라우저에서는 푸시 알림이 지원되지 않습니다.",push_permission_denied: "알림 권한이 허용되지 않았습니다.",+ delete: "삭제",+ no_devices_registered: "아직 등록된 기기가 없습니다.",}diff --git a/scripts/i18n/nl.js b/scripts/i18n/nl.jsindex 008c6fd..8640644 100644--- a/scripts/i18n/nl.js+++ b/scripts/i18n/nl.js@@ -52,4 +52,6 @@ let i18n = {no_results_found: "Geen resultaten gevonden",push_not_supported: "Pushmeldingen worden niet ondersteund in deze browser.",push_permission_denied: "Toestemming voor meldingen is niet verleend.",+ delete: "Verwijderen",+ no_devices_registered: "Nog geen apparaten geregistreerd.",}diff --git a/scripts/i18n/pl.js b/scripts/i18n/pl.jsindex ac0cda7..bd6b100 100644--- a/scripts/i18n/pl.js+++ b/scripts/i18n/pl.js@@ -52,4 +52,6 @@ let i18n = {no_results_found: "Nie znaleziono wyników",push_not_supported: "Powiadomienia push nie są obsługiwane w tej przeglądarce.",push_permission_denied: "Nie udzielono zgody na powiadomienia.",+ delete: "Usuń",+ no_devices_registered: "Nie zarejestrowano jeszcze żadnych urządzeń.",}diff --git a/scripts/i18n/pt.js b/scripts/i18n/pt.jsindex 7f1ab2f..7b993fa 100644--- a/scripts/i18n/pt.js+++ b/scripts/i18n/pt.js@@ -52,4 +52,6 @@ let i18n = {no_results_found: "Nenhum resultado encontrado",push_not_supported: "As notificações push não são suportadas neste navegador.",push_permission_denied: "A permissão de notificações não foi concedida.",+ delete: "Remover",+ no_devices_registered: "Ainda não há dispositivos registados.",};diff --git a/scripts/i18n/pt_br.js b/scripts/i18n/pt_br.jsindex c885b2e..4501335 100644--- a/scripts/i18n/pt_br.js+++ b/scripts/i18n/pt_br.js@@ -52,4 +52,6 @@ let i18n = {no_results_found: "Nenhum resultado encontrado",push_not_supported: "As notificações push não são compatíveis com este navegador.",push_permission_denied: "A permissão de notificações não foi concedida.",+ delete: "Excluir",+ no_devices_registered: "Nenhum dispositivo registrado ainda.",}diff --git a/scripts/i18n/ro.js b/scripts/i18n/ro.jsindex 2722e82..3903d30 100644--- a/scripts/i18n/ro.js+++ b/scripts/i18n/ro.js@@ -52,4 +52,6 @@ let i18n = {no_results_found: "Nu s-au găsit rezultate",push_not_supported: "Notificările push nu sunt acceptate în acest browser.",push_permission_denied: "Permisiunea pentru notificări nu a fost acordată.",+ delete: "Șterge",+ no_devices_registered: "Niciun dispozitiv înregistrat încă.",}diff --git a/scripts/i18n/ru.js b/scripts/i18n/ru.jsindex 7bb4a44..d106e1f 100644--- a/scripts/i18n/ru.js+++ b/scripts/i18n/ru.js@@ -52,4 +52,6 @@ let i18n = {no_results_found: "Ничего не найдено",push_not_supported: "Push-уведомления не поддерживаются в этом браузере.",push_permission_denied: "Разрешение на уведомления не было предоставлено.",+ delete: "Удалить",+ no_devices_registered: "Пока нет зарегистрированных устройств.",}diff --git a/scripts/i18n/sl.js b/scripts/i18n/sl.jsindex 77136f8..262a7c4 100644--- a/scripts/i18n/sl.js+++ b/scripts/i18n/sl.js@@ -52,4 +52,6 @@ let i18n = {no_results_found: "Ni najdenih rezultatov",push_not_supported: "Potisna obvestila v tem brskalniku niso podprta.",push_permission_denied: "Dovoljenje za obvestila ni bilo odobreno.",+ delete: "Izbriši",+ no_devices_registered: "Še ni registriranih naprav.",}diff --git a/scripts/i18n/sr.js b/scripts/i18n/sr.jsindex 0c2807b..cf50a08 100644--- a/scripts/i18n/sr.js+++ b/scripts/i18n/sr.js@@ -52,4 +52,6 @@ let i18n = {no_results_found: "Нема резултата",push_not_supported: "Push обавештења нису подржана у овом прегледачу.",push_permission_denied: "Дозвола за обавештења није одобрена.",+ delete: "Обриши",+ no_devices_registered: "Још увек нема регистрованих уређаја.",}diff --git a/scripts/i18n/sr_lat.js b/scripts/i18n/sr_lat.jsindex fce30fe..071b6d2 100644--- a/scripts/i18n/sr_lat.js+++ b/scripts/i18n/sr_lat.js@@ -52,4 +52,6 @@ let i18n = {no_results_found: "Nema rezultata",push_not_supported: "Push obaveštenja nisu podržana u ovom pregledaču.",push_permission_denied: "Dozvola za obaveštenja nije odobrena.",+ delete: "Izbriši",+ no_devices_registered: "Još uvek nema registrovanih uređaja.",}diff --git a/scripts/i18n/tr.js b/scripts/i18n/tr.jsindex c58e74a..277c5f1 100644--- a/scripts/i18n/tr.js+++ b/scripts/i18n/tr.js@@ -52,6 +52,8 @@ let i18n = {no_results_found: "Sonuç bulunamadı",push_not_supported: "Bu tarayıcıda push bildirimleri desteklenmiyor.",push_permission_denied: "Bildirim izni verilmedi.",+ delete: "Sil",+ no_devices_registered: "Henüz kayıtlı cihaz yok.",}diff --git a/scripts/i18n/uk.js b/scripts/i18n/uk.jsindex 09a2b2d..6a3a94e 100644--- a/scripts/i18n/uk.js+++ b/scripts/i18n/uk.js@@ -52,4 +52,6 @@ let i18n = {no_results_found: "Нічого не знайдено",push_not_supported: "Push-сповіщення не підтримуються в цьому браузері.",push_permission_denied: "Дозвіл на сповіщення не надано.",+ delete: "Видалити",+ no_devices_registered: "Поки що немає зареєстрованих пристроїв.",}diff --git a/scripts/i18n/vi.js b/scripts/i18n/vi.jsindex 94e35ce..e3cdac8 100644--- a/scripts/i18n/vi.js+++ b/scripts/i18n/vi.js@@ -52,4 +52,6 @@ let i18n = {no_results_found: "Không tìm thấy kết quả",push_not_supported: "Trình duyệt này không hỗ trợ thông báo đẩy.",push_permission_denied: "Quyền thông báo chưa được cấp.",+ delete: "Xóa",+ no_devices_registered: "Chưa có thiết bị nào được đăng ký.",}diff --git a/scripts/i18n/zh_cn.js b/scripts/i18n/zh_cn.jsindex aac445b..6cecd12 100644--- a/scripts/i18n/zh_cn.js+++ b/scripts/i18n/zh_cn.js@@ -52,4 +52,6 @@ let i18n = {no_results_found: "未找到结果",push_not_supported: "此浏览器不支持推送通知。",push_permission_denied: "未授予通知权限。",+ delete: "删除",+ no_devices_registered: "尚未注册任何设备。",};diff --git a/scripts/i18n/zh_tw.js b/scripts/i18n/zh_tw.jsindex 1cd203f..ba19ac0 100644--- a/scripts/i18n/zh_tw.js+++ b/scripts/i18n/zh_tw.js@@ -52,4 +52,6 @@ let i18n = {no_results_found: "找不到結果",push_not_supported: "此瀏覽器不支援推播通知。",push_permission_denied: "未授予通知權限。",+ delete: "刪除",+ no_devices_registered: "尚未註冊任何裝置。",};diff --git a/scripts/notifications.js b/scripts/notifications.jsindex e8c8197..a6144f3 100644--- a/scripts/notifications.js+++ b/scripts/notifications.js@@ -464,6 +464,47 @@ function urlBase64ToUint8Array(base64String) {return outputArray;}+// Builds (or replaces) one device's row in the list from what+// savepushsubscription.php handed back, without touching anything else on+// the page - in particular, without a reload, which would reset every+// notification section back to its default collapsed state along with it.+//+// user_agent is a raw client-supplied HTTP header, so it is never spliced+// into innerHTML: the name goes in through .textContent, and the "delete"+// button through addEventListener rather than an onclick="..." string built+// from the same value.+function renderPushDeviceRow(subscription) {+ const list = document.getElementById("pushDevicesList");+ const existingRow = list.querySelector(`.push-device-row[data-subscriptionid="${subscription.id}"]`);++ const row = existingRow || document.createElement("div");+ row.className = "push-device-row";+ row.setAttribute("data-subscriptionid", subscription.id);+ row.innerHTML = "";++ const name = document.createElement("span");+ name.className = "push-device-name";+ name.textContent = subscription.user_agent;+ row.appendChild(name);++ const deleteButton = document.createElement("button");+ deleteButton.type = "button";+ deleteButton.className = "secondary-button thin";+ deleteButton.textContent = translate('delete');+ deleteButton.addEventListener('click', function () {+ removePushSubscriptionButton(subscription.id, row);+ });+ row.appendChild(deleteButton);++ if (!existingRow) {+ const noDevices = document.getElementById("noPushDevices");+ if (noDevices) {+ noDevices.remove();+ }+ list.appendChild(row);+ }+}+function subscribePushButtonClick() {const button = document.getElementById("subscribePushButton");@@ -499,11 +540,12 @@ function subscribePushButtonClick() {return response.json();}).then(function (data) {if (data.success) {- location.reload();+ renderPushDeviceRow(data.subscription);+ showSuccessMessage(data.message);} else {showErrorMessage(data.message);- button.disabled = false;}+ button.disabled = false;}).catch(function (error) {showErrorMessage(error);button.disabled = false;@@ -511,7 +553,9 @@ function subscribePushButtonClick() {});}-function removePushSubscriptionButton(id) {+function removePushSubscriptionButton(id, row) {+ row = row || document.querySelector(`.push-device-row[data-subscriptionid="${id}"]`);+fetch('endpoints/notifications/removepushsubscription.php', {method: 'POST',headers: {@@ -523,7 +567,18 @@ function removePushSubscriptionButton(id) {.then(response => response.json()).then(data => {if (data.success) {- location.reload();+ if (row) {+ row.remove();+ }++ const list = document.getElementById("pushDevicesList");+ if (list && !list.querySelector(".push-device-row")) {+ const noDevices = document.createElement("p");+ noDevices.id = "noPushDevices";+ noDevices.className = "push-no-devices";+ noDevices.textContent = translate('no_devices_registered');+ list.appendChild(noDevices);+ }} else {showErrorMessage(data.message);}