From bd8d8a3f611701ae8302a3087667be6680cc1441 Mon Sep 17 00:00:00 2001 From: "maxime.batista" Date: Mon, 30 Oct 2023 16:49:50 +0100 Subject: [PATCH] persist in database sample form inputs and show the list of previously entered prompts when displaying form's result --- front/ViewRenderer.tsx | 2 ++ front/views/DisplayResults.tsx | 13 ++++++---- front/views/SampleForm.tsx | 10 ++++---- public/index.php | 5 ++-- src/Controller/SampleFormController.php | 19 ++++++++++++-- src/Gateway/FormResultGateway.php | 34 +++++++++++++++++++++++++ 6 files changed, 69 insertions(+), 14 deletions(-) create mode 100644 src/Gateway/FormResultGateway.php diff --git a/front/ViewRenderer.tsx b/front/ViewRenderer.tsx index ffaf886..17148dc 100644 --- a/front/ViewRenderer.tsx +++ b/front/ViewRenderer.tsx @@ -11,6 +11,8 @@ export function renderView(Component: FunctionComponent, args: {}) { document.getElementById('root') as HTMLElement ); + console.log(args) + root.render( diff --git a/front/views/DisplayResults.tsx b/front/views/DisplayResults.tsx index faf5e28..d6a88db 100644 --- a/front/views/DisplayResults.tsx +++ b/front/views/DisplayResults.tsx @@ -1,12 +1,15 @@ -import ReactDOM from "react-dom/client"; -import React from "react"; -export default function DisplayResults({password, username}: any) { +export default function DisplayResults({results}: { results: { name: string, description: string }[] }) { + const list = results.map(({name, description}) => +
+

username: {name}

+

description: {description}

+
+ ) return (
-

username: {username}

-

password: {password}

+ {list}
) } diff --git a/front/views/SampleForm.tsx b/front/views/SampleForm.tsx index 5108697..c8f1ca4 100644 --- a/front/views/SampleForm.tsx +++ b/front/views/SampleForm.tsx @@ -5,12 +5,12 @@ export default function SampleForm() { return (

Hello, this is a sample form made in react !

-
+ - - - - + + + +
) diff --git a/public/index.php b/public/index.php index 71c24e3..813bf9e 100644 --- a/public/index.php +++ b/public/index.php @@ -5,6 +5,7 @@ require "../config.php"; require "../sql/database.php"; use App\Controller\SampleFormController; +use App\Gateway\FormResultGateway; /** * relative path of the index.php's directory from the server's document root. @@ -28,9 +29,9 @@ $pdo = get_database(); $router = new AltoRouter(); $router->setBasePath($basePath); -$sampleFormController = new SampleFormController(); +$sampleFormController = new SampleFormController(new FormResultGateway($pdo)); $router->map("GET", "/", fn() => $sampleFormController->displayForm()); -$router->map("POST", "/result", fn() => $sampleFormController->displayResults($_POST)); +$router->map("POST", "/submit", fn() => $sampleFormController->submitForm($_POST)); $match = $router->match(); diff --git a/src/Controller/SampleFormController.php b/src/Controller/SampleFormController.php index 5c4dfbd..57625e9 100644 --- a/src/Controller/SampleFormController.php +++ b/src/Controller/SampleFormController.php @@ -3,13 +3,28 @@ namespace App\Controller; require_once __DIR__ . "/../react-display.php"; +use App\Gateway\FormResultGateway; class SampleFormController { + + private FormResultGateway $gateway; + + /** + * @param FormResultGateway $gateway + */ + public function __construct(FormResultGateway $gateway) + { + $this->gateway = $gateway; + } + + public function displayForm() { send_react_front("views/SampleForm.tsx", []); } - public function displayResults(array $request) { - send_react_front("views/DisplayResults.tsx", $request); + public function submitForm(array $request) { + $this->gateway->insert($request["name"], $request["description"]); + $results = ["results" => $this->gateway->listResults()]; + send_react_front("views/DisplayResults.tsx", $results); } } \ No newline at end of file diff --git a/src/Gateway/FormResultGateway.php b/src/Gateway/FormResultGateway.php new file mode 100644 index 0000000..f34ca3a --- /dev/null +++ b/src/Gateway/FormResultGateway.php @@ -0,0 +1,34 @@ +insertion_stmnt = $pdo->prepare("INSERT INTO FormEntries VALUES (:name, :description)"); + $this->list_stmnt = $pdo->prepare("SELECT * FROM FormEntries"); + } + + + function insert(string $username, string $description) { + $this->insertion_stmnt->bindValue(":name", $username, PDO::PARAM_STR); + $this->insertion_stmnt->bindValue(":description", $description, PDO::PARAM_STR); + + $this->insertion_stmnt->execute(); + } + + function listResults(): array { + $this->list_stmnt->execute(); + return $this->list_stmnt->fetchAll(); + } +} \ No newline at end of file