import { defineStore, storeToRefs } from "pinia"; import { ref, computed } from "vue"; import { useProjectsStore } from "./projects"; export const useUserStore = defineStore("user", () => { const user = ref(null); const { projects } = storeToRefs(useProjectsStore()); 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; }); } return project; }); } function readAllNotifications(notificationId) { user.value.notifications.forEach((notification) => { notification.isRead = true; }); } function canEditComment(comment) { return user.value.uuid === comment.author.uuid; } return { user, notifications, readNotification, readAllNotifications, canEditComment, }; });