designtopack/src/stores/user.js

59 lines
1.5 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());
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;
2025-01-15 14:18:48 +01:00
const projectNotifications = project.notifications.map((notification) => {
const newNotification = {
2025-01-10 17:40:45 +01:00
...notification,
2025-01-15 14:18:48 +01:00
project: project,
isRead: notification.readby?.includes(user.value.uuid),
};
return newNotification;
});
const userNotifications = projectNotifications.filter(
(notification) => notification.author.uuid !== user.value.uuid
2025-01-10 17:40:45 +01:00
);
2025-01-15 14:18:48 +01:00
return [...acc, ...userNotifications];
2025-01-10 17:40:45 +01:00
}, []);
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
});
}
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
});