From ef09a04b5e9ec2e4f56dfa88465140eafaf4893a Mon Sep 17 00:00:00 2001 From: isUnknown Date: Wed, 15 Jan 2025 16:24:34 +0100 Subject: [PATCH] read all working --- public/site/plugins/notifications/index.php | 6 - .../plugins/notifications/routes/readAll.php | 17 ++- .../notifications/src/NotificationsPage.php | 128 +++++++++++------- src/components/comments/Comment.vue | 1 - src/stores/api.js | 12 +- src/stores/user.js | 5 +- src/views/Notifications.vue | 1 - 7 files changed, 105 insertions(+), 65 deletions(-) diff --git a/public/site/plugins/notifications/index.php b/public/site/plugins/notifications/index.php index ee316ec..91da6f9 100644 --- a/public/site/plugins/notifications/index.php +++ b/public/site/plugins/notifications/index.php @@ -24,10 +24,4 @@ Kirby::plugin("adrienpayet/pdc-notifications", [ require(__DIR__ . "/routes/readAll.php"), require(__DIR__ . "/routes/read.php") ], - "userMethods" => [ - // "sendNotification" => require(__DIR__ . "/user-methods/send.php"), - "deleteNotification" => require(__DIR__ . "/user-methods/delete.php"), - "readNotification" => require(__DIR__ . "/user-methods/read.php"), - "readAllNotifications" => require(__DIR__ . "/user-methods/readAll.php") - ] ]); diff --git a/public/site/plugins/notifications/routes/readAll.php b/public/site/plugins/notifications/routes/readAll.php index 9a9f5fc..22b66e1 100644 --- a/public/site/plugins/notifications/routes/readAll.php +++ b/public/site/plugins/notifications/routes/readAll.php @@ -4,7 +4,20 @@ return [ 'pattern' => '(:all)read-all-notifications.json', 'method' => 'GET', 'action' => function () { - $newNotifications = kirby()->user()->readAllNotifications(); - return $newNotifications; + $projects = page("projects"); + + foreach ($projects->children() as $project) { + try { + $project->readAllNotifications(); + return json_encode([ + "status" => "success" + ]); + } catch (\Throwable $th) { + return json_encode([ + "status" => "error", + "message" => $th->getMessage() . ' line ' . $th->getLine() . " in file " . $th->getFile() + ]); + } + } } ]; \ No newline at end of file diff --git a/public/site/plugins/notifications/src/NotificationsPage.php b/public/site/plugins/notifications/src/NotificationsPage.php index c8aed33..bc6afcc 100644 --- a/public/site/plugins/notifications/src/NotificationsPage.php +++ b/public/site/plugins/notifications/src/NotificationsPage.php @@ -5,62 +5,88 @@ use Kirby\Data\Yaml; use adrienpayet\notifications\Notification; class NotificationsPage extends Page { - public function createNotification($notificationData) { - $newNotification = new Notification($notificationData); + public function createNotification($notificationData) { + $newNotification = new Notification($notificationData); - $notifications = $this->notifications()->isNotEmpty() - ? Yaml::decode($this->notifications()->value()) - : []; - - $notifications[] = $newNotification->toArray(); + $notifications = $this->notifications()->isNotEmpty() + ? Yaml::decode($this->notifications()->value()) + : []; + + $notifications[] = $newNotification->toArray(); - $this->update([ - 'notifications' => $notifications - ]); - } + $this->update([ + 'notifications' => $notifications + ]); + } - public function deleteNotification($notificationId) { - $notifications = $this->notifications()->isNotEmpty() - ? Yaml::decode($this->notifications()->value()) - : []; - - foreach ($notifications as $key => $notification) { - if ($notification['id'] === $notificationId) { - unset($notifications[$key]); - } - } + public function deleteNotification($notificationId) { + $notifications = $this->notifications()->isNotEmpty() + ? Yaml::decode($this->notifications()->value()) + : []; + + foreach ($notifications as $key => $notification) { + if ($notification['id'] === $notificationId) { + unset($notifications[$key]); + } + } - $this->update([ - 'notifications' => $notifications - ]); - } + $this->update([ + 'notifications' => $notifications + ]); + } - public function readNotification($notificationId) { - $notifications = $this->notifications()->isNotEmpty() - ? Yaml::decode($this->notifications()->value()) - : []; - - foreach ($notifications as $key => &$notification) { - if ($notification['id'] !== $notificationId) { - continue; - } - - if (!isset($notification["readby"])) { - $notification["readby"] = []; - } - - $userUuid = (string) kirby()->user()->uuid(); - if (in_array($userUuid, $notification["readby"])) { - return; - } - - $notification["readby"][] = $userUuid; - break; - } + public function readNotification($notificationId) { + $notifications = $this->notifications()->isNotEmpty() + ? Yaml::decode($this->notifications()->value()) + : []; + + foreach ($notifications as $key => &$notification) { + if ($notification['id'] !== $notificationId) { + continue; + } + + if (!isset($notification["readby"])) { + $notification["readby"] = []; + } + + $userUuid = (string) kirby()->user()->uuid(); + if (in_array($userUuid, $notification["readby"])) { + continue; + } + + $notification["readby"][] = $userUuid; + break; + } - $this->update([ - 'notifications' => Yaml::encode($notifications) - ]); - } + $this->update([ + 'notifications' => Yaml::encode($notifications) + ]); + } + + public function readAllNotifications() { + $notifications = $this->notifications()->isNotEmpty() + ? Yaml::decode($this->notifications()->value()) + : []; + + foreach ($notifications as $key => &$notification) { + if (!isset($notification["readby"])) { + $notification["readby"] = []; + } + + $userUuid = (string) kirby()->user()->uuid(); + if (in_array($userUuid, $notification["readby"])) { + continue; + } + + $notification["readby"][] = $userUuid; + break; + } + + + + $this->update([ + 'notifications' => Yaml::encode($notifications) + ]); + } } \ No newline at end of file diff --git a/src/components/comments/Comment.vue b/src/components/comments/Comment.vue index 68713fb..5803e9b 100644 --- a/src/components/comments/Comment.vue +++ b/src/components/comments/Comment.vue @@ -151,7 +151,6 @@ async function read() { comment.id, page.value.uri ); - userStore.readNotification(comment.id, route.params.id); } catch (error) { console.log("Erreur lors de la lecture de la notification : ", error); } diff --git a/src/stores/api.js b/src/stores/api.js index bd83048..f9475e1 100644 --- a/src/stores/api.js +++ b/src/stores/api.js @@ -1,8 +1,9 @@ import { defineStore, storeToRefs } from "pinia"; -import { usePageStore } from "./page.js"; -import uniqid from "uniqid"; +import { useUserStore } from "./user"; export const useApiStore = defineStore("api", () => { + const userStore = useUserStore(); + async function fetchData(path = window.location.pathname) { const isHomePage = path === "/"; path = path === "/" ? "/home" : path; @@ -166,6 +167,10 @@ export const useApiStore = defineStore("api", () => { if (data.error) { throw new Error(data); } else { + if (!projectId.startsWith("/")) { + projectId = "/" + projectId; + } + userStore.readNotification(notificationId, projectId); return data; } } catch (error) { @@ -208,7 +213,10 @@ export const useApiStore = defineStore("api", () => { if (data.error) { throw new Error(data); } else { + console.log(data); + userStore.readAllNotifications(); console.log("All notifications read."); + return data; } } catch (error) { throw error; diff --git a/src/stores/user.js b/src/stores/user.js index 136f323..7fe5956 100644 --- a/src/stores/user.js +++ b/src/stores/user.js @@ -21,11 +21,12 @@ export const useUserStore = defineStore("user", () => { }); }); - function readNotification(notificationId, projectSlug) { + function readNotification(notificationId, projectId) { + console.log("Read notification", notificationId, projectId); projects.value = projects.value.map((project) => ({ ...project, notifications: - project.slug === projectSlug + project.uuid === projectId || project.uri === projectId ? project.notifications.map((notification) => notification.id === notificationId ? { diff --git a/src/views/Notifications.vue b/src/views/Notifications.vue index a2c09c1..3c56093 100644 --- a/src/views/Notifications.vue +++ b/src/views/Notifications.vue @@ -117,7 +117,6 @@ function changeTab(newValue) { function readAll() { try { api.readAllNotifications(); - userStore.readAllNotifications(); } catch (error) { console.log("Could not read all notifications : ", error); }