Skip to content

Commit 559c1f5

Browse files
committed
refactored event and delay utilities on action, async, form and transition, addeds specs and docs
1 parent 8b25f8c commit 559c1f5

File tree

16 files changed

+580
-41
lines changed

16 files changed

+580
-41
lines changed

app/concepts/matestack/ui/core/action/action.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ const componentDef = {
2020
if (self.componentConfig["emit"] != undefined) {
2121
matestackEventHub.$emit(self.componentConfig["emit"]);
2222
}
23-
if (self.componentConfig["min_defer"] != undefined) {
23+
if (self.componentConfig["delay"] != undefined) {
2424
setTimeout(function () {
2525
self.sendRequest()
26-
}, parseInt(self.componentConfig["min_defer"]));
26+
}, parseInt(self.componentConfig["delay"]));
2727
} else {
2828
this.sendRequest()
2929
}

app/concepts/matestack/ui/core/form/form.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,10 @@ const componentDef = {
108108
if (self.componentConfig["emit"] != undefined) {
109109
matestackEventHub.$emit(self.componentConfig["emit"]);
110110
}
111-
if (self.componentConfig["min_defer"] != undefined) {
111+
if (self.componentConfig["delay"] != undefined) {
112112
setTimeout(function () {
113113
self.sendRequest()
114-
}, parseInt(self.componentConfig["min_defer"]));
114+
}, parseInt(self.componentConfig["delay"]));
115115
} else {
116116
this.sendRequest()
117117
}

