read notification working

This commit is contained in:
isUnknown 2025-01-15 14:56:17 +01:00
parent 0b472988a2
commit 2071d9bd6d
4 changed files with 57 additions and 48 deletions

View file

@ -86,6 +86,7 @@ import { useDialogStore } from "../../stores/dialog";
import { computed, onMounted, ref, useTemplateRef } from "vue"; import { computed, onMounted, ref, useTemplateRef } from "vue";
import { storeToRefs } from "pinia"; import { storeToRefs } from "pinia";
import { usePageStore } from "../../stores/page"; import { usePageStore } from "../../stores/page";
import { useRoute } from "vue-router";
dayjs.locale("fr"); dayjs.locale("fr");
@ -96,6 +97,7 @@ const { comment, commentIndex } = defineProps({
const emits = defineEmits(["update:file", "close:comment"]); const emits = defineEmits(["update:file", "close:comment"]);
const route = useRoute();
const userStore = useUserStore(); const userStore = useUserStore();
const api = useApiStore(); const api = useApiStore();
const dialog = useDialogStore(); const dialog = useDialogStore();
@ -146,8 +148,7 @@ async function read() {
if (getStatus.value !== "unread") return; if (getStatus.value !== "unread") return;
try { try {
const newNotification = await api.readNotification(comment, page.value.uri); const newNotification = await api.readNotification(comment, page.value.uri);
console.log(newNotification); userStore.readNotification(comment.id, route.params.id);
userStore.readNotification(comment.id);
} catch (error) { } catch (error) {
console.log("Erreur lors de la lecture de la notification : ", error); console.log("Erreur lors de la lecture de la notification : ", error);
} }

View file

@ -2,7 +2,6 @@
<article <article
class="notification | bg-white rounded-lg | p-16 | flow" class="notification | bg-white rounded-lg | p-16 | flow"
data-type="comment" data-type="comment"
@click="router.push(href)"
> >
<header> <header>
<p class="flex"> <p class="flex">
@ -39,31 +38,8 @@
</template> </template>
<script setup> <script setup>
import { useRouter } from "vue-router";
import { useNotificationsStore } from "../../stores/notifications"; import { useNotificationsStore } from "../../stores/notifications";
const router = useRouter();
const { notification } = defineProps({ notification: Object }); const { notification } = defineProps({ notification: Object });
const { formatDate } = useNotificationsStore(); const { formatDate } = useNotificationsStore();
const href = getTargetPath();
// Functions
function getTargetPath() {
const uri = notification.location.page.uri;
const isDocumentBrief =
notification.location.page.template === "client-brief" &&
notification.location?.file.type === "document";
if (isDocumentBrief) {
return notification.project.uri + "?dialog=client-brief&comments=true";
}
if (notification.location.page.template === "track") {
return notification.project.uri + "?dialog=virtual-sample&comments=true";
}
return uri;
}
</script> </script>

View file

@ -7,34 +7,44 @@ export const useUserStore = defineStore("user", () => {
const { projects } = storeToRefs(useProjectsStore()); const { projects } = storeToRefs(useProjectsStore());
const notifications = computed(() => { const notifications = computed(
return projects.value.reduce((acc, project) => { () => {
if (!project.notifications) return acc; return projects.value.reduce((acc, project) => {
if (!project.notifications) return acc;
const projectNotifications = project.notifications.map((notification) => { const projectNotifications = project.notifications.map(
const newNotification = { (notification) => {
...notification, const newNotification = {
project: project, ...notification,
isRead: notification.readby?.includes(user.value.uuid), project: project,
}; isRead: notification.readby?.includes(user.value.uuid),
};
return newNotification; return newNotification;
}); }
);
const userNotifications = projectNotifications.filter( const userNotifications = projectNotifications.filter(
(notification) => notification.author.uuid !== user.value.uuid (notification) => notification.author.uuid !== user.value.uuid
); );
return [...acc, ...userNotifications]; return [...acc, ...userNotifications];
}, []); }, []);
}); },
{ deep: true }
);
function readNotification(notificationId) { function readNotification(notificationId, projectSlug) {
notifications.value = notifications.value.map((notification) => { projects.value = projects.value.map((project) => {
if (notification.id === notificationId) { if (project.slug === projectSlug) {
notification.isRead = true; project.notifications = project.notifications.map((notification) => {
if (notification.id === notificationId) {
notification.readby.push(user.value.uuid);
}
return notification;
});
} }
return notification; return project;
}); });
} }

View file

@ -45,6 +45,7 @@
:is="notificationComponents[notification.type]" :is="notificationComponents[notification.type]"
:notification="notification" :notification="notification"
:data-status="notification.isRead ? 'read' : 'unread'" :data-status="notification.isRead ? 'read' : 'unread'"
@click="router.push(getHref(notification))"
/> />
</template> </template>
</section> </section>
@ -62,10 +63,12 @@ import { useApiStore } from "../stores/api";
import Comment from "../components/notifications/Comment.vue"; import Comment from "../components/notifications/Comment.vue";
import Reply from "../components/notifications/Reply.vue"; import Reply from "../components/notifications/Reply.vue";
import Content from "../components/notifications/Content.vue"; import Content from "../components/notifications/Content.vue";
import { useRouter } from "vue-router";
dayjs.locale("fr"); dayjs.locale("fr");
const userStore = useUserStore(); const userStore = useUserStore();
const router = useRouter();
const { notifications } = storeToRefs(useUserStore()); const { notifications } = storeToRefs(useUserStore());
const api = useApiStore(); const api = useApiStore();
const currentTab = ref("all"); const currentTab = ref("all");
@ -119,6 +122,25 @@ function readAll() {
console.log("Could not read all notifications : ", error); console.log("Could not read all notifications : ", error);
} }
} }
// Functions
function getHref(notification) {
const uri = notification.location.page.uri;
const isDocumentBrief =
notification.location.page.template === "client-brief" &&
notification.location?.file.type === "document";
if (isDocumentBrief) {
return notification.project.uri + "?dialog=client-brief&comments=true";
}
if (notification.location.page.template === "track") {
return notification.project.uri + "?dialog=virtual-sample&comments=true";
}
return uri;
}
</script> </script>
<style scoped> <style scoped>