From edc64700c2562bde448b7bb5b5f796e07ba55df0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milan=20Felix=20=C5=A0ulc?= Date: Thu, 28 Sep 2023 16:42:01 +0200 Subject: [PATCH] App: use nella, better UI --- app/{bootstrap.php => Bootstrap.php} | 0 app/UI/@Templates/@layout.latte | 48 +++++++++++++ app/UI/Admin/AdminPresenter.php | 10 +++ .../Admin/Templates}/@layout.latte | 2 +- app/UI/Admin/Templates/default.latte | 4 ++ app/UI/BasePresenter.php | 10 +++ .../Home/HomePresenter.php} | 24 ++++--- app/UI/Home/Templates/default.latte | 68 +++++++++++++++++++ app/presenters/AdminPresenter.php | 8 --- app/presenters/BasePresenter.php | 13 ---- app/presenters/Error4xxPresenter.php | 31 --------- app/presenters/ErrorPresenter.php | 50 -------------- app/presenters/templates/@layout.latte | 31 --------- app/presenters/templates/Admin/default.latte | 2 - app/presenters/templates/Error/403.latte | 7 -- app/presenters/templates/Error/404.latte | 8 --- app/presenters/templates/Error/405.latte | 6 -- app/presenters/templates/Error/410.latte | 6 -- app/presenters/templates/Error/4xx.latte | 4 -- app/presenters/templates/Error/500.phtml | 20 ------ app/presenters/templates/Error/503.phtml | 22 ------ .../templates/Homepage/default.latte | 62 ----------------- app/router/RouterFactory.php | 20 ------ 23 files changed, 154 insertions(+), 302 deletions(-) rename app/{bootstrap.php => Bootstrap.php} (100%) create mode 100644 app/UI/@Templates/@layout.latte create mode 100644 app/UI/Admin/AdminPresenter.php rename app/{presenters/templates/Admin => UI/Admin/Templates}/@layout.latte (81%) create mode 100644 app/UI/Admin/Templates/default.latte create mode 100644 app/UI/BasePresenter.php rename app/{presenters/HomepagePresenter.php => UI/Home/HomePresenter.php} (70%) create mode 100644 app/UI/Home/Templates/default.latte delete mode 100644 app/presenters/AdminPresenter.php delete mode 100644 app/presenters/BasePresenter.php delete mode 100644 app/presenters/Error4xxPresenter.php delete mode 100644 app/presenters/ErrorPresenter.php delete mode 100644 app/presenters/templates/@layout.latte delete mode 100644 app/presenters/templates/Admin/default.latte delete mode 100644 app/presenters/templates/Error/403.latte delete mode 100644 app/presenters/templates/Error/404.latte delete mode 100644 app/presenters/templates/Error/405.latte delete mode 100644 app/presenters/templates/Error/410.latte delete mode 100644 app/presenters/templates/Error/4xx.latte delete mode 100644 app/presenters/templates/Error/500.phtml delete mode 100644 app/presenters/templates/Error/503.phtml delete mode 100644 app/presenters/templates/Homepage/default.latte delete mode 100644 app/router/RouterFactory.php diff --git a/app/bootstrap.php b/app/Bootstrap.php similarity index 100% rename from app/bootstrap.php rename to app/Bootstrap.php diff --git a/app/UI/@Templates/@layout.latte b/app/UI/@Templates/@layout.latte new file mode 100644 index 00000000..a8bc4fd1 --- /dev/null +++ b/app/UI/@Templates/@layout.latte @@ -0,0 +1,48 @@ + + + + + + + {block #title|striptags}Webpack Skeleton{/} + + {block #head} + {* This CSS bundle is generated via Webpack. See webpack.config.js *} + + {/block} + + + +
+
+
+

+ Contributte / Webpack Skeleton +

+
+ +
+
+ {$flash->message} +
+
+ +
+ {include #content} +
+ +
+ © {=date(Y)} by @f3l1x +
+
+
+ +{block #scripts} + {* This JS bundle is generated via Webpack. See webpack.config.js *} + +{/block} + + + + + diff --git a/app/UI/Admin/AdminPresenter.php b/app/UI/Admin/AdminPresenter.php new file mode 100644 index 00000000..cae45ba6 --- /dev/null +++ b/app/UI/Admin/AdminPresenter.php @@ -0,0 +1,10 @@ + diff --git a/app/UI/Admin/Templates/default.latte b/app/UI/Admin/Templates/default.latte new file mode 100644 index 00000000..f37acccf --- /dev/null +++ b/app/UI/Admin/Templates/default.latte @@ -0,0 +1,4 @@ +{block #content} +
Go to front →
+ +
This is admin!
diff --git a/app/UI/BasePresenter.php b/app/UI/BasePresenter.php new file mode 100644 index 00000000..ae2bbf71 --- /dev/null +++ b/app/UI/BasePresenter.php @@ -0,0 +1,10 @@ +addText('username', 'Username') ->setRequired('Username is mandatory') - ->setAttribute('placeholder', 'Type your name Mr.?'); + ->setHtmlAttribute('placeholder', 'Type your name Mr.?'); $form->addText('email', 'Email') ->setHtmlAttribute('placeholder', 'Type your e-mail') ->setOption('description', Html::el('span')->setHtml('Try to type cool@nette.org to see validation.')) ->setEmptyValue('@') - ->addFilter(function ($email) { - return Strings::lower($email); - }) + ->addFilter(fn ($email) => Strings::lower($email)) ->addRule($form::REQUIRED, 'E-mail is mandatory') ->addRule($form::EMAIL, 'Given e-mail is not e-mail'); @@ -51,20 +51,22 @@ protected function createComponentUserForm(): Form $form->addSubmit('send', 'OK'); - $form->onValidate[] = function (Form $form) { + $form->onValidate[] = function (Form $form): void { + $values = $form->getUnsafeValues(ArrayHash::class); + // Validate e-mail duplicities (against DB?) - if (Strings::endsWith($form->values->email, '@nette.org')) { - $form->addError(sprintf('E-mail "%s" is already picked', $form->values->email)); + if (str_ends_with($values->email, '@nette.org')) { + $form->addError(sprintf('E-mail "%s" is already picked', $values->email)); } }; - $form->onSubmit[] = function () { + $form->onSubmit[] = function (): void { // This method in invoked always $this->redrawControl('userFormError'); $this->redrawControl('userFormOk'); }; - $form->onSuccess[] = function () { + $form->onSuccess[] = function (): void { // Some handling on success... }; diff --git a/app/UI/Home/Templates/default.latte b/app/UI/Home/Templates/default.latte new file mode 100644 index 00000000..beeab79f --- /dev/null +++ b/app/UI/Home/Templates/default.latte @@ -0,0 +1,68 @@ +{block #content} +
Go to admin →
+ +
Take a look at webpack.config.js.
+ +
+
Snippets
+ + +
+
+
Box 1 RELOAD
+
Time is: {$datetime|date:'d.m.Y H:i:s'}
+
+
+
Box 2 RELOAD
+
Time is: {$datetime|date:'H:i:s'}
+
+
+
Box 3 RELOAD
+
Time is: {$datetime|date:'U'}
+
+
+
+ +
+
Forms
+
+
+ {snippet userFormOk} + {php $form = $presenter['userForm']} + {if $form->isSuccess()} +
Form OK!
+
{=Tracy\Debugger::dump($form->values, true)|noescape}
+ {/if} + {/snippet} + + {snippet userFormError} + {php $form = $presenter['userForm']} +
+ {$error} +
+ {/snippet} + +
+
+ {label username /} + + + {$form['username']->getOption('description')} +
+
+ {label email /} + + + {$form['email']->getOption('description')} +
+
+ {label age /} + {input age, class => 'mt-1 block w-full'} + + {$form['age']->getOption('description')} +
+ +
+
+
+
diff --git a/app/presenters/AdminPresenter.php b/app/presenters/AdminPresenter.php deleted file mode 100644 index d6730f91..00000000 --- a/app/presenters/AdminPresenter.php +++ /dev/null @@ -1,8 +0,0 @@ -getRequest()?->isMethod(Request::FORWARD)) { - $this->error(); - } - } - - public function renderDefault(BadRequestException $exception): void - { - // load template 403.latte or 404.latte or ... 4xx.latte - $file = __DIR__ . sprintf('/templates/Error/%s.latte', $exception->getCode()); - $file = is_file($file) ? $file : __DIR__ . '/templates/Error/4xx.latte'; - $this->template->setFile($file); - } - -} diff --git a/app/presenters/ErrorPresenter.php b/app/presenters/ErrorPresenter.php deleted file mode 100644 index fab24fac..00000000 --- a/app/presenters/ErrorPresenter.php +++ /dev/null @@ -1,50 +0,0 @@ -logger = $logger; - } - - public function run(Request $request): Response - { - $e = $request->getParameter('exception'); - - if ($e instanceof BadRequestException) { - // $this->logger->log("HTTP code {$e->getCode()}: {$e->getMessage()} in {$e->getFile()}:{$e->getLine()}", 'access'); - [$module, , $sep] = Helpers::splitName($request->getPresenterName()); - $errorPresenter = $module . $sep . 'Error4xx'; - return new ForwardResponse($request->setPresenterName($errorPresenter)); - } - - $this->logger->log($e, ILogger::EXCEPTION); - - return new CallbackResponse(function (IRequest $httpRequest, IResponse $httpResponse) { - if (preg_match('#^text/html(?:;|$)#', (string) $httpResponse->getHeader('Content-Type'))) { - require __DIR__ . '/templates/Error/500.phtml'; - } - }); - } - -} diff --git a/app/presenters/templates/@layout.latte b/app/presenters/templates/@layout.latte deleted file mode 100644 index ab2d262e..00000000 --- a/app/presenters/templates/@layout.latte +++ /dev/null @@ -1,31 +0,0 @@ -{** - * @param string $basePath web base path - * @param array $flashes flash messages - *} - - - - - - {ifset title}{include title|stripHtml} | {/ifset}Nette + Webpack 5 - - - {block #head} - {* This CSS bundle is generated via Webpack. See webpack.config.js *} - - {/block} - - - -
-
{$flash->message}
- - {include #content} -
- -{block #scripts} - {* This JS bundle is generated via Webpack. See webpack.config.js *} - -{/block} - - diff --git a/app/presenters/templates/Admin/default.latte b/app/presenters/templates/Admin/default.latte deleted file mode 100644 index c384f2c0..00000000 --- a/app/presenters/templates/Admin/default.latte +++ /dev/null @@ -1,2 +0,0 @@ -{block #content} -

This is admin

diff --git a/app/presenters/templates/Error/403.latte b/app/presenters/templates/Error/403.latte deleted file mode 100644 index de00328e..00000000 --- a/app/presenters/templates/Error/403.latte +++ /dev/null @@ -1,7 +0,0 @@ -{block content} -

Access Denied

- -

You do not have permission to view this page. Please try contact the web -site administrator if you believe you should be able to view this page.

- -

error 403

diff --git a/app/presenters/templates/Error/404.latte b/app/presenters/templates/Error/404.latte deleted file mode 100644 index 022001c2..00000000 --- a/app/presenters/templates/Error/404.latte +++ /dev/null @@ -1,8 +0,0 @@ -{block content} -

Page Not Found

- -

The page you requested could not be found. It is possible that the address is -incorrect, or that the page no longer exists. Please use a search engine to find -what you are looking for.

- -

error 404

diff --git a/app/presenters/templates/Error/405.latte b/app/presenters/templates/Error/405.latte deleted file mode 100644 index d4248923..00000000 --- a/app/presenters/templates/Error/405.latte +++ /dev/null @@ -1,6 +0,0 @@ -{block content} -

Method Not Allowed

- -

The requested method is not allowed for the URL.

- -

error 405

diff --git a/app/presenters/templates/Error/410.latte b/app/presenters/templates/Error/410.latte deleted file mode 100644 index 99bde92c..00000000 --- a/app/presenters/templates/Error/410.latte +++ /dev/null @@ -1,6 +0,0 @@ -{block content} -

Page Not Found

- -

The page you requested has been taken off the site. We apologize for the inconvenience.

- -

error 410

diff --git a/app/presenters/templates/Error/4xx.latte b/app/presenters/templates/Error/4xx.latte deleted file mode 100644 index d5ce82f1..00000000 --- a/app/presenters/templates/Error/4xx.latte +++ /dev/null @@ -1,4 +0,0 @@ -{block content} -

Oops...

- -

Your browser sent a request that this server could not understand or process.

diff --git a/app/presenters/templates/Error/500.phtml b/app/presenters/templates/Error/500.phtml deleted file mode 100644 index 619611ea..00000000 --- a/app/presenters/templates/Error/500.phtml +++ /dev/null @@ -1,20 +0,0 @@ - - - -Server Error - - - -
-

Server Error

- -

We're sorry! The server encountered an internal error and - was unable to complete your request. Please try again later.

- -

error 500

-
diff --git a/app/presenters/templates/Error/503.phtml b/app/presenters/templates/Error/503.phtml deleted file mode 100644 index 1c2c4273..00000000 --- a/app/presenters/templates/Error/503.phtml +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - - -Site is temporarily down for maintenance - -

We're Sorry

- -

The site is temporarily down for maintenance. Please try again in a few minutes.

diff --git a/app/presenters/templates/Homepage/default.latte b/app/presenters/templates/Homepage/default.latte deleted file mode 100644 index a407cf37..00000000 --- a/app/presenters/templates/Homepage/default.latte +++ /dev/null @@ -1,62 +0,0 @@ -{block #content} -

Nette + Webpack 5

-

Take a look at webpack.config.js.

- -
-

Snippets RELOAD ALL

-
-
-
-

Box 1 RELOAD

- Time is: {$datetime|date:'d.m.Y H:i:s'} -
-
-

Box 2 RELOAD

- Time is: {$datetime|date:'H:i:s'} -
-
-

Box 3 RELOAD

- Time is: {$datetime|date:'U'} -
-
-
- -
-

Form

-
-
- - {snippet userFormOk} - {php $form = $presenter['userForm']} - {if $form->isSuccess()} -
Form OK!
-
{=Tracy\Debugger::dump($form->values, true)|noescape}
- {/if} - {/snippet} - - {snippet userFormError} - {php $form = $presenter['userForm']} -
{$error}
- {/snippet} - -
- {label username /} - - - {$form['username']->getOption('description')} -
-
- {label email /} - - - {$form['email']->getOption('description')} -
-
- {label age /} - {input age, class => 'form-control'} - - {$form['age']->getOption('description')} -
- -
-
diff --git a/app/router/RouterFactory.php b/app/router/RouterFactory.php deleted file mode 100644 index dcc4c2cf..00000000 --- a/app/router/RouterFactory.php +++ /dev/null @@ -1,20 +0,0 @@ -/', 'Homepage:default'); - - return $router; - } - -}