app/concepts/matestack/ui/core/transition/transition.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,11 @@ const componentDef = {
1616
methods: {
1717
navigateTo: function(url){
1818
const self = this
19-
if (self.componentConfig["emit"] != undefined) {
20-
matestackEventHub.$emit(self.componentConfig["emit"]);
21-
}
22-
if (self.componentConfig["min_defer"] != undefined) {
19+
matestackEventHub.$emit("page_loading_triggered", url);
20+
if (self.componentConfig["delay"] != undefined) {
2321
setTimeout(function () {
2422
self.performNavigation(url)
25-
}, parseInt(self.componentConfig["min_defer"]));
23+
}, parseInt(self.componentConfig["delay"]));
2624
} else {
2725
this.performNavigation(url)
2826
}

docs/components/action.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,22 @@ If no `text` is given, the default text "Are you sure?" will be used.
5454
confirm: true
5555
```
5656

57+
### Emit
58+
59+
This event gets emitted right after triggering the action. In contrast to the `sucsess` or `failure` events, it will be emitted regardless of the server response.
60+
61+
```ruby
62+
emit: "action_submitted"
63+
```
64+
65+
### Delay
66+
67+
You can use this attribute if you want to delay the actual action submit request. It will not delay the event specified with the `emit` attribute.
68+
69+
```ruby
70+
delay: 1000 # means 1000 ms
71+
```
72+
5773
### Success
5874

5975
The success part of the action component gets triggered once the action we wanted to perform returns a success code, usually the `200` HTTP status code.

docs/components/async.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ Lazy (or defered) loading can be configured like shown [here](#defer).
2828

2929
If you want to simply hide the async component on initial pageload and display it later on, read below how to use `show_on`, which can be combined with `rerender_on`.
3030

31+
You can pass in multiple, comma-separated events on which the component should rerender.
32+
33+
```ruby
34+
async rerender_on: 'my_event, some_other_event'
35+
```
36+
3137
### Show_on
3238

3339
The `show_on` option lets us define an event on which the component gets shown. The content is still rendered on init pageload, but simply hidden in the browser until the event is emitted. If you want to have proper deferred loading, please refer to [defer](#defer)
@@ -40,6 +46,12 @@ async show_on: 'my_event' do
4046
end
4147
```
4248

49+
You can pass in multiple, comma-separated events on which the component should be shown.
50+
51+
```ruby
52+
async show_on: 'my_event, some_other_event'
53+
```
54+
4355
### Hide_on
4456

4557
The `hide_on` option lets us define an event on which the component gets hidden.
@@ -52,6 +64,13 @@ async hide_on: 'my_event' do
5264
end
5365
```
5466

67+
You can pass in multiple, comma-separated events on which the component should be hidden.
68+
69+
```ruby
70+
async hide_on: 'my_event, some_other_event'
71+
```
72+
73+
5574
### Hide_after
5675

5776
The `hide_after` option lets us define a timespan in milliseconds after which the component gets hidden.

docs/components/form.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,21 @@ Using the standard Rails params, we can pass information to our route!
106106
params: { id: 42 }
107107
```
108108

109+
### Emit
110+
111+
This event gets emitted right after form submit. In contrast to the `sucsess` or `failure` events, it will be emitted regardless of the server response.
112+
113+
```ruby
114+
emit: "form_submitted"
115+
```
116+
117+
### Delay
118+
119+
You can use this attribute if you want to delay the actual form submit request. It will not delay the event specified with the `emit` attribute.
120+
121+
```ruby
122+
delay: 1000 # means 1000 ms
123+
```
109124

110125
### Success
111126

@@ -1348,4 +1363,4 @@ datalist id: 'datalist-id' do
13481363
option value: 10
13491364
option value: 20
13501365
end
1351-
```
1366+
```

docs/components/transition.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,27 @@ transition path: :page1_path, text: 'Click me for a transition'
3030

3131
If no text is present, the transition component expects a block that it then *yields* the usual way.
3232

33+
34+
### Delay
35+
36+
You can use this attribute if you want to delay the actual transition. It will not delay the `page_loading_triggered` event
37+
38+
```ruby
39+
delay: 1000 # means 1000 ms
40+
```
41+
42+
### Events
43+
44+
The `transition` component automatically emits events on:
45+
46+
* transition triggered by user action -> "page_loading_triggered"
47+
* *optional client side delay via `delay` attribute*
48+
* start to get new page from server -> "page_loading"
49+
* *server side/network delay*
50+
* successfully received new page from server -> "page_loaded"
51+
* failed to receive new page from server -> "page_loading_error"
52+
53+
3354
## Examples
3455

3556
The transition core component renders the HTML `<a>` tag and performs a page transition

spec/support/test_controller.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class TestController < ActionController::Base
2+
3+
before_action :check_params
4+
5+
def check_params
6+
expect_params(params.permit!.to_h)
7+
end
8+
9+
def expect_params(params)
10+
end
11+
12+
end

spec/usage/components/action/delay.rb

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
require_relative "../../../support/utils"
2+
require_relative "../../../support/test_controller"
3+
include Utils
4+
5+
describe "Action Component", type: :feature, js: true do
6+
7+
before :each do
8+
9+
class ActionTestController < TestController
10+
11+
def test
12+
receive_timestamp = DateTime.now.strftime('%Q')
13+
render json: { received_at: receive_timestamp }, status: 200
14+
end
15+
16+
end
17+
18+
allow_any_instance_of(ActionTestController).to receive(:expect_params)
19+
20+
end
21+
22+
describe "delay attribute" do
23+
24+
it "if set, delays action submit" do
25+
26+
Rails.application.routes.append do
27+
post '/action_test', to: 'action_test#test', as: 'action_delay_test'
28+
end
29+
Rails.application.reload_routes!
30+
31+
32+
class ExamplePage < Matestack::Ui::Page
33+
34+
def response
35+
components {
36+
action action_config do
37+
button text: "Click me!"
38+
end
39+
div id: "timestamp" do
40+
async show_on: "action_submitted_successfully" do
41+
plain "{{event.data.received_at}}"
42+
end
43+
end
44+
}
45+
end
46+
47+
def action_config
48+
return {
49+
method: :post,
50+
path: :action_delay_test_path,
51+
data: {
52+
foo: DateTime.now.strftime('%Q')
53+
},
54+
delay: 1000,
55+
success: {
56+
emit: "action_submitted_successfully"
57+
}
58+
}
59+
end
60+
61+
end
62+
63+
visit "/example"
64+
65+
submit_timestamp = DateTime.now.strftime('%Q').to_i
66+
click_button "Click me!"
67+
68+
sleep 1.5
69+
70+
element = page.find("#timestamp")
71+
receive_timestamp = element.text.to_i
72+
73+
expect(receive_timestamp - submit_timestamp).to be > 1000
74+
75+
end
76+
77+
end
78+
79+
end

spec/usage/components/action/emit.rb

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
require_relative "../../../support/utils"
2+
require_relative "../../../support/test_controller"
3+
include Utils
4+
5+
describe "Action Component", type: :feature, js: true do
6+
7+
before :each do
8+
9+
class ActionTestController < TestController
10+
11+
def test
12+
render json: { }, status: 200
13+
end
14+
15+
end
16+
17+
allow_any_instance_of(ActionTestController).to receive(:expect_params)
18+
19+
end
20+
21+
describe "emit attribute" do
22+
23+
it "if set, emits event directly when action is submitted (not waiting for success or failure)" do
24+
25+
Rails.application.routes.append do
26+
post '/action_test', to: 'action_test#test', as: 'action_emit'
27+
end
28+
Rails.application.reload_routes!
29+
30+
31+
class ExamplePage < Matestack::Ui::Page
32+
33+
def response
34+
components {
35+
action action_config do
36+
button text: "Click me!"
37+
end
38+
async show_on: "action_submitted" do
39+
plain "action submitted!"
40+
end
41+
}
42+
end
43+
44+
def action_config
45+
return {
46+
method: :post,
47+
path: :action_emit_path,
48+
data: {
49+
foo: "bar"
50+
},
51+
emit: "action_submitted"
52+
}
53+
end
54+
55+
end
56+
57+
visit "/example"
58+
59+
expect_any_instance_of(ActionTestController).to receive(:expect_params)
60+
.with(hash_including(:foo => "bar"))
61+
62+
click_button "Click me!"
63+
64+
expect(page).to have_content("action submitted!")
65+
end
66+
67+
end
68+
69+
end

0 commit comments

Comments
 (0)