From c75c5e1d8ab53e51e5856befddb123890d606995 Mon Sep 17 00:00:00 2001 From: isUnknown Date: Thu, 23 Jan 2025 16:16:51 +0100 Subject: [PATCH] project creation request notification working --- .../routes/request-project-creation.php | 21 ++++++++- .../plugins/classes/location/Location.php | 9 ---- .../plugins/classes/location/PageDetails.php | 1 + public/site/templates/projects.json.php | 2 +- .../notifications/ProjectRequest.vue | 46 +++++++++++++++++++ src/stores/projects.js | 11 ++++- src/views/Notifications.vue | 16 ++++++- 7 files changed, 93 insertions(+), 13 deletions(-) create mode 100644 src/components/notifications/ProjectRequest.vue diff --git a/public/site/config/routes/request-project-creation.php b/public/site/config/routes/request-project-creation.php index 3b2d70e..9a474d7 100644 --- a/public/site/config/routes/request-project-creation.php +++ b/public/site/config/routes/request-project-creation.php @@ -7,6 +7,8 @@ return [ $json = file_get_contents('php://input'); $data = json_decode($json); + $user = kirby()->user(); + $client = kirby()->user()->client()->toPage()->uuid(); $projectData = [ @@ -23,7 +25,24 @@ return [ $projects = page("projects"); try { - $projects->createChild($projectData); + $newProject = $projects->createChild($projectData); + + $date = new DateTime(); + $formattedDate = $date->format(DateTime::ISO8601); + + $notificationData = [ + "location" => [ + "page" => $newProject + ], + "date" => (string) $formattedDate, + "text" => nl2br(esc($data->details)), + "author" => $user, + "id" => Str::uuid(), + "type" => "project-request", + ]; + + $newProject->createNotification($notificationData); + return [ "status" => "success", ]; diff --git a/public/site/plugins/classes/location/Location.php b/public/site/plugins/classes/location/Location.php index b5649c5..39af7dc 100644 --- a/public/site/plugins/classes/location/Location.php +++ b/public/site/plugins/classes/location/Location.php @@ -5,16 +5,12 @@ namespace adrienpayet\D2P\data\location; class Location { protected PageDetails $page; - // protected ?string $dialogUri = null; - // protected ProjectDetails $project; protected ?FileDetails $file = null; protected ?array $parent = null; public function __construct(array $data) { $this->page = new PageDetails($data["page"]); - // $this->dialogUri = $data["dialogUri"]; - // $this->project = new ProjectDetails($data["project"]); if (isset($data['file'])) { $this->file = new FileDetails($data["file"]); @@ -33,13 +29,8 @@ class Location public function toArray() { $array = [ "page" => $this->page->toArray(), - // "project" => $this->project->toArray(), ]; - // if ($this->dialogUri) { - // $array["dialogUri"] = $this->dialogUri; - // } - if ($this->file) { $array["file"] = $this->file; } diff --git a/public/site/plugins/classes/location/PageDetails.php b/public/site/plugins/classes/location/PageDetails.php index 738a28c..be2c85a 100644 --- a/public/site/plugins/classes/location/PageDetails.php +++ b/public/site/plugins/classes/location/PageDetails.php @@ -16,6 +16,7 @@ class PageDetails return [ "uuid" => (string) $this->page->uuid(), "template" => (string) $this->page->template(), + "panelUrl" => (string) $this->page->panel()->url(), "uri" => (string) $this->page->uri(), "title" => (string) $this->page->title(), ]; diff --git a/public/site/templates/projects.json.php b/public/site/templates/projects.json.php index fe80916..ca28e48 100644 --- a/public/site/templates/projects.json.php +++ b/public/site/templates/projects.json.php @@ -16,7 +16,7 @@ function getProjectData($project) { } try { - $children = $kirby->user()->role() == "admin" ? $page->children()->map(fn($project) => getProjectData($project))->values() : $kirby->user()->projects()->toPages()->map(fn($project) => getProjectData($project))->values(); + $children = $kirby->user()->role() == "admin" ? $page->childrenAndDrafts()->map(fn($project) => getProjectData($project))->values() : $kirby->user()->projects()->toPages()->map(fn($project) => getProjectData($project))->values(); } catch (\Throwable $th) { throw new Exception($th->getMessage() . " line " . $th->getLine(), 1); $children = []; diff --git a/src/components/notifications/ProjectRequest.vue b/src/components/notifications/ProjectRequest.vue new file mode 100644 index 0000000..93bb3aa --- /dev/null +++ b/src/components/notifications/ProjectRequest.vue @@ -0,0 +1,46 @@ + + + diff --git a/src/stores/projects.js b/src/stores/projects.js index 8e7d421..b1861b7 100644 --- a/src/stores/projects.js +++ b/src/stores/projects.js @@ -8,6 +8,9 @@ export const useProjectsStore = defineStore("projects", () => { const currentProjects = computed(() => { return projects.value.filter((project) => project.status === "listed"); }); + const draftProjects = computed(() => { + return projects.value.filter((project) => project.status === "draft"); + }); const archivedProjects = computed(() => { return projects.value.filter((project) => project.status === "unlisted"); }); @@ -23,5 +26,11 @@ export const useProjectsStore = defineStore("projects", () => { return project; } - return { projects, currentProjects, archivedProjects, getProjectByUuid }; + return { + projects, + currentProjects, + archivedProjects, + draftProjects, + getProjectByUuid, + }; }); diff --git a/src/views/Notifications.vue b/src/views/Notifications.vue index 3c56093..77eca51 100644 --- a/src/views/Notifications.vue +++ b/src/views/Notifications.vue @@ -45,7 +45,7 @@ :is="notificationComponents[notification.type]" :notification="notification" :data-status="notification.isRead ? 'read' : 'unread'" - @click="router.push(getHref(notification))" + @click="handleNotificationClick(notification)" /> @@ -64,6 +64,7 @@ import Comment from "../components/notifications/Comment.vue"; import Reply from "../components/notifications/Reply.vue"; import Content from "../components/notifications/Content.vue"; import { useRouter } from "vue-router"; +import ProjectRequest from "../components/notifications/ProjectRequest.vue"; dayjs.locale("fr"); @@ -95,6 +96,7 @@ const notificationComponents = { comment: Comment, "comment-reply": Reply, content: Content, + "project-request": ProjectRequest, }; const sortedNotifications = computed(() => { @@ -123,6 +125,14 @@ function readAll() { } // Functions +function handleNotificationClick(notification) { + const href = getHref(notification); + if (href.startsWith("http")) { + window.open(href, "_blank"); + } else { + router.push(href); + } +} function getHref(notification) { const uri = notification.location.page.uri; @@ -138,6 +148,10 @@ function getHref(notification) { return notification.project.uri + "?dialog=virtual-sample&comments=true"; } + if (notification.location.page.panelurl) { + return notification.location.page.panelurl; + } + return uri; }