|
1 | 1 | # Action Component API |
2 | 2 |
|
3 | | -The action component allows us to trigger async HTTP requests without Javascript! |
| 3 | +The `action` component can be used to trigger asynchronous requests from - for example a button click - or any other html markup. The `action` components let's us wrap content in an `a` tag which is then clickable and triggers a background request with the configured request method to the configured path and with optionally given params and let's us react to the server response. It can distinguish between a successful and failed response and emit events, transition somewhere, completely redirect and more. You only need to configure it according to your needs. |
| 4 | + |
| 5 | +```ruby |
| 6 | +def response |
| 7 | + action action_config do |
| 8 | + button 'Delete' |
| 9 | + end |
| 10 | +end |
| 11 | + |
| 12 | +def action_config |
| 13 | + { |
| 14 | + path: product_path(product), |
| 15 | + method: :delete, |
| 16 | + sucess: { |
| 17 | + transition: { |
| 18 | + follow_response: true |
| 19 | + } |
| 20 | + }, |
| 21 | + failure: { |
| 22 | + emit: 'deletion-failed' |
| 23 | + } |
| 24 | + } |
| 25 | +end |
| 26 | +``` |
4 | 27 |
|
5 | 28 | ## Parameters |
6 | 29 |
|
|
117 | 140 |
|
118 | 141 | #### Perform redirect |
119 | 142 |
|
120 | | -We can also perform a redirect \(full page load\) that only gets triggered on success and also accepts further params: |
| 143 | +We can also perform a redirect (full page load) that only gets triggered on success and also accepts further params: |
121 | 144 |
|
122 | | -Please be aware, that emiting a event doen't have an effect when performing a redirect instead of a transition, as the whole page \(including the surrounding app\) gets reloaded! |
| 145 | +Please be aware, that emiting a event doen't have an effect when performing a redirect instead of a transition, as the whole page (including the surrounding app) gets reloaded! |
123 | 146 |
|
124 | 147 | ```ruby |
125 | 148 | success: { |
@@ -341,7 +364,7 @@ class ExamplePage < Matestack::Ui::Page |
341 | 364 | end |
342 | 365 | ``` |
343 | 366 |
|
344 | | -Now, if we click the button and everything goes well \(which should be the case in this very simple example\), we can see the timestamp gets updated - nice! |
| 367 | +Now, if we click the button and everything goes well (which should be the case in this very simple example), we can see the timestamp gets updated - nice! |
345 | 368 |
|
346 | 369 | #### Async request with success event emit used for notification |
347 | 370 |
|
@@ -378,7 +401,7 @@ This time, after clicking our action component we should see the `good job!` mes |
378 | 401 |
|
379 | 402 | #### Async request with failure event emit used for notification |
380 | 403 |
|
381 | | -In the examples before, we always assumed \(and made sure\) that things went well. Now, it's the first time to use the `failure_action_test_path` to see how we can notify the user if things go wrong! |
| 404 | +In the examples before, we always assumed (and made sure) that things went well. Now, it's the first time to use the `failure_action_test_path` to see how we can notify the user if things go wrong! |
382 | 405 |
|
383 | 406 | ```ruby |
384 | 407 | class ExamplePage < Matestack::Ui::Page |
|
532 | 555 |
|
533 | 556 | Now, we can visit `localhost:3000/action_test/page1` and see our first page, shown by the `This is Page 1` text. |
534 | 557 |
|
535 | | -There, we can click on our button \(`Click me!`\) and get transfered to the second page. There, we see the `This is Page 2` text and, for 300ms, our `server says: good job!` success message. Neat! |
| 558 | +There, we can click on our button (`Click me!`) and get transfered to the second page. There, we see the `This is Page 2` text and, for 300ms, our `server says: good job!` success message. Neat! |
536 | 559 |
|
537 | | -If we click the button \(`Click me!`\) on the second page, we get the failure message \(`server says: something went wrong!`\) and get sent back to page 2, just as we wanted to. |
| 560 | +If we click the button (`Click me!`) on the second page, we get the failure message (`server says: something went wrong!`) and get sent back to page 2, just as we wanted to. |
0 commit comments