diff --git a/openwisp_notifications/static/openwisp-notifications/js/unsubscribe.js b/openwisp_notifications/static/openwisp-notifications/js/unsubscribe.js index 5926f5b0..ef65cd5f 100644 --- a/openwisp_notifications/static/openwisp-notifications/js/unsubscribe.js +++ b/openwisp_notifications/static/openwisp-notifications/js/unsubscribe.js @@ -21,25 +21,30 @@ function updateSubscription(subscribe) { if (subscribe) { statusMessage.textContent = gettext('You are currently subscribed to notifications.'); toggleBtn.textContent = gettext('Unsubscribe'); - confirmationMsg.textContent = data.message; + toggleBtn.setAttribute('data-hasSubscribe', 'true'); + confirmationMsg.textContent = 'Successfully subscribed!'; } else { statusMessage.textContent = gettext('You are currently unsubscribed from notifications.'); toggleBtn.textContent = gettext('Subscribe'); - confirmationMsg.textContent = data.message; + toggleBtn.setAttribute('data-hasSubscribe', 'false'); + confirmationMsg.textContent = 'Successfully unsubscribed!'; } confirmationMsg.style.display = 'block'; } else { window.alert(data.message); } + }) + .catch(error => { + console.error('Error:', error); }); } document.addEventListener('DOMContentLoaded', function() { const toggleBtn = document.getElementById('toggle-btn'); toggleBtn.addEventListener('click', function() { - const currentStatus = toggleBtn.textContent.trim().toLowerCase(); - const subscribe = currentStatus === gettext('subscribe').toLowerCase(); + const isSubscribed = toggleBtn.getAttribute('data-hasSubscribe') === 'true'; + const subscribe = !isSubscribed; updateSubscription(subscribe); }); }); diff --git a/openwisp_notifications/templates/openwisp_notifications/unsubscribe.html b/openwisp_notifications/templates/openwisp_notifications/unsubscribe.html index 62fb0325..41438570 100644 --- a/openwisp_notifications/templates/openwisp_notifications/unsubscribe.html +++ b/openwisp_notifications/templates/openwisp_notifications/unsubscribe.html @@ -29,7 +29,7 @@