refactor readNotification
This commit is contained in:
parent
2071d9bd6d
commit
f2758679d5
1 changed files with 28 additions and 36 deletions
|
|
@ -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) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue