designtopack/src/components/comments/Comments.vue

127 lines
3.2 KiB
Vue
Raw Normal View History

2024-10-23 09:48:27 +02:00
<template>
<aside id="comments-container" aria-labelledby="comments-label">
<h2 id="comments-label" class="sr-only">Commentaires</h2>
<div class="comments | flow">
2024-10-23 11:32:51 +02:00
<div
v-for="(page, pageIndex) in localComments"
class="comments__page-group"
>
<article
v-for="(comment, commentIndex) in Object.values(page)"
:key="pageIndex + commentIndex"
class="comment | flow"
data-status="unread"
>
<header>
<p>
<strong>{{ comment.username }}</strong>
<span class="comment__id">#{{ commentIndex + 1 }}</span>
<span class="comment__page">Page {{ pageIndex }}</span>
<time :datetime="dayjs(comment.date).format('YYYY-MM-DD')">{{
formatDate(comment.date)
}}</time>
</p>
</header>
<p class="comment__body">
{{ comment.text }}
2024-10-23 09:48:27 +02:00
</p>
2024-10-23 11:32:51 +02:00
</article>
</div>
2024-10-23 09:48:27 +02:00
</div>
<button
id="add-comment"
class="btn btn--white-20 | w-full"
@click="isAddOpen = true"
>
Ajouter un commentaire
</button>
<form
v-if="isAddOpen"
action=""
method="post"
class="flow"
@submit="addComment"
>
<label class="sr-only" for="comment">Votre commentaire</label>
<textarea
name="comment"
id="comment"
placeholder="Ajouter un commentaire"
rows="3"
class="text-sm | rounded-lg bg-black p-12"
v-model="newCommentText"
></textarea>
<footer class="flex">
<button class="btn btn--white-20">Annuler</button>
<input type="submit" class="btn btn--tranparent" />
</footer>
</form>
</aside>
</template>
<script setup>
2024-10-23 11:32:51 +02:00
import dayjs from "dayjs";
import "dayjs/locale/fr";
import uniqid from "uniqid";
2024-10-23 09:48:27 +02:00
import { ref } from "vue";
import { useUserStore } from "../../stores/user";
2024-10-23 11:32:51 +02:00
import { usePageStore } from "../../stores/page";
dayjs.locale("fr");
2024-10-23 09:48:27 +02:00
2024-10-23 11:32:51 +02:00
const { currentPageIndex, file, comments } = defineProps({
currentPageIndex: Number,
file: Object,
comments: Object,
2024-10-23 09:48:27 +02:00
});
const { user } = useUserStore();
2024-10-23 11:32:51 +02:00
const { page } = usePageStore();
2024-10-23 09:48:27 +02:00
const newCommentText = ref("");
const isAddOpen = ref(false);
2024-10-23 11:32:51 +02:00
const localComments = ref(comments);
2024-10-23 09:48:27 +02:00
// Functions
function addComment(event) {
event.preventDefault();
2024-10-23 11:32:51 +02:00
const date = dayjs().format();
2024-10-23 09:48:27 +02:00
const comment = {
2024-10-23 11:32:51 +02:00
pageUri: page.uri + "/client-brief",
targetPage: currentPageIndex,
fileName: file.name,
2024-10-23 09:48:27 +02:00
userUuid: user.uuid,
text: newCommentText.value,
2024-10-23 11:32:51 +02:00
date,
id: uniqid(),
};
const headers = {
method: "POST",
body: JSON.stringify(comment),
2024-10-23 09:48:27 +02:00
};
2024-10-23 11:32:51 +02:00
fetch("/add-comment.json", headers)
.then((res) => res.json())
.then((json) => (localComments.value = json))
.catch((error) => {
console.error("Erreur lors de la sauvegarde :", error);
});
}
function formatDate(date) {
const todayNumber = parseInt(dayjs().format("YYMMD"));
const dateNumber = parseInt(dayjs(date).format("YYMMD"));
if (dateNumber === todayNumber) {
return "Aujourd'hui";
}
if (dateNumber === todayNumber - 1) {
return "hier";
}
return dayjs(date).format("D MMM YY");
2024-10-23 09:48:27 +02:00
}
</script>