read comment working
This commit is contained in:
parent
583daa1759
commit
39fc379541
12 changed files with 125 additions and 78 deletions
|
|
@ -12,5 +12,6 @@ Kirby::plugin('adrienpayet/kirby4-comments', [
|
||||||
require(__DIR__ . '/routes/update.php'),
|
require(__DIR__ . '/routes/update.php'),
|
||||||
require(__DIR__ . '/routes/delete.php'),
|
require(__DIR__ . '/routes/delete.php'),
|
||||||
require(__DIR__ . '/routes/reply.php'),
|
require(__DIR__ . '/routes/reply.php'),
|
||||||
|
require(__DIR__ . '/routes/read.php'),
|
||||||
]
|
]
|
||||||
]);
|
]);
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,7 @@ return [
|
||||||
|
|
||||||
echo json_encode(getFileData($newFile));
|
echo json_encode(getFileData($newFile));
|
||||||
|
|
||||||
kirby()->user()->deleteNotification($page, $data->id);
|
kirby()->user()->deleteNotification($page->managers()->toUsers(), $data->id);
|
||||||
|
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
28
public/site/plugins/comments/routes/read.php
Normal file
28
public/site/plugins/comments/routes/read.php
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
return [
|
||||||
|
'pattern' => '(:all)read-comment.json',
|
||||||
|
'method' => 'POST',
|
||||||
|
'action' => function () {
|
||||||
|
$json = file_get_contents('php://input');
|
||||||
|
$data = json_decode($json);
|
||||||
|
|
||||||
|
try {
|
||||||
|
$newNotification = kirby()->user()->readNotification($data->notificationId);
|
||||||
|
if ($newNotification) {
|
||||||
|
return [
|
||||||
|
"success" => "Notification read.",
|
||||||
|
"data" => $newNotification
|
||||||
|
];
|
||||||
|
} else {
|
||||||
|
throw new Exception("Can't find corresponding notification.", 1);
|
||||||
|
}
|
||||||
|
} catch (\Throwable $th) {
|
||||||
|
return [
|
||||||
|
"error" => $th->getMessage(),
|
||||||
|
"notificationId" => $data->notificationId,
|
||||||
|
"userName" => kirby()->user()->name()->value()
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
@ -43,6 +43,7 @@ class BaseComment {
|
||||||
$this->date = $date;
|
$this->date = $date;
|
||||||
$this->id = $id;
|
$this->id = $id;
|
||||||
$this->type = $type;
|
$this->type = $type;
|
||||||
|
$this->isRead = false;
|
||||||
$this->position = $position;
|
$this->position = $position;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -78,6 +79,20 @@ class BaseComment {
|
||||||
return $this->type;
|
return $this->type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function isRead() {
|
||||||
|
return $this->isRead;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function read() {
|
||||||
|
$this->isRead = true;
|
||||||
|
return $this->isRead;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function unread() {
|
||||||
|
$this->isRead = false;
|
||||||
|
return $this->isRead;
|
||||||
|
}
|
||||||
|
|
||||||
public function pageIndex() {
|
public function pageIndex() {
|
||||||
$this->position['pageIndex'];
|
$this->position['pageIndex'];
|
||||||
}
|
}
|
||||||
|
|
@ -93,6 +108,7 @@ class BaseComment {
|
||||||
'date' => $this->date,
|
'date' => $this->date,
|
||||||
'id' => $this->id,
|
'id' => $this->id,
|
||||||
'type' => $this->type,
|
'type' => $this->type,
|
||||||
|
'isRead' => $this->isRead
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,9 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
Kirby::plugin('adrienpayet/pdc-notifications', [
|
Kirby::plugin('adrienpayet/pdc-notifications', [
|
||||||
// 'routes' => [
|
|
||||||
// require(__DIR__ . '/routes/mark-as-read.php'),
|
|
||||||
// ],
|
|
||||||
'userMethods' => [
|
'userMethods' => [
|
||||||
'sendNotification' => require(__DIR__ . '/user-methods/send.php'),
|
'sendNotification' => require(__DIR__ . '/user-methods/send.php'),
|
||||||
'deleteNotification' => require(__DIR__ . '/user-methods/delete.php')
|
'deleteNotification' => require(__DIR__ . '/user-methods/delete.php'),
|
||||||
|
'readNotification' => require(__DIR__ . '/user-methods/read.php')
|
||||||
]
|
]
|
||||||
]);
|
]);
|
||||||
|
|
|
||||||
|
|
@ -1,25 +1,22 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
return function($project, $notificationId) {
|
return function($notificationId) {
|
||||||
$projectManagers = $project->managers()->toUsers();
|
$user = kirby()->user();
|
||||||
|
try {
|
||||||
|
$notifications = $user->notifications()->isNotEmpty()
|
||||||
|
? Yaml::decode($user->notifications()->value())
|
||||||
|
: [];
|
||||||
|
|
||||||
foreach ($projectManagers as $user) {
|
foreach ($notifications as $key => $notification) {
|
||||||
try {
|
if ($notification['id'] === $notificationId) {
|
||||||
$notifications = $user->notifications()->isNotEmpty()
|
unset($notifications[$key]);
|
||||||
? Yaml::decode($user->notifications()->value())
|
|
||||||
: [];
|
|
||||||
|
|
||||||
foreach ($notifications as $key => $notification) {
|
|
||||||
if ($notification['id'] === $notificationId) {
|
|
||||||
unset($notifications[$key]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$user->update([
|
|
||||||
'notifications' => Yaml::encode(array_values($notifications))
|
|
||||||
]);
|
|
||||||
} catch (\Throwable $th) {
|
|
||||||
throw new Exception("Error updating notifications: " . $th->getMessage() . ' line ' . $th->getLine(), 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$user->update([
|
||||||
|
'notifications' => Yaml::encode(array_values($notifications))
|
||||||
|
]);
|
||||||
|
} catch (\Throwable $th) {
|
||||||
|
throw new Exception("Error updating notifications: " . $th->getMessage() . ' line ' . $th->getLine(), 1);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,26 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
return function ($group, $notificationId) {
|
return function($notificationId) {
|
||||||
$notifications = Yaml::decode($this->notifications()->value());
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$notifications[$group][$notificationId]['isRead'] = true;
|
$notifications = $this->notifications()->isNotEmpty()
|
||||||
} catch (\Throwable $th) {
|
? Yaml::decode($this->notifications()->value())
|
||||||
//throw $th;
|
: [];
|
||||||
}
|
|
||||||
|
|
||||||
$this->update([
|
$newNotification = null;
|
||||||
"notifications" => $notifications
|
foreach ($notifications as $key => $notification) {
|
||||||
]);
|
if ($notification['id'] === $notificationId) {
|
||||||
return $notifications;
|
$notifications[$key]['isRead'] = true;
|
||||||
|
$newNotification = $notifications[$key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->update([
|
||||||
|
'notifications' => Yaml::encode(array_values($notifications))
|
||||||
|
]);
|
||||||
|
|
||||||
|
return $newNotification;
|
||||||
|
} catch (\Throwable $th) {
|
||||||
|
throw new Exception("Error updating notifications: " . $th->getMessage() . ' line ' . $th->getLine(), 1);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
@ -14,11 +14,11 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
return function ($projectUri, $notificationData) {
|
return function ($projectUri, $notificationData) {
|
||||||
$notificationData['isRead'] = false;
|
|
||||||
$recipients = page($projectUri)->managers()->toUsers()->not($this);
|
$recipients = page($projectUri)->managers()->toUsers()->not($this);
|
||||||
|
|
||||||
if (!$recipients) return;
|
if (!$recipients) return;
|
||||||
|
|
||||||
|
$notificationData['isRead'];
|
||||||
foreach ($recipients as $otherUser) {
|
foreach ($recipients as $otherUser) {
|
||||||
try {
|
try {
|
||||||
$notifications = $otherUser->notifications()->isNotEmpty()
|
$notifications = $otherUser->notifications()->isNotEmpty()
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
:id="`comment-${comment.id}`"
|
:id="`comment-${comment.id}`"
|
||||||
class="comment | flow"
|
class="comment | flow"
|
||||||
:data-status="status"
|
:data-status="status"
|
||||||
|
@click="read()"
|
||||||
>
|
>
|
||||||
<header>
|
<header>
|
||||||
<p>
|
<p>
|
||||||
|
|
@ -15,7 +16,7 @@
|
||||||
<time
|
<time
|
||||||
class="comment__date"
|
class="comment__date"
|
||||||
:datetime="dayjs(comment.date).format('YYYY-MM-DD')"
|
:datetime="dayjs(comment.date).format('YYYY-MM-DD')"
|
||||||
>{{ formatDate(comment.date) }}</time
|
>{{ formatDate() }}</time
|
||||||
>
|
>
|
||||||
</p>
|
</p>
|
||||||
</header>
|
</header>
|
||||||
|
|
@ -29,7 +30,7 @@
|
||||||
}}
|
}}
|
||||||
</p>
|
</p>
|
||||||
<div
|
<div
|
||||||
v-if="comment.author.uuid === user.uuid"
|
v-if="comment.author.uuid === userStore.user.uuid"
|
||||||
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">
|
||||||
|
|
@ -38,7 +39,7 @@
|
||||||
<button
|
<button
|
||||||
class="btn btn--transparent btn--icon btn--sm"
|
class="btn btn--transparent btn--icon btn--sm"
|
||||||
data-icon="delete"
|
data-icon="delete"
|
||||||
@click="deleteComment($event, comment)"
|
@click="deleteComment($event)"
|
||||||
>
|
>
|
||||||
<span class="sr-only">Supprimer</span>
|
<span class="sr-only">Supprimer</span>
|
||||||
</button>
|
</button>
|
||||||
|
|
@ -54,6 +55,7 @@ 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 } from "vue";
|
||||||
|
import { storeToRefs } from "pinia";
|
||||||
|
|
||||||
dayjs.locale("fr");
|
dayjs.locale("fr");
|
||||||
|
|
||||||
|
|
@ -64,22 +66,25 @@ const { comment, commentIndex } = defineProps({
|
||||||
|
|
||||||
const emits = defineEmits(["update:file", "close:comment"]);
|
const emits = defineEmits(["update:file", "close:comment"]);
|
||||||
|
|
||||||
const { user } = useUserStore();
|
const userStore = useUserStore();
|
||||||
const api = useApiStore();
|
const api = useApiStore();
|
||||||
const dialog = useDialogStore();
|
const dialog = useDialogStore();
|
||||||
|
|
||||||
// Functions
|
// Functions
|
||||||
const status = computed(() => {
|
const status = computed(() => {
|
||||||
const correspondingNotification = user.notifications.find(
|
const correspondingNotification = userStore.notifications.find(
|
||||||
(notification) => notification.id === comment.id
|
(notification) => notification.id === comment.id
|
||||||
);
|
);
|
||||||
if (!correspondingNotification) return undefined;
|
if (!correspondingNotification.isRead) {
|
||||||
return correspondingNotification.isRead ? undefined : "unread";
|
return "unread";
|
||||||
|
} else {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
function formatDate(date) {
|
function formatDate() {
|
||||||
const todayNumber = parseInt(dayjs().format("YYMMD"));
|
const todayNumber = parseInt(dayjs().format("YYMMD"));
|
||||||
const dateNumber = parseInt(dayjs(date).format("YYMMD"));
|
const dateNumber = parseInt(dayjs(comment.date).format("YYMMD"));
|
||||||
|
|
||||||
if (dateNumber === todayNumber) {
|
if (dateNumber === todayNumber) {
|
||||||
return "Aujourd'hui";
|
return "Aujourd'hui";
|
||||||
|
|
@ -97,17 +102,17 @@ function closeAddField() {
|
||||||
newCommentText.value = "";
|
newCommentText.value = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
async function readNotification(notificationId) {
|
async function read() {
|
||||||
const newNotifications = await api.readNotification(
|
if (status.value !== "unread") return;
|
||||||
user.uuid,
|
try {
|
||||||
"comments",
|
const newNotification = await api.readNotification(comment.id);
|
||||||
notificationId
|
userStore.readNotification(comment.id);
|
||||||
);
|
} catch (error) {
|
||||||
|
console.log("Erreur lors de la lecture de la notification : ", error);
|
||||||
user.notifications = newNotifications;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function deleteComment(event, comment) {
|
async function deleteComment(event) {
|
||||||
event.stopPropagation();
|
event.stopPropagation();
|
||||||
const newFile = await api.deleteComment(comment);
|
const newFile = await api.deleteComment(comment);
|
||||||
dialog.updateFile(newFile);
|
dialog.updateFile(newFile);
|
||||||
|
|
|
||||||
|
|
@ -12,10 +12,7 @@
|
||||||
:comment="comment"
|
:comment="comment"
|
||||||
:commentIndex="comments.length - commentIndex"
|
:commentIndex="comments.length - commentIndex"
|
||||||
:key="comment.id"
|
:key="comment.id"
|
||||||
@click="
|
@click="openComment(comment)"
|
||||||
readNotification(comment);
|
|
||||||
openComment(comment);
|
|
||||||
"
|
|
||||||
@update:file="changeFile"
|
@update:file="changeFile"
|
||||||
@close:comment="closeComment"
|
@close:comment="closeComment"
|
||||||
/>
|
/>
|
||||||
|
|
@ -248,16 +245,6 @@ function handleCommentPositionClick(event) {
|
||||||
toggleCommentPositionMode(false);
|
toggleCommentPositionMode(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
function readNotification(comment) {
|
|
||||||
const correspondingNotification = user.notifications.find(
|
|
||||||
(notification) => notification.id === comment.id
|
|
||||||
);
|
|
||||||
|
|
||||||
if (correspondingNotification) {
|
|
||||||
correspondingNotification.isRead = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function openComment(comment) {
|
function openComment(comment) {
|
||||||
if (comment.replies.length) {
|
if (comment.replies.length) {
|
||||||
openedComment.value = comment;
|
openedComment.value = comment;
|
||||||
|
|
|
||||||
|
|
@ -189,28 +189,25 @@ export const useApiStore = defineStore("api", () => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function readNotification(userUuid, group, notificationId) {
|
async function readNotification(notificationId) {
|
||||||
const headers = {
|
const headers = {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
userUuid,
|
|
||||||
notificationId,
|
notificationId,
|
||||||
group,
|
|
||||||
}),
|
}),
|
||||||
};
|
};
|
||||||
try {
|
try {
|
||||||
const response = await fetch("/mark-as-read.json", headers);
|
const response = await fetch("/read-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}`);
|
||||||
}
|
}
|
||||||
const newNotifications = await response.json();
|
const data = await response.json();
|
||||||
return newNotifications;
|
if (data.error) {
|
||||||
|
throw new Error(data);
|
||||||
|
} else {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(
|
|
||||||
"Une erreur s'est produite lors de la lecture de la notification n°",
|
|
||||||
notificationId,
|
|
||||||
error
|
|
||||||
);
|
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,5 +10,13 @@ export const useUserStore = defineStore("user", () => {
|
||||||
: Object.values(user.value.notifications);
|
: Object.values(user.value.notifications);
|
||||||
});
|
});
|
||||||
|
|
||||||
return { user, notifications };
|
function readNotification(notificationId) {
|
||||||
|
user.value.notifications.forEach((notification) => {
|
||||||
|
if (notification.id === notificationId) {
|
||||||
|
notification.isRead = true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return { user, notifications, readNotification };
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue