#88 - edit comment fonctional but not styled

This commit is contained in:
isUnknown 2025-01-07 15:19:50 +01:00
parent ea2817244b
commit 6cc8325d2b
5 changed files with 102 additions and 23 deletions

View file

@ -4,25 +4,42 @@ return [
'pattern' => '(:all)update-comment.json', 'pattern' => '(:all)update-comment.json',
'method' => 'POST', 'method' => 'POST',
'action' => function () { 'action' => function () {
$json = file_get_contents('php://input'); $json = file_get_contents('php://input');
$data = json_decode($json); $data = json_decode($json);
$page = page($data->pageUri); $page = page($data->location->page->uri);
$file = $page->file($data->fileName); $project = page($data->location->project->uri);
$user = kirby()->user($data->userUuid); $file = $page->file($data->location->file->uuid);
$isReply = $data->location->parentId ?? false;
$comments = $file->comments()->isEmpty() == true ? [] : Yaml::decode($file->comments()->value());
$comments = $file->comments()->isEmpty() == true ? [] : Yaml::decode($file->comments()->value()); foreach ($comments as $key => &$comment) {
if ($isReply) {
if ($comment['id'] === $data->location->parentId) {
foreach ($comment['replies'] as $replyKey => $reply) {
if ($reply['id'] === $data->id) {
$comment['replies'][$replyKey]["text"] = $data->text;
$comment['replies'][$replyKey]["date"] = $data->date;
$comment['replies'] = array_values($comment['replies']);
}
}
}
} else {
if ($comment['id'] === $data->id) {
$comments[$key]["text"] = $data->text;
$comments[$key]["date"] = $data->date;
}
}
}
$comments[$data->id]['text'] = $data->text; $comments = array_values($comments);
$comments[$data->id]['date'] = $data->date;
$newFile = $file->update([
'comments' => $comments
]);
$user->sendNotification('comments', $comments[$data->id]); $newFile = $file->update([
'comments' => $comments
return getFileData($newFile); ]);
echo json_encode(getFileData($newFile));
exit;
} }
]; ];

View file

@ -20,10 +20,14 @@
> >
</p> </p>
</header> </header>
<p class="comment__body">
<p v-if="!isEditCommentMode" class="comment__body">
{{ comment.text }} {{ comment.text }}
</p> </p>
<footer class="comment__replies">
<textarea v-else class="comment__body" v-model="draftText"></textarea>
<footer v-if="!isEditCommentMode" class="comment__replies">
<p v-if="comment.replies?.length > 0"> <p v-if="comment.replies?.length > 0">
{{ comment.replies.length }} réponse{{ {{ comment.replies.length }} réponse{{
comment.replies.length > 1 ? "s" : "" comment.replies.length > 1 ? "s" : ""
@ -33,7 +37,11 @@
v-if="userStore.canEditComment(comment)" v-if="userStore.canEditComment(comment)"
class="comment__ctas | mt-8" class="comment__ctas | mt-8"
> >
<button class="btn btn--transparent btn--icon btn--sm" data-icon="edit"> <button
class="btn btn--transparent btn--icon btn--sm"
data-icon="edit"
@click="isEditCommentMode = true"
>
<span class="sr-only">Éditer</span> <span class="sr-only">Éditer</span>
</button> </button>
<button <button
@ -45,6 +53,18 @@
</button> </button>
</div> </div>
</footer> </footer>
<footer v-else class="flex">
<input
type="submit"
class="btn btn--tranparent"
value="Sauvegarder"
@click="saveEditedComment($event)"
/>
<button class="btn btn--white-10" @click="cancelEditComment($event)">
Annuler
</button>
</footer>
</article> </article>
</template> </template>
@ -54,7 +74,7 @@ import "dayjs/locale/fr";
import { useUserStore } from "../../stores/user"; import { useUserStore } from "../../stores/user";
import { useApiStore } from "../../stores/api"; import { useApiStore } from "../../stores/api";
import { useDialogStore } from "../../stores/dialog"; import { useDialogStore } from "../../stores/dialog";
import { computed } from "vue"; import { computed, ref } from "vue";
import { storeToRefs } from "pinia"; import { storeToRefs } from "pinia";
dayjs.locale("fr"); dayjs.locale("fr");
@ -69,7 +89,8 @@ const emits = defineEmits(["update:file", "close:comment"]);
const userStore = useUserStore(); const userStore = useUserStore();
const api = useApiStore(); const api = useApiStore();
const dialog = useDialogStore(); const dialog = useDialogStore();
const { activeTracks } = storeToRefs(useDialogStore()); const { activeTracks, isEditCommentMode } = storeToRefs(useDialogStore());
const draftText = ref(comment.text);
// Functions // Functions
const getStatus = computed(() => { const getStatus = computed(() => {
@ -132,6 +153,20 @@ async function deleteComment(event) {
emits("close:comment"); emits("close:comment");
} }
} }
function saveEditedComment(event) {
event.stopImmediatePropagation();
comment.text = draftText.value;
comment.date = dayjs().format();
api.updateComment(comment);
isEditCommentMode.value = false;
}
function cancelEditComment(event) {
event.stopImmediatePropagation();
isEditCommentMode.value = false;
draftText.value = comment.text;
}
</script> </script>
<style scoped> <style scoped>

View file

@ -121,9 +121,8 @@ dayjs.locale("fr");
const { user } = useUserStore(); const { user } = useUserStore();
const { page } = usePageStore(); const { page } = usePageStore();
const dialog = useDialogStore(); const dialog = useDialogStore();
const { comments, openedFile, draftComment, activeTracks } = storeToRefs( const { comments, openedFile, draftComment, activeTracks, isEditCommentMode } =
useDialogStore() storeToRefs(useDialogStore());
);
const api = useApiStore(); const api = useApiStore();
const openedComment = ref(null); const openedComment = ref(null);
@ -305,6 +304,8 @@ function prepareDraftCommentInImage(event) {
} }
function openComment(comment) { function openComment(comment) {
if (isEditCommentMode.value) return;
openedComment.value = comment; openedComment.value = comment;
if (activeTracks?.value.length === 1) { if (activeTracks?.value.length === 1) {

View file

@ -143,6 +143,29 @@ export const useApiStore = defineStore("api", () => {
} }
} }
async function updateComment(comment) {
const headers = {
method: "POST",
body: JSON.stringify(comment),
};
try {
const response = await fetch("/update-comment.json", headers);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const newFile = await response.json();
return newFile;
} catch (error) {
console.error(
"Une erreur s'est produite lors de la mise à jour du commentaire :",
comment,
error
);
throw error;
}
}
async function deleteComment(comment) { async function deleteComment(comment) {
const headers = { const headers = {
method: "POST", method: "POST",
@ -259,6 +282,7 @@ export const useApiStore = defineStore("api", () => {
fetchData, fetchData,
fetchRoute, fetchRoute,
addComment, addComment,
updateComment,
deleteComment, deleteComment,
replyComment, replyComment,
readNotification, readNotification,

View file

@ -6,6 +6,7 @@ export const useDialogStore = defineStore("dialog", () => {
const content = ref(null); const content = ref(null);
const openedFile = ref(null); const openedFile = ref(null);
const activeTracks = ref(null); const activeTracks = ref(null);
const isEditCommentMode = ref(false);
const comments = computed(() => { const comments = computed(() => {
if (activeTracks.value?.length > 0) { if (activeTracks.value?.length > 0) {
@ -143,5 +144,6 @@ export const useDialogStore = defineStore("dialog", () => {
isCommentsOpen, isCommentsOpen,
isCommentPanelEnabled, isCommentPanelEnabled,
updateFile, updateFile,
isEditCommentMode,
}; };
}); });