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());
|
|
|
|
|
|
2024-10-30 10:56:11 +01:00
|
|
|
const notifications = computed(() => {
|
2025-01-10 17:40:45 +01:00
|
|
|
return projects.value.reduce((acc, project) => {
|
|
|
|
|
if (!project.notifications) return acc;
|
|
|
|
|
|
|
|
|
|
const projectNotifications = project.notifications.map(
|
|
|
|
|
(notification) => ({
|
|
|
|
|
...notification,
|
|
|
|
|
project: {
|
|
|
|
|
id: project.id,
|
|
|
|
|
name: project.title,
|
|
|
|
|
},
|
|
|
|
|
isRead:
|
|
|
|
|
notification.author.uuid === user.value.uuid ||
|
|
|
|
|
notification.readby?.includes(user.value.uuid),
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return [...acc, ...projectNotifications];
|
|
|
|
|
}, []);
|
2024-10-30 10:56:11 +01:00
|
|
|
});
|
|
|
|
|
|
2024-11-18 09:36:15 +01:00
|
|
|
function readNotification(notificationId) {
|
2025-01-10 17:40:45 +01:00
|
|
|
notifications.value = notifications.value.map((notification) => {
|
2024-11-18 09:36:15 +01:00
|
|
|
if (notification.id === notificationId) {
|
2025-01-10 17:40:45 +01:00
|
|
|
notification.isRead = true;
|
2024-11-18 09:36:15 +01:00
|
|
|
}
|
2025-01-10 17:40:45 +01:00
|
|
|
return notification;
|
2024-11-18 09:36:15 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-18 13:45:40 +01:00
|
|
|
function readAllNotifications(notificationId) {
|
|
|
|
|
user.value.notifications.forEach((notification) => {
|
2025-01-10 17:40:45 +01:00
|
|
|
notification.isRead = true;
|
2024-11-18 13:45:40 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
});
|