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
This commit is contained in:
Elia Bieri 2023-02-28 08:12:22 +00:00
parent 2d580cad18
commit d686173b31
1 changed files with 15 additions and 3 deletions

View File

@ -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;
}
const userStore = useUserStore();
let timerHandle: number | null = null;
watch(userStore, () => {
if (userStore.loggedIn && timerHandle === null) {
log.debug("Notification polling started");
updateUnreadCount();
setInterval(async () => await updateUnreadCount(), 30000);
timerHandle = setInterval(async () => await updateUnreadCount(), 30000);
} else if (!userStore.loggedIn) {
log.debug("Notification polling stopped");
timerHandle = null;
}
});
return { loadNotifications, hasUnread, allCount };
});