Skip to content

Commit ce64ce9

Browse files
Merge branch 'develop' into add_template_component
2 parents d8f5317 + 8938cf6 commit ce64ce9

File tree

12 files changed

+512
-51
lines changed

12 files changed

+512
-51
lines changed

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

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,55 @@ const componentDef = {
1414
methods: {
1515
perform: function(){
1616
const self = this
17-
axios({
18-
method: self.componentConfig["method"],
19-
url: self.componentConfig["action_path"],
20-
data: self.componentConfig["data"],
21-
headers: {
22-
'X-CSRF-Token': document.getElementsByName("csrf-token")[0].getAttribute('content')
23-
}
24-
})
25-
.then(function(response){
26-
if (self.componentConfig["success"] != undefined && self.componentConfig["success"]["emit"] != undefined) {
27-
matestackEventHub.$emit(self.componentConfig["success"]["emit"], response.data);
28-
}
29-
if (self.componentConfig["success"] != undefined && self.componentConfig["success"]["transition"] != undefined && self.$store != undefined) {
30-
let path = self.componentConfig["success"]["transition"]["path"]
31-
self.$store.dispatch('navigateTo', {url: path, backwards: false})
32-
}
33-
})
34-
.catch(function(error){
35-
if (self.componentConfig["failure"] != undefined && self.componentConfig["failure"]["emit"] != undefined) {
36-
matestackEventHub.$emit(self.componentConfig["failure"]["emit"], error.response.data);
37-
}
38-
if (self.componentConfig["failure"] != undefined && self.componentConfig["failure"]["transition"] != undefined && self.$store != undefined) {
39-
let path = self.componentConfig["failure"]["transition"]["path"]
40-
self.$store.dispatch('navigateTo', {url: path, backwards: false})
41-
}
42-
})
17+
if (
18+
(self.componentConfig["confirm"] == undefined) || confirm(self.componentConfig["confirm_text"])
19+
)
20+
{
21+
axios({
22+
method: self.componentConfig["method"],
23+
url: self.componentConfig["action_path"],
24+
data: self.componentConfig["data"],
25+
headers: {
26+
'X-CSRF-Token': document.getElementsByName("csrf-token")[0].getAttribute('content')
27+
}
28+
})
29+
.then(function(response){
30+
if (self.componentConfig["success"] != undefined && self.componentConfig["success"]["emit"] != undefined) {
31+
matestackEventHub.$emit(self.componentConfig["success"]["emit"], response.data);
32+
}
33+
if (self.componentConfig["success"] != undefined
34+
&& self.componentConfig["success"]["transition"] != undefined
35+
&& (
36+
self.componentConfig["success"]["transition"]["follow_response"] == undefined
37+
||
38+
self.componentConfig["success"]["transition"]["follow_response"] === false
39+
)
40+
&& self.$store != undefined
41+
) {
42+
let path = self.componentConfig["success"]["transition"]["path"]
43+
self.$store.dispatch('navigateTo', {url: path, backwards: false})
44+
return;
45+
}
46+
if (self.componentConfig["success"] != undefined
47+
&& self.componentConfig["success"]["transition"] != undefined
48+
&& self.componentConfig["success"]["transition"]["follow_response"] === true
49+
&& self.$store != undefined
50+
) {
51+
let path = response.data["transition_to"] || response.request.responseURL;
52+
self.$store.dispatch('navigateTo', {url: path, backwards: false});
53+
return;
54+
}
55+
})
56+
.catch(function(error){
57+
if (self.componentConfig["failure"] != undefined && self.componentConfig["failure"]["emit"] != undefined) {
58+
matestackEventHub.$emit(self.componentConfig["failure"]["emit"], error.response.data);
59+
}
60+
if (self.componentConfig["failure"] != undefined && self.componentConfig["failure"]["transition"] != undefined && self.$store != undefined) {
61+
let path = self.componentConfig["failure"]["transition"]["path"]
62+
self.$store.dispatch('navigateTo', {url: path, backwards: false})
63+
}
64+
})
65+
}
4366
}
4467
}
4568
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ def setup
1919
if options[:notify].nil?
2020
@component_config[:notify] = true
2121
end
22+
if @component_config[:confirm] = options[:confirm]
23+
@component_config[:confirm_text] = options[:confirm].try(:[], :text) || "Are you sure?"
24+
end
2225
end
2326

2427
def action_path
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
%dialog{@tag_attributes}
2+
- if options[:text].blank? && block_given?
3+
= yield
4+
- else
5+
= options[:text]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module Matestack::Ui::Core::Dialog
2+
class Dialog < Matestack::Ui::Core::Component::Static
3+
def setup
4+
@tag_attributes.merge!({
5+
"open": options[:open] ||= nil
6+
})
7+
end
8+
end
9+
end

docs/components/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ You can build your [own components](/docs/extend/README.md) as well, both static
2525
- [dl](/docs/components/dl.md)
2626
- dd
2727
- dt
28+
- [dialog](/docs/components/dialog.md)
2829
- [div](/docs/components/div.md)
2930
- [em](/docs/components/em.md)
3031
- [fieldset](/docs/components/fieldset.md)

docs/components/action.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,22 @@ data: {
3838
}
3939
```
4040

41+
### Confirm
42+
43+
When specified, a [browser-native confirm dialog](https://developer.mozilla.org/en-US/docs/Web/API/Window/confirm) is shown before the action is actually performed. The action only is performed after the user confirms. The action is not performed if the user declines to confirm dialog.
44+
45+
```ruby
46+
confirm: {
47+
text: "Do you really want to delete this item?"
48+
}
49+
```
50+
51+
If no `text` is given, the default text "Are you sure?" will be used.
52+
53+
```ruby
54+
confirm: true
55+
```
56+
4157
### Success
4258

4359
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.
@@ -62,6 +78,33 @@ success: {
6278
}
6379
```
6480

81+
When the server redirects to a url, for example after creating a new record, the transition needs to be configured to follow this redirect of the server response.
82+
83+
```ruby
84+
success: {
85+
emit: 'my_action_success',
86+
transition: {
87+
follow_response: true
88+
}
89+
}
90+
```
91+
92+
A controller action that would create a record and then respond with the url the page should transition to, could look like this:
93+
94+
```ruby
95+
class TestModelsController < ApplicationController
96+
include Matestack::Ui::Core::ApplicationHelper
97+
98+
def create
99+
@test_model = TestModel.create(test_model_params)
100+
101+
render json: {
102+
transition_to: test_model_path(@test_model)
103+
}, status: :ok
104+
end
105+
end
106+
```
107+
65108
### Failure
66109

67110
As counterpart to the success part of the action component, there is also the possibility to define the failure behavior. This is what gets triggered after the response to our action returns a failure code, usually in the range of `400` or `500` HTTP status codes.

docs/components/dialog.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# matestack core component: Dialog
2+
3+
Show [specs](/spec/usage/components/dialog_spec.rb)
4+
5+
The HTML `<dialog>` tag implemented in Ruby.
6+
7+
## Parameters
8+
9+
This component can take 3 optional configuration params and either yield content or display what gets passed to the `text` configuration param.
10+
11+
#### # id (optional)
12+
Expects a string with all ids the `<dialog>` should have.
13+
14+
#### # class (optional)
15+
Expects a string with all classes the `<dialog>` should have.
16+
17+
#### # open (optional)
18+
Expects a boolean. If set to true, the `<dialog>` will be visible.
19+
20+
## Example 1: Yield a given block
21+
22+
```ruby
23+
dialog id: 'foo', class: 'bar' do
24+
plain 'Dialog example 1' # optional content
25+
end
26+
```
27+
28+
returns
29+
30+
```html
31+
<dialog id="foo" class="bar">
32+
Dialog example 1
33+
</dialog>
34+
```
35+
36+
## Example 2: Render `options[:text]` param
37+
38+
```ruby
39+
dialog id: 'foo', class: 'bar', text: 'Dialog example 2', open: true
40+
```
41+
42+
returns
43+
44+
```html
45+
<dialog id="foo" class="bar" open="open">
46+
Dialog example 2
47+
</dialog>
48+
```

lib/generators/matestack/core/component/templates/docs/components/%file_name%.md.tt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,4 @@ returns
4242
<<%= file_name.underscore %> id="foo" class="bar">
4343
<%= file_name.camelcase %> example 2
4444
</<%= file_name.underscore %>>
45+
```

0 commit comments

Comments
 (0)