refactor comment system into comments plugin
This commit is contained in:
parent
31844269c0
commit
fb36329556
8 changed files with 134 additions and 101 deletions
|
|
@ -12,75 +12,37 @@ Template: document
|
||||||
|
|
||||||
Comments:
|
Comments:
|
||||||
|
|
||||||
1:
|
m2u905al:
|
||||||
m2t7307h:
|
pageUri: >
|
||||||
fileUuid: file://s0lNtRA0Z7ybTCWG
|
projects/miss-dior-blooming-bouquet/client-brief
|
||||||
page: 1
|
fileUuid: file://s0lNtRA0Z7ybTCWG
|
||||||
text: Un commentaire
|
filePageTarget: 1
|
||||||
username: Adrien Payet
|
position:
|
||||||
date: 2024-10-28T16:49:51+01:00
|
x: null
|
||||||
id: m2t7307h
|
y: null
|
||||||
m2t76m4t:
|
replies: [ ]
|
||||||
fileUuid: file://s0lNtRA0Z7ybTCWG
|
text: com1
|
||||||
page: 1
|
user:
|
||||||
text: Autre commentaire
|
name: Adrien Payet
|
||||||
username: Utilisateur Dior
|
email: adrien.payet@outlook.com
|
||||||
date: 2024-10-28T16:52:39+01:00
|
uuid: user://WWjXgPWk
|
||||||
id: m2t76m4t
|
role: admin
|
||||||
m2t7pg5m:
|
date: 2024-10-29T10:31:23+01:00
|
||||||
fileUuid: file://s0lNtRA0Z7ybTCWG
|
id: m2u905al
|
||||||
page: 1
|
m2u955a7:
|
||||||
text: Nouveau commentaire
|
pageUri: >
|
||||||
username: Adrien Payet
|
projects/miss-dior-blooming-bouquet/client-brief
|
||||||
date: 2024-10-28T17:07:18+01:00
|
fileUuid: file://s0lNtRA0Z7ybTCWG
|
||||||
id: m2t7pg5m
|
filePageTarget: 1
|
||||||
m2t82m8c:
|
position:
|
||||||
fileUuid: file://s0lNtRA0Z7ybTCWG
|
x: null
|
||||||
page: 1
|
y: null
|
||||||
text: test
|
replies: [ ]
|
||||||
username: Adrien Payet
|
text: come2
|
||||||
date: 2024-10-28T17:17:32+01:00
|
user:
|
||||||
id: m2t82m8c
|
name: Adrien Payet
|
||||||
m2t83fp2:
|
email: adrien.payet@outlook.com
|
||||||
fileUuid: file://s0lNtRA0Z7ybTCWG
|
uuid: user://WWjXgPWk
|
||||||
page: 1
|
role: admin
|
||||||
text: tt
|
date: 2024-10-29T10:35:16+01:00
|
||||||
username: Adrien Payet
|
id: m2u955a7
|
||||||
date: 2024-10-28T17:18:11+01:00
|
|
||||||
id: m2t83fp2
|
|
||||||
m2t83syr:
|
|
||||||
fileUuid: file://s0lNtRA0Z7ybTCWG
|
|
||||||
page: 1
|
|
||||||
text: test
|
|
||||||
username: Adrien Payet
|
|
||||||
date: 2024-10-28T17:18:28+01:00
|
|
||||||
id: m2t83syr
|
|
||||||
m2t85i0w:
|
|
||||||
fileUuid: file://s0lNtRA0Z7ybTCWG
|
|
||||||
page: 1
|
|
||||||
text: test
|
|
||||||
username: Adrien Payet
|
|
||||||
date: 2024-10-28T17:19:47+01:00
|
|
||||||
id: m2t85i0w
|
|
||||||
m2t8ayc5:
|
|
||||||
fileUuid: file://s0lNtRA0Z7ybTCWG
|
|
||||||
page: 1
|
|
||||||
text: test
|
|
||||||
username: Adrien Payet
|
|
||||||
date: 2024-10-28T17:24:01+01:00
|
|
||||||
id: m2t8ayc5
|
|
||||||
m2t9dyjc:
|
|
||||||
fileUuid: file://s0lNtRA0Z7ybTCWG
|
|
||||||
page: 1
|
|
||||||
text: test 89
|
|
||||||
username: Utilisateur Dior
|
|
||||||
date: 2024-10-28T17:54:21+01:00
|
|
||||||
id: m2t9dyjc
|
|
||||||
2:
|
|
||||||
m2t8bv59:
|
|
||||||
fileUuid: file://s0lNtRA0Z7ybTCWG
|
|
||||||
page: 2
|
|
||||||
text: test 40
|
|
||||||
username: Adrien Payet
|
|
||||||
date: 2024-10-28T17:24:44+01:00
|
|
||||||
id: m2t8bv59
|
|
||||||
|
|
@ -26,7 +26,6 @@ return [
|
||||||
require(__DIR__ . '/routes/save-file.php'),
|
require(__DIR__ . '/routes/save-file.php'),
|
||||||
require(__DIR__ . '/routes/remove-file.php'),
|
require(__DIR__ . '/routes/remove-file.php'),
|
||||||
require(__DIR__ . '/routes/upload-pdf.php'),
|
require(__DIR__ . '/routes/upload-pdf.php'),
|
||||||
require(__DIR__ . '/routes/add-comment.php'),
|
|
||||||
require(__DIR__ . '/routes/read-notification.php'),
|
require(__DIR__ . '/routes/read-notification.php'),
|
||||||
],
|
],
|
||||||
'hooks' => [
|
'hooks' => [
|
||||||
|
|
|
||||||
9
public/site/plugins/comments/index.php
Normal file
9
public/site/plugins/comments/index.php
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
Kirby::plugin('adrienpayet/pdc-comments', [
|
||||||
|
'routes' => [
|
||||||
|
require(__DIR__ . '/routes/create.php'),
|
||||||
|
require(__DIR__ . '/routes/update.php'),
|
||||||
|
require(__DIR__ . '/routes/delete.php'),
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'pattern' => '(:all)add-comment.json',
|
'pattern' => '(:all)create-comment.json',
|
||||||
'method' => 'POST',
|
'method' => 'POST',
|
||||||
'action' => function () {
|
'action' => function () {
|
||||||
$json = file_get_contents('php://input');
|
$json = file_get_contents('php://input');
|
||||||
|
|
@ -15,15 +15,26 @@ return [
|
||||||
$comments = $file->comments()->isEmpty() == true ? [] : Yaml::decode($file->comments()->value());
|
$comments = $file->comments()->isEmpty() == true ? [] : Yaml::decode($file->comments()->value());
|
||||||
|
|
||||||
$newComment = [
|
$newComment = [
|
||||||
|
'pageUri' => $data->pageUri,
|
||||||
'fileUuid' => (string) $file->uuid(),
|
'fileUuid' => (string) $file->uuid(),
|
||||||
'page' => $data->targetPage,
|
'filePageTarget' => $data->targetPage,
|
||||||
|
'position' => [
|
||||||
|
'x' => null,
|
||||||
|
'y' => null
|
||||||
|
],
|
||||||
|
'replies' => [],
|
||||||
'text' => $data->text,
|
'text' => $data->text,
|
||||||
'username' => $user->name()->isNotEmpty() ? (string) $user->name() : (string) $user->email(),
|
'user' => [
|
||||||
|
'name' => (string) $user->name(),
|
||||||
|
'email' => (string) $user->email(),
|
||||||
|
'uuid' => (string) $user->uuid(),
|
||||||
|
'role' => (string) $user->role()
|
||||||
|
],
|
||||||
'date' => (string) $data->date,
|
'date' => (string) $data->date,
|
||||||
'id' => $data->id,
|
'id' => $data->id,
|
||||||
];
|
];
|
||||||
|
|
||||||
$comments[$data->targetPage][$data->id] = $newComment;
|
$comments[$data->id] = $newComment;
|
||||||
|
|
||||||
$newFile = $file->update([
|
$newFile = $file->update([
|
||||||
'comments' => $comments
|
'comments' => $comments
|
||||||
25
public/site/plugins/comments/routes/delete.php
Normal file
25
public/site/plugins/comments/routes/delete.php
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
return [
|
||||||
|
'pattern' => '(:all)delete-comment.json',
|
||||||
|
'method' => 'POST',
|
||||||
|
'action' => function () {
|
||||||
|
$json = file_get_contents('php://input');
|
||||||
|
$data = json_decode($json);
|
||||||
|
|
||||||
|
$page = page($data->pageUri);
|
||||||
|
$file = $page->file($data->fileName);
|
||||||
|
$user = kirby()->user($data->userUuid);
|
||||||
|
|
||||||
|
|
||||||
|
$comments = $file->comments()->isEmpty() == true ? [] : Yaml::decode($file->comments()->value());
|
||||||
|
|
||||||
|
unset($comments[$data->id] );
|
||||||
|
|
||||||
|
$newFile = $file->update([
|
||||||
|
'comments' => $comments
|
||||||
|
]);
|
||||||
|
|
||||||
|
return getFileData($newFile);
|
||||||
|
}
|
||||||
|
];
|
||||||
28
public/site/plugins/comments/routes/update.php
Normal file
28
public/site/plugins/comments/routes/update.php
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
return [
|
||||||
|
'pattern' => '(:all)create-comment.json',
|
||||||
|
'method' => 'POST',
|
||||||
|
'action' => function () {
|
||||||
|
$json = file_get_contents('php://input');
|
||||||
|
$data = json_decode($json);
|
||||||
|
|
||||||
|
$page = page($data->pageUri);
|
||||||
|
$file = $page->file($data->fileName);
|
||||||
|
$user = kirby()->user($data->userUuid);
|
||||||
|
|
||||||
|
|
||||||
|
$comments = $file->comments()->isEmpty() == true ? [] : Yaml::decode($file->comments()->value());
|
||||||
|
|
||||||
|
$comments[$data->id]['text'] = $data->text;
|
||||||
|
$comments[$data->id]['date'] = $data->date;
|
||||||
|
|
||||||
|
$newFile = $file->update([
|
||||||
|
'comments' => $comments
|
||||||
|
]);
|
||||||
|
|
||||||
|
// $user->sendNotification('comments', $comments[$data->id]);
|
||||||
|
|
||||||
|
return getFileData($newFile);
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
@ -2,32 +2,30 @@
|
||||||
<aside id="comments-container" aria-labelledby="comments-label">
|
<aside id="comments-container" aria-labelledby="comments-label">
|
||||||
<h2 id="comments-label" class="sr-only">Commentaires</h2>
|
<h2 id="comments-label" class="sr-only">Commentaires</h2>
|
||||||
<div class="comments | flow">
|
<div class="comments | flow">
|
||||||
<div v-for="(page, pageIndex) in comments" class="comments__page-group">
|
<article
|
||||||
<article
|
v-for="(comment, commentIndex) in Object.values(comments).reverse()"
|
||||||
v-for="(comment, commentIndex) in Object.values(page)"
|
:key="commentIndex"
|
||||||
:key="pageIndex + commentIndex"
|
class="comment | flow"
|
||||||
class="comment | flow"
|
:data-status="setStatus(comment)"
|
||||||
:data-status="setStatus(comment)"
|
@click="open(comment)"
|
||||||
@click="readNotification(comment)"
|
>
|
||||||
>
|
<header>
|
||||||
<header>
|
<p>
|
||||||
<p>
|
<strong>{{ comment.user.name ?? comment.user.email }}</strong>
|
||||||
<strong>{{ comment.username }}</strong>
|
<span class="comment__id">#{{ commentIndex + 1 }}</span> •
|
||||||
<span class="comment__id">#{{ commentIndex + 1 }}</span> •
|
<span class="comment__page">Page {{ pageIndex }}</span>
|
||||||
<span class="comment__page">Page {{ pageIndex }}</span>
|
<time :datetime="dayjs(comment.date).format('YYYY-MM-DD')">{{
|
||||||
<time :datetime="dayjs(comment.date).format('YYYY-MM-DD')">{{
|
formatDate(comment.date)
|
||||||
formatDate(comment.date)
|
}}</time>
|
||||||
}}</time>
|
|
||||||
</p>
|
|
||||||
</header>
|
|
||||||
<p class="comment__body">
|
|
||||||
{{ comment.text }}
|
|
||||||
</p>
|
</p>
|
||||||
</article>
|
</header>
|
||||||
</div>
|
<p class="comment__body">
|
||||||
|
{{ comment.text }}
|
||||||
|
</p>
|
||||||
|
</article>
|
||||||
</div>
|
</div>
|
||||||
<button
|
<button
|
||||||
id="add-comment"
|
id="create-comment"
|
||||||
class="btn btn--white-20 | w-full"
|
class="btn btn--white-20 | w-full"
|
||||||
@click="isAddOpen = true"
|
@click="isAddOpen = true"
|
||||||
>
|
>
|
||||||
|
|
@ -100,6 +98,7 @@ async function addComment(event) {
|
||||||
const newFile = await api.addComment(comment);
|
const newFile = await api.addComment(comment);
|
||||||
newCommentText.value = "";
|
newCommentText.value = "";
|
||||||
isAddOpen.value = false;
|
isAddOpen.value = false;
|
||||||
|
emits("update:file", newFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatDate(date) {
|
function formatDate(date) {
|
||||||
|
|
|
||||||
|
|
@ -128,7 +128,7 @@ export const useApiStore = defineStore("api", () => {
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch("/add-comment.json", headers);
|
const response = await fetch("/create-comment.json", headers);
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(`HTTP error! status: ${response.status}`);
|
throw new Error(`HTTP error! status: ${response.status}`);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue