diff --git a/src/lib/notifications.ts b/src/lib/notifications.ts index 5f8cc92e..fafd3612 100644 --- a/src/lib/notifications.ts +++ b/src/lib/notifications.ts @@ -46,11 +46,17 @@ export function sendNotification( } -async function unsubscribe() { +export async function unsubscribe() { if ('serviceWorker' in navigator) { const registration = await navigator.serviceWorker.ready; const subscription = await registration.pushManager.getSubscription(); if (subscription) { + const res = await fetch('/api/deletePushSubscription', { + method: 'GET', + headers: { + 'Content-Type': 'application/json' + }, + }); await subscription.unsubscribe(); } } diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index 2a66ec63..2aeac013 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -132,7 +132,7 @@ onMount(async () => { if (data?.user?.id) { await loadWorker(); - + /* const permissionGranted = await requestNotificationPermission(); if (permissionGranted) { @@ -142,6 +142,7 @@ await subscribeUser(); } } + */ } await checkMarketHour(); diff --git a/src/routes/api/deletePushSubscription/+server.ts b/src/routes/api/deletePushSubscription/+server.ts index cd54f089..71beaf10 100644 --- a/src/routes/api/deletePushSubscription/+server.ts +++ b/src/routes/api/deletePushSubscription/+server.ts @@ -3,22 +3,15 @@ import { error} from '@sveltejs/kit'; -export const POST = (async ({ locals, request }) => { +export const GET = (async ({ locals }) => { const { user, pb } = locals; if (!user?.id) { console.log('No username passed to addSubscription'); throw error(401, 'Unauthorized'); } + - const data = await request.json(); - - if (!data?.subscription) { - console.log('No subscription passed to unsubscribe', data); - throw error(400, 'Bad Request'); - } - - const output = await pb.collection("pushSubscription").getFullList({ filter: `user="${user?.id}"`, }); diff --git a/src/routes/api/sendPushSubscription/+server.ts b/src/routes/api/sendPushSubscription/+server.ts index e493d407..7d434e48 100644 --- a/src/routes/api/sendPushSubscription/+server.ts +++ b/src/routes/api/sendPushSubscription/+server.ts @@ -13,14 +13,12 @@ webPush.setVapidDetails( export const POST: RequestHandler = async ({ request, locals }) => { const { pb, apiKey } = locals; - const { title, body, key } = await request?.json(); + const { body, key } = await request?.json(); if (apiKey === key) { try { - console.log(title, body) - // Get all push subscriptions const subscriptions = await pb.collection('pushSubscription').getFullList({ sort: '-created' @@ -53,7 +51,7 @@ export const POST: RequestHandler = async ({ request, locals }) => { } } else { - console.log('api key wrong') + console.log('key is wrong') } }; diff --git a/src/routes/profile/+page.svelte b/src/routes/profile/+page.svelte index 36f08d78..0b2ecc7e 100644 --- a/src/routes/profile/+page.svelte +++ b/src/routes/profile/+page.svelte @@ -1,13 +1,21 @@ - - - {$numberOfUnreadNotification > 0 ? `(${$numberOfUnreadNotification})` : ""} - My Account · Stocknear - - - - - - - - - - - - - - - +

User Information @@ -206,7 +226,62 @@

+

+ Push Notification +

+
+ {#if nottifPermGranted === null} +

Checking permissions...

+ {:else if nottifPermGranted === true} + {#if isPushSubscribed} +

Push notifications are currently active.

+
+ +
+ {:else} +

+ Stay up-to-date with real-time price alerts, the latest + stock news, and earnings calls delivered straight to your + device. +

+ + {/if} + + + {/if} +
+
+ +

Manage Subscription @@ -290,7 +365,7 @@ > @@ -306,7 +381,7 @@ : 'cursor-not-allowed'} {subscriptionData?.card_brand !== null && subscriptionData?.card_brand?.length !== 0 ? 'bg-white sm:hover:bg-white/80 text-black font-semibold' - : 'bg-gray-600 opacity-[0.8] text-white'} text-sm sm:text-[1rem] px-4 py-2 rounded-md mt-5" + : 'bg-gray-600 opacity-[0.8] text-white'} text-sm sm:text-[1rem] px-4 py-2 rounded mt-5" > Change to Annual Plan @@ -315,7 +390,7 @@ {:else if subscriptionData?.status_formatted === "Cancelled"} @@ -331,7 +406,7 @@

Need help?

@@ -405,7 +480,7 @@ on:click={() => (isClicked = !isClicked)} class="{!isClicked ? '' - : 'hidden'} cursor-pointer px-7 py-2 mb-5 rounded-md bg-red-600 text-center text-white text-[1rem] font-normal" + : 'hidden'} cursor-pointer px-7 py-2 mb-5 rounded bg-red-600 text-center text-white text-[1rem] font-normal" > Cancel Subscription (isClicked = !isClicked)} class="{!isClicked ? '' - : 'hidden'} cursor-pointer px-7 py-2 mb-5 rounded-md bg-[#fff] sm:hover:bg-gray-300 text-center text-black text-[1rem] font-medium" + : 'hidden'} cursor-pointer px-7 py-2 mb-5 rounded bg-[#fff] sm:hover:bg-gray-300 text-center text-black text-[1rem] font-medium" > Proceed