From d686173b31b96c5cbb6f0facc1e06eff8f285ccc Mon Sep 17 00:00:00 2001 From: Elia Bieri Date: Tue, 28 Feb 2023 08:12:22 +0000 Subject: [PATCH] Merged in bugfix/VBV-261-deactivate-notifications-when-logged-out (pull request #29) VBV-261 start & stop notification polling based on user state * Start & stop notification polling based on user state * Improve readability Approved-by: Christian Cueni --- client/src/stores/notifications.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) 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 }; });