Skip to content

Commit 8763db9

Browse files
Update notification.js
1 parent baedac3 commit 8763db9

1 file changed

Lines changed: 20 additions & 13 deletions

File tree

public/js/modules/notification.js

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function urlBase64ToUint8Array(base64String) {
3232
}
3333

3434
/**
35-
* Unsubscribes the user from the current push subscription, if one exists.
35+
* Unsubscribes the user from the current push subscription on this device.
3636
* @returns {Promise<boolean>} - True if successful or already unsubscribed.
3737
*/
3838
async function unsubscribeCurrentUser() {
@@ -43,9 +43,13 @@ async function unsubscribeCurrentUser() {
4343
try {
4444
const subscription = await appState.swRegistration.pushManager.getSubscription();
4545
if (subscription) {
46-
await unsubscribeFromPush(subscription.endpoint); // Notify backend first
47-
await subscription.unsubscribe();
48-
console.log('[NOTIF] User unsubscribed successfully.');
46+
// --- **FIX: Only notify the backend if we have a valid endpoint to send** ---
47+
// This prevents the 400 error if the subscription is stale or malformed.
48+
if (subscription.endpoint) {
49+
await unsubscribeFromPush(subscription.endpoint); // Notify backend to delete this specific subscription.
50+
}
51+
await subscription.unsubscribe(); // This removes the subscription from the browser itself.
52+
console.log('[NOTIF] User unsubscribed successfully from this device.');
4953
}
5054
isSubscribed = false;
5155
return true;
@@ -76,7 +80,7 @@ export async function subscribeUserToPush(force = false) {
7680
isSubscribed = (subscription !== null);
7781

7882
if (isSubscribed) {
79-
console.log('[NOTIF] User is already subscribed.');
83+
console.log('[NOTIF] User is already subscribed on this device.');
8084
if (force) showNotification('Subscription refreshed successfully!');
8185
} else {
8286
console.log('[NOTIF] User is NOT subscribed. Attempting to subscribe...');
@@ -92,7 +96,7 @@ export async function subscribeUserToPush(force = false) {
9296
applicationServerKey: applicationServerKey
9397
});
9498

95-
console.log('[NOTIF] New push subscription created:', newSubscription);
99+
console.log('[NOTIF] New push subscription created for this device:', newSubscription);
96100
const success = await subscribeToPush(newSubscription);
97101
if (success) {
98102
isSubscribed = true;
@@ -120,7 +124,7 @@ export const loadAndScheduleNotifications = async () => {
120124
guideState.userNotifications = notifications;
121125
console.log(`[NOTIF] Loaded ${notifications.length} scheduled notifications from server.`);
122126

123-
renderNotificationSettings(); // Render the settings/actions panel
127+
renderNotificationSettings();
124128
renderNotifications();
125129
renderPastNotifications();
126130
await handleSearchAndFilter(false);
@@ -140,8 +144,11 @@ export const addOrRemoveNotification = async (programDetails) => {
140144
`Are you sure you want to remove the notification for "${programDetails.programTitle}"?`,
141145
async () => {
142146
if (await deleteProgramNotification(existingNotification.id)) {
143-
notificationChannel.postMessage({ type: 'refresh-notifications' });
144147
showNotification(`Notification for "${programDetails.programTitle}" removed.`);
148+
// --- **FIX: Refresh UI immediately** ---
149+
// Post message for other tabs, then call directly to refresh the current tab.
150+
notificationChannel.postMessage({ type: 'refresh-notifications' });
151+
loadAndScheduleNotifications();
145152
}
146153
}
147154
);
@@ -196,8 +203,11 @@ export const addOrRemoveNotification = async (programDetails) => {
196203
};
197204

198205
if (await addProgramNotification(newNotificationData)) {
199-
notificationChannel.postMessage({ type: 'refresh-notifications' });
200206
showNotification(`Notification set for "${newNotificationData.programTitle}"!`);
207+
// --- **FIX: Refresh UI immediately** ---
208+
// Post message for other tabs, then call directly to refresh the current tab.
209+
notificationChannel.postMessage({ type: 'refresh-notifications' });
210+
loadAndScheduleNotifications();
201211
}
202212
}
203213
};
@@ -209,10 +219,6 @@ export const findNotificationForProgram = (program, channelId) => {
209219
);
210220
};
211221

212-
/**
213-
* --- **FIX: New function to render the notification settings/actions** ---
214-
* This creates the UI for the "Re-subscribe" button.
215-
*/
216222
export const renderNotificationSettings = () => {
217223
const settingsEl = UIElements.notificationSettings;
218224
if (!settingsEl) return;
@@ -345,6 +351,7 @@ const setupNotificationListEventListeners = () => {
345351
async () => {
346352
if (await deleteProgramNotification(notificationId)) {
347353
notificationChannel.postMessage({ type: 'refresh-notifications' });
354+
loadAndScheduleNotifications();
348355
}
349356
}
350357
);

0 commit comments

Comments
 (0)