diff --git a/src/stores/user.js b/src/stores/user.js index 3666b8f..8b43db6 100644 --- a/src/stores/user.js +++ b/src/stores/user.js @@ -7,45 +7,37 @@ export const useUserStore = defineStore("user", () => { const { projects } = storeToRefs(useProjectsStore()); - const notifications = computed( - () => { - return projects.value.reduce((acc, project) => { - if (!project.notifications) return acc; + const notifications = computed(() => { + return projects.value.flatMap((project) => { + if (!project.notifications) return []; - const projectNotifications = project.notifications.map( - (notification) => { - const newNotification = { - ...notification, - project: project, - isRead: notification.readby?.includes(user.value.uuid), - }; - - return newNotification; - } - ); - - const userNotifications = projectNotifications.filter( - (notification) => notification.author.uuid !== user.value.uuid - ); - - return [...acc, ...userNotifications]; - }, []); - }, - { deep: true } - ); + return project.notifications + .filter((notification) => notification.author.uuid !== user.value.uuid) + .map((notification) => ({ + ...notification, + project: project, + isRead: notification.readby?.includes(user.value.uuid), + })); + }); + }); function readNotification(notificationId, projectSlug) { - projects.value = projects.value.map((project) => { - if (project.slug === projectSlug) { - project.notifications = project.notifications.map((notification) => { - if (notification.id === notificationId) { - notification.readby.push(user.value.uuid); - } - return notification; - }); - } - return project; - }); + projects.value = projects.value.map((project) => ({ + ...project, + notifications: + project.slug === projectSlug + ? project.notifications.map((notification) => + notification.id === notificationId + ? { + ...notification, + readby: [ + ...new Set([...notification.readby, user.value.uuid]), + ], + } + : notification + ) + : project.notifications, + })); } function readAllNotifications(notificationId) {