refactor readNotification

This commit is contained in:
isUnknown 2025-01-15 15:06:38 +01:00
parent 2071d9bd6d
commit f2758679d5

View file

@ -7,45 +7,37 @@ export const useUserStore = defineStore("user", () => {
const { projects } = storeToRefs(useProjectsStore()); const { projects } = storeToRefs(useProjectsStore());
const notifications = computed( const notifications = computed(() => {
() => { return projects.value.flatMap((project) => {
return projects.value.reduce((acc, project) => { if (!project.notifications) return [];
if (!project.notifications) return acc;
const projectNotifications = project.notifications.map( return project.notifications
(notification) => { .filter((notification) => notification.author.uuid !== user.value.uuid)
const newNotification = { .map((notification) => ({
...notification, ...notification,
project: project, project: project,
isRead: notification.readby?.includes(user.value.uuid), 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 }
);
function readNotification(notificationId, projectSlug) { function readNotification(notificationId, projectSlug) {
projects.value = projects.value.map((project) => { projects.value = projects.value.map((project) => ({
if (project.slug === projectSlug) { ...project,
project.notifications = project.notifications.map((notification) => { notifications:
if (notification.id === notificationId) { project.slug === projectSlug
notification.readby.push(user.value.uuid); ? project.notifications.map((notification) =>
} notification.id === notificationId
return notification; ? {
}); ...notification,
} readby: [
return project; ...new Set([...notification.readby, user.value.uuid]),
}); ],
}
: notification
)
: project.notifications,
}));
} }
function readAllNotifications(notificationId) { function readAllNotifications(notificationId) {