Notifications page : mark all as read working

This commit is contained in:
isUnknown 2024-11-18 13:45:40 +01:00
parent cdf663d4ca
commit 5c9f450539
7 changed files with 76 additions and 19 deletions

View file

@ -1,9 +1,13 @@
<?php
Kirby::plugin('adrienpayet/pdc-notifications', [
'routes' => [
require(__DIR__ . '/routes/readAll.php'),
],
'userMethods' => [
'sendNotification' => require(__DIR__ . '/user-methods/send.php'),
'deleteNotification' => require(__DIR__ . '/user-methods/delete.php'),
'readNotification' => require(__DIR__ . '/user-methods/read.php')
'readNotification' => require(__DIR__ . '/user-methods/read.php'),
'readAllNotifications' => require(__DIR__ . '/user-methods/readAll.php')
]
]);

View file

@ -1,15 +0,0 @@
<?php
return [
'pattern' => '(:all)mark-as-read.json',
'method' => 'POST',
'action' => function () {
$json = file_get_contents('php://input');
$data = json_decode($json);
$user = kirby()->user($data->userUuid);
$newNotifications = $user->readNotification($data->group, $data->notificationId);
return $newNotifications;
}
];

View file

@ -0,0 +1,10 @@
<?php
return [
'pattern' => '(:all)read-all-notifications.json',
'method' => 'GET',
'action' => function () {
$newNotifications = kirby()->user()->readAllNotifications();
return $newNotifications;
}
];

View file

@ -0,0 +1,22 @@
<?php
return function() {
try {
$notifications = $this->notifications()->isNotEmpty()
? Yaml::decode($this->notifications()->value())
: [];
$newNotification = null;
foreach ($notifications as $key => $notification) {
$notifications[$key]['isRead'] = true;
}
$updatedUser = $this->update([
'notifications' => Yaml::encode(array_values($notifications))
]);
return Yaml::decode($updatedUser->notifications()->value());
} catch (\Throwable $th) {
throw new Exception("Error updating notifications: " . $th->getMessage() . ' line ' . $th->getLine(), 1);
}
};