designtopack/src/stores/user.js

71 lines
2 KiB
JavaScript
Raw Normal View History

2025-04-30 16:21:56 +02:00
import { defineStore, storeToRefs } from 'pinia';
import { ref, computed } from 'vue';
import { useProjectsStore } from './projects';
2024-09-10 12:09:53 +02:00
2025-04-30 16:21:56 +02:00
export const useUserStore = defineStore('user', () => {
2024-09-10 12:09:53 +02:00
const user = ref(null);
2025-04-30 16:21:56 +02:00
const isLogged = computed(() => {
return user.value?.hasOwnProperty('role');
});
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) {
2025-04-30 16:21:56 +02:00
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,
2025-04-30 16:21:56 +02:00
isLogged,
2025-01-07 11:33:15 +01:00
notifications,
readNotification,
readAllNotifications,
canEditComment,
};
2024-09-10 12:09:53 +02:00
});