read all working
This commit is contained in:
parent
495e1c024e
commit
ef09a04b5e
7 changed files with 105 additions and 65 deletions
|
|
@ -24,10 +24,4 @@ Kirby::plugin("adrienpayet/pdc-notifications", [
|
||||||
require(__DIR__ . "/routes/readAll.php"),
|
require(__DIR__ . "/routes/readAll.php"),
|
||||||
require(__DIR__ . "/routes/read.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")
|
|
||||||
]
|
|
||||||
]);
|
]);
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,20 @@ return [
|
||||||
'pattern' => '(:all)read-all-notifications.json',
|
'pattern' => '(:all)read-all-notifications.json',
|
||||||
'method' => 'GET',
|
'method' => 'GET',
|
||||||
'action' => function () {
|
'action' => function () {
|
||||||
$newNotifications = kirby()->user()->readAllNotifications();
|
$projects = page("projects");
|
||||||
return $newNotifications;
|
|
||||||
|
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()
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
@ -5,62 +5,88 @@ use Kirby\Data\Yaml;
|
||||||
use adrienpayet\notifications\Notification;
|
use adrienpayet\notifications\Notification;
|
||||||
|
|
||||||
class NotificationsPage extends Page {
|
class NotificationsPage extends Page {
|
||||||
public function createNotification($notificationData) {
|
public function createNotification($notificationData) {
|
||||||
$newNotification = new Notification($notificationData);
|
$newNotification = new Notification($notificationData);
|
||||||
|
|
||||||
$notifications = $this->notifications()->isNotEmpty()
|
$notifications = $this->notifications()->isNotEmpty()
|
||||||
? Yaml::decode($this->notifications()->value())
|
? Yaml::decode($this->notifications()->value())
|
||||||
: [];
|
: [];
|
||||||
|
|
||||||
$notifications[] = $newNotification->toArray();
|
$notifications[] = $newNotification->toArray();
|
||||||
|
|
||||||
$this->update([
|
$this->update([
|
||||||
'notifications' => $notifications
|
'notifications' => $notifications
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function deleteNotification($notificationId) {
|
public function deleteNotification($notificationId) {
|
||||||
$notifications = $this->notifications()->isNotEmpty()
|
$notifications = $this->notifications()->isNotEmpty()
|
||||||
? Yaml::decode($this->notifications()->value())
|
? Yaml::decode($this->notifications()->value())
|
||||||
: [];
|
: [];
|
||||||
|
|
||||||
foreach ($notifications as $key => $notification) {
|
foreach ($notifications as $key => $notification) {
|
||||||
if ($notification['id'] === $notificationId) {
|
if ($notification['id'] === $notificationId) {
|
||||||
unset($notifications[$key]);
|
unset($notifications[$key]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->update([
|
$this->update([
|
||||||
'notifications' => $notifications
|
'notifications' => $notifications
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function readNotification($notificationId) {
|
public function readNotification($notificationId) {
|
||||||
$notifications = $this->notifications()->isNotEmpty()
|
$notifications = $this->notifications()->isNotEmpty()
|
||||||
? Yaml::decode($this->notifications()->value())
|
? Yaml::decode($this->notifications()->value())
|
||||||
: [];
|
: [];
|
||||||
|
|
||||||
foreach ($notifications as $key => &$notification) {
|
foreach ($notifications as $key => &$notification) {
|
||||||
if ($notification['id'] !== $notificationId) {
|
if ($notification['id'] !== $notificationId) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isset($notification["readby"])) {
|
if (!isset($notification["readby"])) {
|
||||||
$notification["readby"] = [];
|
$notification["readby"] = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
$userUuid = (string) kirby()->user()->uuid();
|
$userUuid = (string) kirby()->user()->uuid();
|
||||||
if (in_array($userUuid, $notification["readby"])) {
|
if (in_array($userUuid, $notification["readby"])) {
|
||||||
return;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$notification["readby"][] = $userUuid;
|
$notification["readby"][] = $userUuid;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->update([
|
$this->update([
|
||||||
'notifications' => Yaml::encode($notifications)
|
'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)
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -151,7 +151,6 @@ async function read() {
|
||||||
comment.id,
|
comment.id,
|
||||||
page.value.uri
|
page.value.uri
|
||||||
);
|
);
|
||||||
userStore.readNotification(comment.id, route.params.id);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log("Erreur lors de la lecture de la notification : ", error);
|
console.log("Erreur lors de la lecture de la notification : ", error);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
import { defineStore, storeToRefs } from "pinia";
|
import { defineStore, storeToRefs } from "pinia";
|
||||||
import { usePageStore } from "./page.js";
|
import { useUserStore } from "./user";
|
||||||
import uniqid from "uniqid";
|
|
||||||
|
|
||||||
export const useApiStore = defineStore("api", () => {
|
export const useApiStore = defineStore("api", () => {
|
||||||
|
const userStore = useUserStore();
|
||||||
|
|
||||||
async function fetchData(path = window.location.pathname) {
|
async function fetchData(path = window.location.pathname) {
|
||||||
const isHomePage = path === "/";
|
const isHomePage = path === "/";
|
||||||
path = path === "/" ? "/home" : path;
|
path = path === "/" ? "/home" : path;
|
||||||
|
|
@ -166,6 +167,10 @@ export const useApiStore = defineStore("api", () => {
|
||||||
if (data.error) {
|
if (data.error) {
|
||||||
throw new Error(data);
|
throw new Error(data);
|
||||||
} else {
|
} else {
|
||||||
|
if (!projectId.startsWith("/")) {
|
||||||
|
projectId = "/" + projectId;
|
||||||
|
}
|
||||||
|
userStore.readNotification(notificationId, projectId);
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
@ -208,7 +213,10 @@ export const useApiStore = defineStore("api", () => {
|
||||||
if (data.error) {
|
if (data.error) {
|
||||||
throw new Error(data);
|
throw new Error(data);
|
||||||
} else {
|
} else {
|
||||||
|
console.log(data);
|
||||||
|
userStore.readAllNotifications();
|
||||||
console.log("All notifications read.");
|
console.log("All notifications read.");
|
||||||
|
return data;
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw error;
|
throw error;
|
||||||
|
|
|
||||||
|
|
@ -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) => ({
|
projects.value = projects.value.map((project) => ({
|
||||||
...project,
|
...project,
|
||||||
notifications:
|
notifications:
|
||||||
project.slug === projectSlug
|
project.uuid === projectId || project.uri === projectId
|
||||||
? project.notifications.map((notification) =>
|
? project.notifications.map((notification) =>
|
||||||
notification.id === notificationId
|
notification.id === notificationId
|
||||||
? {
|
? {
|
||||||
|
|
|
||||||
|
|
@ -117,7 +117,6 @@ function changeTab(newValue) {
|
||||||
function readAll() {
|
function readAll() {
|
||||||
try {
|
try {
|
||||||
api.readAllNotifications();
|
api.readAllNotifications();
|
||||||
userStore.readAllNotifications();
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log("Could not read all notifications : ", error);
|
console.log("Could not read all notifications : ", error);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue