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:
|
||||
|
||||
1:
|
||||
m2t7307h:
|
||||
fileUuid: file://s0lNtRA0Z7ybTCWG
|
||||
page: 1
|
||||
text: Un commentaire
|
||||
username: Adrien Payet
|
||||
date: 2024-10-28T16:49:51+01:00
|
||||
id: m2t7307h
|
||||
m2t76m4t:
|
||||
fileUuid: file://s0lNtRA0Z7ybTCWG
|
||||
page: 1
|
||||
text: Autre commentaire
|
||||
username: Utilisateur Dior
|
||||
date: 2024-10-28T16:52:39+01:00
|
||||
id: m2t76m4t
|
||||
m2t7pg5m:
|
||||
fileUuid: file://s0lNtRA0Z7ybTCWG
|
||||
page: 1
|
||||
text: Nouveau commentaire
|
||||
username: Adrien Payet
|
||||
date: 2024-10-28T17:07:18+01:00
|
||||
id: m2t7pg5m
|
||||
m2t82m8c:
|
||||
fileUuid: file://s0lNtRA0Z7ybTCWG
|
||||
page: 1
|
||||
text: test
|
||||
username: Adrien Payet
|
||||
date: 2024-10-28T17:17:32+01:00
|
||||
id: m2t82m8c
|
||||
m2t83fp2:
|
||||
fileUuid: file://s0lNtRA0Z7ybTCWG
|
||||
page: 1
|
||||
text: tt
|
||||
username: Adrien Payet
|
||||
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
|
||||
m2u905al:
|
||||
pageUri: >
|
||||
projects/miss-dior-blooming-bouquet/client-brief
|
||||
fileUuid: file://s0lNtRA0Z7ybTCWG
|
||||
filePageTarget: 1
|
||||
position:
|
||||
x: null
|
||||
y: null
|
||||
replies: [ ]
|
||||
text: com1
|
||||
user:
|
||||
name: Adrien Payet
|
||||
email: adrien.payet@outlook.com
|
||||
uuid: user://WWjXgPWk
|
||||
role: admin
|
||||
date: 2024-10-29T10:31:23+01:00
|
||||
id: m2u905al
|
||||
m2u955a7:
|
||||
pageUri: >
|
||||
projects/miss-dior-blooming-bouquet/client-brief
|
||||
fileUuid: file://s0lNtRA0Z7ybTCWG
|
||||
filePageTarget: 1
|
||||
position:
|
||||
x: null
|
||||
y: null
|
||||
replies: [ ]
|
||||
text: come2
|
||||
user:
|
||||
name: Adrien Payet
|
||||
email: adrien.payet@outlook.com
|
||||
uuid: user://WWjXgPWk
|
||||
role: admin
|
||||
date: 2024-10-29T10:35:16+01:00
|
||||
id: m2u955a7
|
||||
|
|
@ -26,7 +26,6 @@ return [
|
|||
require(__DIR__ . '/routes/save-file.php'),
|
||||
require(__DIR__ . '/routes/remove-file.php'),
|
||||
require(__DIR__ . '/routes/upload-pdf.php'),
|
||||
require(__DIR__ . '/routes/add-comment.php'),
|
||||
require(__DIR__ . '/routes/read-notification.php'),
|
||||
],
|
||||
'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
|
||||
|
||||
return [
|
||||
'pattern' => '(:all)add-comment.json',
|
||||
'pattern' => '(:all)create-comment.json',
|
||||
'method' => 'POST',
|
||||
'action' => function () {
|
||||
$json = file_get_contents('php://input');
|
||||
|
|
@ -15,15 +15,26 @@ return [
|
|||
$comments = $file->comments()->isEmpty() == true ? [] : Yaml::decode($file->comments()->value());
|
||||
|
||||
$newComment = [
|
||||
'pageUri' => $data->pageUri,
|
||||
'fileUuid' => (string) $file->uuid(),
|
||||
'page' => $data->targetPage,
|
||||
'filePageTarget' => $data->targetPage,
|
||||
'position' => [
|
||||
'x' => null,
|
||||
'y' => null
|
||||
],
|
||||
'replies' => [],
|
||||
'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,
|
||||
'id' => $data->id,
|
||||
];
|
||||
|
||||
$comments[$data->targetPage][$data->id] = $newComment;
|
||||
$comments[$data->id] = $newComment;
|
||||
|
||||
$newFile = $file->update([
|
||||
'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">
|
||||
<h2 id="comments-label" class="sr-only">Commentaires</h2>
|
||||
<div class="comments | flow">
|
||||
<div v-for="(page, pageIndex) in comments" class="comments__page-group">
|
||||
<article
|
||||
v-for="(comment, commentIndex) in Object.values(page)"
|
||||
:key="pageIndex + commentIndex"
|
||||
class="comment | flow"
|
||||
:data-status="setStatus(comment)"
|
||||
@click="readNotification(comment)"
|
||||
>
|
||||
<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 }}
|
||||
<article
|
||||
v-for="(comment, commentIndex) in Object.values(comments).reverse()"
|
||||
:key="commentIndex"
|
||||
class="comment | flow"
|
||||
:data-status="setStatus(comment)"
|
||||
@click="open(comment)"
|
||||
>
|
||||
<header>
|
||||
<p>
|
||||
<strong>{{ comment.user.name ?? comment.user.email }}</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>
|
||||
</article>
|
||||
</div>
|
||||
</header>
|
||||
<p class="comment__body">
|
||||
{{ comment.text }}
|
||||
</p>
|
||||
</article>
|
||||
</div>
|
||||
<button
|
||||
id="add-comment"
|
||||
id="create-comment"
|
||||
class="btn btn--white-20 | w-full"
|
||||
@click="isAddOpen = true"
|
||||
>
|
||||
|
|
@ -100,6 +98,7 @@ async function addComment(event) {
|
|||
const newFile = await api.addComment(comment);
|
||||
newCommentText.value = "";
|
||||
isAddOpen.value = false;
|
||||
emits("update:file", newFile);
|
||||
}
|
||||
|
||||
function formatDate(date) {
|
||||
|
|
|
|||
|
|
@ -128,7 +128,7 @@ export const useApiStore = defineStore("api", () => {
|
|||
};
|
||||
|
||||
try {
|
||||
const response = await fetch("/add-comment.json", headers);
|
||||
const response = await fetch("/create-comment.json", headers);
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue