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 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) {