From 54af78e32c21f358e11ad5204a9247a026ee6171 Mon Sep 17 00:00:00 2001 From: isUnknown Date: Mon, 27 Jan 2025 14:39:52 +0100 Subject: [PATCH] DTL : optimization appointment request working --- .../site/blueprints/tabs/design-to-light.yml | 9 ++++ public/site/config/config.php | 1 + .../request-optimization-appointment.php | 52 +++++++++++++++++++ src/components/ProjectRequestDialog.vue | 3 +- src/components/design-to-light/DTLPanel.vue | 24 +++++---- .../OptimizationRequestDialog.vue | 51 ++++++++++++++++-- src/stores/api.js | 13 ++--- src/views/DesignToLight.vue | 4 +- 8 files changed, 132 insertions(+), 25 deletions(-) create mode 100644 public/site/config/routes/request-optimization-appointment.php diff --git a/public/site/blueprints/tabs/design-to-light.yml b/public/site/blueprints/tabs/design-to-light.yml index d1f45b6..564446b 100644 --- a/public/site/blueprints/tabs/design-to-light.yml +++ b/public/site/blueprints/tabs/design-to-light.yml @@ -3,6 +3,15 @@ label: Design to Light icon: leaf fields: + hasOptimizationRequest: + type: hidden + default: "false" + optimizationRequestDetails: + label: Demande de rendez-vous + type: textarea + disabled: true + when: + hasOptimizationRequest: "true" isDTLEnabled: label: Actif type: toggle diff --git a/public/site/config/config.php b/public/site/config/config.php index 018f3ba..c05e2bf 100644 --- a/public/site/config/config.php +++ b/public/site/config/config.php @@ -29,6 +29,7 @@ return [ require(__DIR__ . '/routes/upload-pdf.php'), require(__DIR__ . '/routes/validate-brief.php'), require(__DIR__ . '/routes/request-project-creation.php'), + require(__DIR__ . '/routes/request-optimization-appointment.php'), ], 'hooks' => [ 'page.create:after' => require_once(__DIR__ . '/hooks/create-steps.php'), diff --git a/public/site/config/routes/request-optimization-appointment.php b/public/site/config/routes/request-optimization-appointment.php new file mode 100644 index 0000000..60b5cce --- /dev/null +++ b/public/site/config/routes/request-optimization-appointment.php @@ -0,0 +1,52 @@ + 'request-optimization-appointment.json', + 'method' => 'POST', + 'action' => function () { + $json = file_get_contents('php://input'); + $data = json_decode($json); + + $user = kirby()->user(); + $project = page($data->projectUri); + + try { + $newProject = $project->update([ + "hasOptimizationRequest" => "true", + "optimizationRequestDetails" => esc("De la part de " . kirby()->user()->name() . " (" . kirby()->user()->email() . ") : \n\n" . "Objet : " . $data->subject . "\n" . $data->details) + ]); + } catch (\Throwable $th) { + return [ + "status" => "error", + "message" => "Can't update project " . $project->title()->value() . ". " . $th->getMessage() . " in " . $th->getFile() . " line " . $th->getLine() + ]; + } + + try { + $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" => "appointment-request", + ]; + + $newProject->createNotification($notificationData); + + return [ + "status" => "success", + ]; + } catch (\Throwable $th) { + return [ + "status" => "error", + "message" => "Can't create notification. " . $th->getMessage() . " in " . $th->getFile() . " line " . $th->getLine() + ]; + } + } +]; diff --git a/src/components/ProjectRequestDialog.vue b/src/components/ProjectRequestDialog.vue index 422920f..04df96a 100644 --- a/src/components/ProjectRequestDialog.vue +++ b/src/components/ProjectRequestDialog.vue @@ -30,7 +30,8 @@ async function handleSubmit() { isDTLEnabled: isDTLEnabled.value, }; - const response = await api.requestProjectCreation(formData); + const response = await api.post(formData, "request-project-creation.json"); + console.log(response); } diff --git a/src/components/design-to-light/DTLPanel.vue b/src/components/design-to-light/DTLPanel.vue index 4b650b8..28b9503 100644 --- a/src/components/design-to-light/DTLPanel.vue +++ b/src/components/design-to-light/DTLPanel.vue @@ -145,16 +145,20 @@ - - + + + diff --git a/src/stores/api.js b/src/stores/api.js index 1e1720d..d12707a 100644 --- a/src/stores/api.js +++ b/src/stores/api.js @@ -75,24 +75,21 @@ export const useApiStore = defineStore("api", () => { } } - async function requestProjectCreation(data) { + async function post(data, route) { const headers = { method: "POST", body: JSON.stringify(data), }; try { - const response = await fetch("/request-project-creation.json", headers); + const response = await fetch(route, headers); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const data = await response.json(); - console.log(data); + return data; } catch (error) { - console.error( - "Une erreur s'est produite lors de l'ajout du commentaire :", - error - ); + console.error("Une erreur s'est produite :", error); throw error; } } @@ -249,6 +246,6 @@ export const useApiStore = defineStore("api", () => { readNotification, readAllNotifications, validateBrief, - requestProjectCreation, + post, }; }); diff --git a/src/views/DesignToLight.vue b/src/views/DesignToLight.vue index 2abf653..0c53cc1 100644 --- a/src/views/DesignToLight.vue +++ b/src/views/DesignToLight.vue @@ -27,7 +27,7 @@ @@ -39,5 +39,5 @@ import { ref } from "vue"; const { page } = storeToRefs(usePageStore()); -const isDialogOpen = ref(false); +const isOptimizationDialogOpen = ref(false);