designtopack/src/stores/user.js

66 lines
1.9 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 15:06:38 +01:00
const notifications = computed(() => {
return projects.value.flatMap((project) => {
if (!project.notifications) return [];
return project.notifications
.filter((notification) => notification.author.uuid !== user.value.uuid)
.map((notification) => ({
...notification,
project: project,
isRead: notification.readby?.includes(user.value.uuid),
}));
});
});
2025-01-15 14:56:17 +01:00
2025-01-15 16:24:34 +01:00
function readNotification(notificationId, projectId) {
console.log("Read notification", notificationId, projectId);
2025-01-15 15:06:38 +01:00
projects.value = projects.value.map((project) => ({
...project,
notifications:
2025-01-15 16:24:34 +01:00
project.uuid === projectId || project.uri === projectId
2025-01-15 15:06:38 +01:00
? project.notifications.map((notification) =>
notification.id === notificationId
? {
...notification,
readby: [
...new Set([...notification.readby, user.value.uuid]),
],
}
: notification
)
: project.notifications,
}));
2024-11-18 09:36:15 +01:00
}
2025-01-15 15:11:02 +01:00
function readAllNotifications() {
2025-01-15 15:09:58 +01:00
projects.value = projects.value.map((project) => ({
...project,
notifications: project.notifications.map((notification) => ({
...notification,
readby: [...new Set([...notification.readby, user.value.uuid])],
})),
}));
}
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
});