diff --git a/client/src/stores/notifications.ts b/client/src/stores/notifications.ts index 104b3e9f..ec6dcdf3 100644 --- a/client/src/stores/notifications.ts +++ b/client/src/stores/notifications.ts @@ -1,7 +1,9 @@ import { itGet } from "@/fetchHelpers"; +import { useUserStore } from "@/stores/user"; import type { Notification } from "@/types"; +import log from "loglevel"; import { defineStore } from "pinia"; -import { ref } from "vue"; +import { ref, watch } from "vue"; type NotificationListData = { all_count: number; @@ -29,8 +31,18 @@ export const useNotificationsStore = defineStore("notifications", () => { hasUnread.value = data.unread_count !== 0; } - updateUnreadCount(); - setInterval(async () => await updateUnreadCount(), 30000); + const userStore = useUserStore(); + let timerHandle: number | null = null; + watch(userStore, () => { + if (userStore.loggedIn && timerHandle === null) { + log.debug("Notification polling started"); + updateUnreadCount(); + timerHandle = setInterval(async () => await updateUnreadCount(), 30000); + } else if (!userStore.loggedIn) { + log.debug("Notification polling stopped"); + timerHandle = null; + } + }); return { loadNotifications, hasUnread, allCount }; });