diff --git a/assets/vue/components/layout/TopbarLoggedIn.vue b/assets/vue/components/layout/TopbarLoggedIn.vue index 896a21cb98b..04819396c65 100644 --- a/assets/vue/components/layout/TopbarLoggedIn.vue +++ b/assets/vue/components/layout/TopbarLoggedIn.vue @@ -16,7 +16,7 @@ @@ -115,13 +115,14 @@ const userSubmenuItems = computed(() => [ }, ]) -function toogleUserMenu(event) { +function toggleUserMenu(event) { elUserSubmenu.value.toggle(event) } -const btnInboxBadge = computed(() => - messageRelUserStore.countUnread > 9 ? "9+" : messageRelUserStore.countUnread.toString(), -) +const btnInboxBadge = computed(() => { + const unreadCount = messageRelUserStore.countUnread + return unreadCount > 20 ? "9+" : unreadCount > 0 ? unreadCount.toString() : null +}) messageRelUserStore.findUnreadCount().catch((e) => notification.showErrorNotification(e)) diff --git a/assets/vue/services/message.js b/assets/vue/services/message.js index 5bc6a575a62..7c3a1951ba5 100644 --- a/assets/vue/services/message.js +++ b/assets/vue/services/message.js @@ -17,6 +17,12 @@ async function create(message) { return await baseService.post("/api/messages", message) } +async function countUnreadMessages(params) { + const queryParams = new URLSearchParams(params).toString() + return await baseService.get(`/api/messages?${queryParams}`) +} + export const messageService = { create, + countUnreadMessages, } diff --git a/assets/vue/store/messageRelUserStore.js b/assets/vue/store/messageRelUserStore.js index 484abeee9ff..7bd40d84fcd 100644 --- a/assets/vue/store/messageRelUserStore.js +++ b/assets/vue/store/messageRelUserStore.js @@ -1,6 +1,7 @@ import { defineStore } from "pinia" import { useSecurityStore } from "./securityStore" -import messageRelUSerService from "../services/messagereluser" +import { messageService } from "../services/message" +import { MESSAGE_TYPE_INBOX } from "../components/message/constants" export const useMessageRelUserStore = defineStore("messageRelUser", { state: () => ({ @@ -10,16 +11,25 @@ export const useMessageRelUserStore = defineStore("messageRelUser", { async findUnreadCount() { const securityStore = useSecurityStore() - const response = await messageRelUSerService.findAll({ - params: { - read: false, - receiver: securityStore.user["@id"], + try { + const params = { + "order[sendDate]": "desc", + "receivers.read": false, + "receivers.receiver": securityStore.user["@id"], itemsPerPage: 1, - }, - }) - const json = await response.json() + msgType: MESSAGE_TYPE_INBOX, + } + const response = await messageService.countUnreadMessages(params) - this.countUnread = json["hydra:totalItems"] + if (response && response["hydra:totalItems"] !== undefined) { + this.countUnread = response["hydra:totalItems"] + } else { + this.countUnread = 0 + } + } catch (error) { + console.error("Error fetching unread count:", error) + this.countUnread = 0 + } }, }, })