Skip to content

Commit b667bcb

Browse files
author
Ravi Singh
committed
fix: push notification toggle — add disable functionality
Toggle was display-only with no onClick handler. Now functional: - Enable: subscribes via pushManager + sends to server - Disable: unsubscribes from pushManager + removes from server - Changed unsubscribe endpoint from DELETE to POST (body support)
1 parent 128aeb3 commit b667bcb

2 files changed

Lines changed: 21 additions & 6 deletions

File tree

pwa/client/src/pages/Settings.jsx

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,24 @@ export default function Settings() {
7777
}
7878
};
7979

80+
const disablePush = async () => {
81+
setPushLoading(true);
82+
try {
83+
const reg = await navigator.serviceWorker?.ready;
84+
const sub = await reg?.pushManager?.getSubscription();
85+
if (sub) {
86+
const { endpoint } = sub.toJSON();
87+
await api.post('/api/push/unsubscribe', { endpoint });
88+
await sub.unsubscribe();
89+
}
90+
setPushEnabled(false);
91+
} catch (err) {
92+
setPushError(`Failed to disable: ${err.message}`);
93+
} finally {
94+
setPushLoading(false);
95+
}
96+
};
97+
8098
const handleSiteSave = async () => {
8199
if (editingSite) {
82100
await updateSite(editingSite.id, siteForm);
@@ -132,12 +150,9 @@ export default function Settings() {
132150
</div>
133151
{typeof Notification === 'undefined' ? (
134152
<span className="text-xs text-slate-500">N/A</span>
135-
) : pushEnabled ? (
136-
<ToggleSwitch on={true} />
137153
) : (
138-
<button onClick={enablePush} disabled={pushLoading}
139-
className="text-xs text-water bg-water/10 px-3 py-1.5 rounded-lg font-medium disabled:opacity-50">
140-
{pushLoading ? 'Setting up...' : 'Enable'}
154+
<button onClick={pushEnabled ? disablePush : enablePush} disabled={pushLoading}>
155+
<ToggleSwitch on={pushEnabled} />
141156
</button>
142157
)}
143158
</div>

pwa/server-cloud/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ app.post('/api/push/subscribe', { preHandler: [app.authenticate] }, async (req)
400400
return { success: true };
401401
});
402402

403-
app.delete('/api/push/unsubscribe', { preHandler: [app.authenticate] }, async (req) => {
403+
app.post('/api/push/unsubscribe', { preHandler: [app.authenticate] }, async (req) => {
404404
const { endpoint } = req.body || {};
405405
await db.run('DELETE FROM push_subs WHERE user_id = $1 AND endpoint = $2', req.user.id, endpoint);
406406
return { success: true };

0 commit comments

Comments
 (0)