add / readNotification working

This commit is contained in:
isUnknown 2024-10-28 17:50:40 +01:00
parent 722c6b198e
commit ed73b33234
6 changed files with 159 additions and 20 deletions

View file

@ -8,6 +8,7 @@
:key="pageIndex + commentIndex"
class="comment | flow"
:data-status="setStatus(comment)"
@click="readNotification(comment)"
>
<header>
<p>
@ -66,6 +67,7 @@ import uniqid from "uniqid";
import { ref } from "vue";
import { useUserStore } from "../../stores/user";
import { usePageStore } from "../../stores/page";
import { useApiStore } from "../../stores/api";
dayjs.locale("fr");
@ -76,13 +78,14 @@ const { currentPageIndex, file, comments } = defineProps({
});
const { user } = useUserStore();
const { page } = usePageStore();
const api = useApiStore();
const newCommentText = ref("");
const isAddOpen = ref(false);
const emits = defineEmits(["update:file"]);
// Functions
function addComment(event) {
async function addComment(event) {
event.preventDefault();
const date = dayjs().format();
const comment = {
@ -94,22 +97,9 @@ function addComment(event) {
date,
id: uniqid(),
};
const headers = {
method: "POST",
body: JSON.stringify(comment),
};
fetch("/add-comment.json", headers)
.then((res) => res.json())
.then((newFile) => {
newCommentText.value = "";
isAddOpen.value = false;
emits("update:file", newFile);
})
.catch((error) => {
console.error("Erreur lors de la sauvegarde :", error);
});
const newFile = await api.addComment(comment);
newCommentText.value = "";
isAddOpen.value = false;
}
function formatDate(date) {
@ -134,7 +124,7 @@ function closeAddField() {
function setStatus(comment) {
try {
if (user?.notifications?.comments.hasOwnProperty(comment.id)) {
if (!user?.notifications?.comments[comment.id].isRead) {
return "unread";
} else {
return undefined;
@ -143,4 +133,14 @@ function setStatus(comment) {
return undefined;
}
}
async function readNotification(notificationId) {
const newNotifications = await api.readNotification(
user.uuid,
"comments",
notificationId
);
user.notifications = newNotifications;
}
</script>