From 89a4236ef9bbd9a0260883d3282c28889fd21e8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fernando=20Corr=C3=AAa=20de=20Oliveira?= Date: Tue, 7 Nov 2023 01:53:05 +0000 Subject: [PATCH] redirect --- README.md | 31 ++++++++++++++++++++--------- examples/App.rakumod | 10 ++++++++++ examples/TodoList.rakumod | 6 ++---- examples/humming-bird-todo.raku | 8 +++++--- lib/HTML/Component.rakumod | 20 ------------------- lib/HTML/Component/Endpoint.rakumod | 2 +- lib/HTML/Component/Tag/Node.rakumod | 8 ++++++++ 7 files changed, 48 insertions(+), 37 deletions(-) create mode 100644 examples/App.rakumod diff --git a/README.md b/README.md index 87b551f..95c3be0 100644 --- a/README.md +++ b/README.md @@ -51,9 +51,7 @@ has Todo @.todos; method RENDER($_) { .ol: { - for @!todos -> Todo $todo { - .add-child: $todo - } + .add-children: @!todos; } .form: :endpoint(self.new-todo), @@ -66,14 +64,28 @@ method RENDER($_) { method new-todo(Str :$description!) is endpoint{ :path, - :return(-> | { boilerplate :title("My TODO list"), { .add-child: TodoList.new } }) + :redirect, } { @!todos.push: Todo.new: :$description; } ``` ```raku -# humming-bird-todo.raku +# examples/App.rakumod +use TodoList; +use HTML::Component; +use HTML::Component::Boilerplate; +unit class App does HTML::Component; + +method RENDER($?) { + boilerplate + :title("My TODO list"), + *.add-child: TodoList.new +} +``` + +```raku +# examples/humming-bird-todo.raku use v6.d; use Humming-Bird::Core; @@ -81,25 +93,26 @@ use HTML::Component::Boilerplate; use lib "examples"; use TodoList; use Todo; +use App; use HTML::Component::Endpoint; -my $index = boilerplate :title("My TODO list"), { .add-child: TodoList.new } +my $index = App.new.RENDER; my $html = $index.HTML; get('/', -> $request, $response { - $response.html($html); + $response.html(App.new.RENDER.HTML); }); for HTML::Component::Endpoint.endpoints { if .verb.uc eq "GET" { get .path, -> $request, $response { - $response.html: .run-defined(Any, |$request.query<>) + $response.html: .run-defined(Any, |$request.query<>).Str; + $response.redirect: $_ with .redirect; } } } listen(12345); - ``` # DESCRIPTION diff --git a/examples/App.rakumod b/examples/App.rakumod new file mode 100644 index 0000000..4d86b33 --- /dev/null +++ b/examples/App.rakumod @@ -0,0 +1,10 @@ +use TodoList; +use HTML::Component; +use HTML::Component::Boilerplate; +unit class App does HTML::Component; + +method RENDER($?) { + boilerplate + :title("My TODO list"), + *.add-child: TodoList.new +} diff --git a/examples/TodoList.rakumod b/examples/TodoList.rakumod index 89c8bea..63bf860 100644 --- a/examples/TodoList.rakumod +++ b/examples/TodoList.rakumod @@ -12,9 +12,7 @@ has Todo @.todos; method RENDER($_) { .ol: { - for @!todos -> Todo $todo { - .add-child: $todo - } + .add-children: @!todos; } .form: :endpoint(self.new-todo), @@ -27,7 +25,7 @@ method RENDER($_) { method new-todo(Str :$description!) is endpoint{ :path, - :return(-> | { boilerplate :title("My TODO list"), { .add-child: TodoList.new } }) + :redirect, } { @!todos.push: Todo.new: :$description; } diff --git a/examples/humming-bird-todo.raku b/examples/humming-bird-todo.raku index 117da86..3086a93 100644 --- a/examples/humming-bird-todo.raku +++ b/examples/humming-bird-todo.raku @@ -5,19 +5,21 @@ use HTML::Component::Boilerplate; use lib "examples"; use TodoList; use Todo; +use App; use HTML::Component::Endpoint; -my $index = boilerplate :title("My TODO list"), { .add-child: TodoList.new } +my $index = App.new.RENDER; my $html = $index.HTML; get('/', -> $request, $response { - $response.html($html); + $response.html(App.new.RENDER.HTML); }); for HTML::Component::Endpoint.endpoints { if .verb.uc eq "GET" { get .path, -> $request, $response { - $response.html: .run-defined(Any, |$request.query<>) + $response.html: .run-defined(Any, |$request.query<>).Str; + $response.redirect: $_ with .redirect; } } } diff --git a/lib/HTML/Component.rakumod b/lib/HTML/Component.rakumod index 5151feb..5121dc9 100644 --- a/lib/HTML/Component.rakumod +++ b/lib/HTML/Component.rakumod @@ -5,28 +5,8 @@ sub html(|c) is export { ::("HTML::Component::Tag::HTML").new: |c; } -# sub body(&func, *%_ where { .keys.all ~~ BODY.&all-html-attrs.any }) is export { -# my $body = BODY.new: |%_; -# $body.&func; -# $body -# } - -#class SNIPPET does OnOl does OnBody does OnHTML does OnHead does Node {} - -# sub snippet(&func) is export { -# #do given func SNIPPET.new { .children.head } -# func SNIPPET.new # for now -# } - method HTML { my $*HTML-COMPONENT-RENDERING = True; $.RENDER: CALLERS::; Empty } - -# method render-root { -# html -> HTML::Component $root { self.RENDER: $root } -# } -# method render { -# snippet(-> HTML::Components $root { self.RENDER: $root }).RENDER -# } diff --git a/lib/HTML/Component/Endpoint.rakumod b/lib/HTML/Component/Endpoint.rakumod index ecc7e87..609df33 100644 --- a/lib/HTML/Component/Endpoint.rakumod +++ b/lib/HTML/Component/Endpoint.rakumod @@ -13,6 +13,7 @@ has $.load-meth = "LOAD"; has Bool $.undefined = &!method.signature.params.first.modifier ne ":D"; has Bool $.defined = &!method.signature.params.first.modifier ne ":U"; has &.return = -> :$component, :$method-output { $method-output }; +has $.redirect; submethod TWEAK(|) { @endpoints.push: self @@ -48,4 +49,3 @@ multi trait_mod:(Method $method, :%endpoint (Str() :$path!, |)) is export { nextsame } } - diff --git a/lib/HTML/Component/Tag/Node.rakumod b/lib/HTML/Component/Tag/Node.rakumod index 077d435..424c3fc 100644 --- a/lib/HTML/Component/Tag/Node.rakumod +++ b/lib/HTML/Component/Tag/Node.rakumod @@ -12,6 +12,14 @@ multi method new(&body, *%_) { $obj } +multi method add-children(+@components) { + $.add-child: $_ for @components +} + +multi method add-child(@components) { + $.add-child: $_ for @components +} + multi method add-child(HTML::Component $comp) { @!children.push: $comp; $comp