diff --git a/src/lib/components/NotificationBell.svelte b/src/lib/components/NotificationBell.svelte index afe78e84..82dfa019 100644 --- a/src/lib/components/NotificationBell.svelte +++ b/src/lib/components/NotificationBell.svelte @@ -1,36 +1,36 @@ - - - - {#if data?.user} - - - + +{#if data?.user} + + {#if hasUnreadElement} -
-
- {$numberOfUnreadNotification <= 9 ? $numberOfUnreadNotification : '9+' } +
+
+ {$numberOfUnreadNotification <= 9 + ? $numberOfUnreadNotification + : "9+"} +
-
- {/if} -
- {/if} - - \ No newline at end of file diff --git a/src/lib/workers/notificationWorker.ts b/src/lib/workers/notificationWorker.ts index 16711c6b..b5db2b80 100644 --- a/src/lib/workers/notificationWorker.ts +++ b/src/lib/workers/notificationWorker.ts @@ -1,28 +1,27 @@ // lib/workers/test.ts -async function loadNotifications(userId: string) { - const postData = { userId: userId }; - +async function loadNotifications() { const response = await fetch("/api/get-notifications", { - method: "POST", + method: "GET", headers: { "Content-Type": "application/json", }, - body: JSON.stringify(postData), }); - const output = (await response.json())?.items; + const output = await response.json(); + return output; } onmessage = async (event: MessageEvent) => { const data = event.data?.message; - const userId = data?.userId; try { - const [notificationList] = await Promise.all([loadNotifications(userId)]); - - const numberOfUnreadNotification = notificationList?.length; - const hasUnreadElement = notificationList?.length !== 0 ? true : false; + const [notificationList] = await Promise.all([loadNotifications()]); + const numberOfUnreadNotification = notificationList.filter( + (item?) => !item?.readed, + ); + const hasUnreadElement = + numberOfUnreadNotification?.length !== 0 ? true : false; const output = { notificationList, hasUnreadElement, diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index bf291d74..aaa777e8 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -136,7 +136,7 @@ notificationList = output?.notificationList; hasUnreadElement = output?.hasUnreadElement; //const unreadNotificationList = output?.unreadNotificationList; - $numberOfUnreadNotification = output?.numberOfUnreadNotification; + $numberOfUnreadNotification = output?.numberOfUnreadNotification?.length; //pushNotification() }; @@ -163,18 +163,19 @@ const handleTwitchMessage = (event) => { // Implement fallback logic here, e.g., using timers or other techniques console.log("Fallback worker activated"); - const postData = { userId: data?.user?.id }; const response = await fetch("/api/get-notifications", { - method: "POST", + method: "GET", headers: { "Content-Type": "application/json", }, - body: JSON.stringify(postData), }); - notificationList = (await response.json())?.items; - hasUnreadElement = notificationList?.length !== 0 ? true : false; - $numberOfUnreadNotification = notificationList?.length; + notificationList = await response.json(); + const numberOfUnreadNotification = notificationList.filter( + (item?) => !item?.readed, + ); + hasUnreadElement = numberOfUnreadNotification?.length !== 0 ? true : false; + $numberOfUnreadNotification = numberOfUnreadNotification?.length; } let Cookie; diff --git a/src/routes/api/get-notifications/+server.ts b/src/routes/api/get-notifications/+server.ts index 9bea99c8..f5b14c23 100644 --- a/src/routes/api/get-notifications/+server.ts +++ b/src/routes/api/get-notifications/+server.ts @@ -1,18 +1,20 @@ import type { RequestHandler } from "./$types"; -export const POST: RequestHandler = async ({ request, locals }) => { - const data = await request.json(); - const { fastifyURL } = locals; +export const GET: RequestHandler = async ({ locals }) => { + const { pb, user } = locals; - const response = await fetch(fastifyURL + "/get-notifications", { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify(data), - }); + let output; - const output = await response.json(); + try { + output = await pb.collection("notifications")?.getFullList({ + filter: `opUser="${user?.id}" `, + expand: "user,post,comment", + sort: "-created", + }); + } catch (e) { + console.log(e); + output = []; + } return new Response(JSON.stringify(output)); }; diff --git a/src/routes/notifications/+page.server.ts b/src/routes/notifications/+page.server.ts index e248c98c..6fcc284a 100644 --- a/src/routes/notifications/+page.server.ts +++ b/src/routes/notifications/+page.server.ts @@ -1,25 +1,21 @@ import { redirect, error } from "@sveltejs/kit"; -export const load = async ({ locals }) => { - const { pb, user } = locals; +export const load = async ({ locals, fetch }) => { + const { pb } = locals; if (!pb.authStore.isValid) { redirect(303, "/login"); } async function getNotifications() { - let output; + const response = await fetch("/api/get-notifications", { + method: "GET", + headers: { + "Content-Type": "application/json", + }, + }); - try { - output = await pb.collection("notifications")?.getFullList({ - filter: `opUser="${user?.id}" `, - expand: "user,post,comment", - sort: "-created", - }); - } catch (e) { - console.log(e); - output = []; - } + const output = await response.json(); return output; }