designtopack/src/stores/user.js

69 lines
1.8 KiB
JavaScript
Raw Normal View History

2025-01-10 17:40:45 +01:00
import { defineStore, storeToRefs } from "pinia";
2024-10-30 10:56:11 +01:00
import { ref, computed } from "vue";
2025-01-10 17:40:45 +01:00
import { useProjectsStore } from "./projects";
2024-09-10 12:09:53 +02:00
export const useUserStore = defineStore("user", () => {
const user = ref(null);
2025-01-10 17:40:45 +01:00
const { projects } = storeToRefs(useProjectsStore());
2025-01-15 14:56:17 +01:00
const notifications = computed(
() => {
return projects.value.reduce((acc, project) => {
if (!project.notifications) return acc;
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 }
);
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;
});
2024-11-18 09:36:15 +01:00
}
2025-01-15 14:56:17 +01:00
return project;
2024-11-18 09:36:15 +01:00
});
}
function readAllNotifications(notificationId) {
user.value.notifications.forEach((notification) => {
2025-01-10 17:40:45 +01:00
notification.isRead = true;
});
}
2025-01-07 11:33:15 +01:00
function canEditComment(comment) {
return user.value.uuid === comment.author.uuid;
}
return {
user,
notifications,
readNotification,
readAllNotifications,
canEditComment,
};
2024-09-10 12:09:53 +02:00
});