Skip to content

Commit be20d08

Browse files
AlexKValjustin808
authored andcommitted
Show errors on the "Simple React" page. (shakacode#321)
* Show details for errors on the "Simple React" page. * Only for "Horizontal Form" and "Stacked Form" * Fix. Clean error alert up after a successful submission. * Add validation styles for input elements * Fix error propagation for "React Demo" page * And resolve eslint warnings because of Eslint (shakacode#320) * Add Capybara specs
1 parent d34c26d commit be20d08

File tree

5 files changed

+65
-6
lines changed

5 files changed

+65
-6
lines changed

client/app/bundles/comments/actions/commentsActionCreators.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export function fetchComments() {
4848
requestsManager
4949
.fetchEntities()
5050
.then(res => dispatch(fetchCommentsSuccess(res.data)))
51-
.catch(res => dispatch(fetchCommentsFailure(res.data)))
51+
.catch(error => dispatch(fetchCommentsFailure(error)))
5252
);
5353
};
5454
}
@@ -60,7 +60,7 @@ export function submitComment(comment) {
6060
requestsManager
6161
.submitEntity({ comment })
6262
.then(res => dispatch(submitCommentSuccess(res.data)))
63-
.catch(res => dispatch(submitCommentFailure(res.data)))
63+
.catch(error => dispatch(submitCommentFailure(error)))
6464
);
6565
};
6666
}

client/app/bundles/comments/components/CommentBox/CommentForm/CommentForm.jsx

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ import BaseComponent from 'libs/components/BaseComponent';
1313
const emptyComment = { author: '', text: '' };
1414
const textPlaceholder = 'Say something using markdown...';
1515

16+
function bsStyleFor(propName, error) {
17+
if (error) {
18+
const errorData = (error && error.response && error.response.data) || {};
19+
return (propName in errorData) ? 'error' : 'success';
20+
}
21+
22+
return null;
23+
}
24+
1625
export default class CommentForm extends BaseComponent {
1726
static propTypes = {
1827
isSaving: PropTypes.bool.isRequired,
@@ -119,6 +128,8 @@ export default class CommentForm extends BaseComponent {
119128
value={this.state.comment.author}
120129
onChange={this.handleChange}
121130
disabled={this.props.isSaving}
131+
hasFeedback
132+
bsStyle={bsStyleFor('author', this.props.error)}
122133
/>
123134
<Input
124135
type="textarea"
@@ -130,6 +141,8 @@ export default class CommentForm extends BaseComponent {
130141
value={this.state.comment.text}
131142
onChange={this.handleChange}
132143
disabled={this.props.isSaving}
144+
hasFeedback
145+
bsStyle={bsStyleFor('text', this.props.error)}
133146
/>
134147
<div className="form-group">
135148
<div className="col-sm-offset-2 col-sm-10">
@@ -159,6 +172,8 @@ export default class CommentForm extends BaseComponent {
159172
value={this.state.comment.author}
160173
onChange={this.handleChange}
161174
disabled={this.props.isSaving}
175+
hasFeedback
176+
bsStyle={bsStyleFor('author', this.props.error)}
162177
/>
163178
<Input
164179
type="textarea"
@@ -168,6 +183,8 @@ export default class CommentForm extends BaseComponent {
168183
value={this.state.comment.text}
169184
onChange={this.handleChange}
170185
disabled={this.props.isSaving}
186+
hasFeedback
187+
bsStyle={bsStyleFor('text', this.props.error)}
171188
/>
172189
<input
173190
type="submit"
@@ -225,12 +242,23 @@ export default class CommentForm extends BaseComponent {
225242
}
226243

227244
errorWarning() {
245+
const error = this.props.error;
246+
228247
// If there is no error, there is nothing to add to the DOM
229-
if (!this.props.error) return null;
248+
if (!error) return null;
249+
250+
const errorData = error.response && error.response.data;
251+
252+
const errorElements = _.transform(errorData, (result, errorText, errorFor) => {
253+
result.push(<li key={errorFor}><b>{_.upperFirst(errorFor)}:</b> {errorText}</li>);
254+
}, []);
255+
230256
return (
231257
<Alert bsStyle="danger" key="commentSubmissionError">
232-
<strong>Your comment was not saved! </strong>
233-
A server error prevented your comment from being saved. Please try again.
258+
<strong>Your comment was not saved!</strong>
259+
<ul>
260+
{errorElements}
261+
</ul>
234262
</Alert>
235263
);
236264
}

client/app/bundles/comments/components/SimpleCommentScreen/SimpleCommentScreen.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export default class SimpleCommentScreen extends BaseComponent {
5555

5656
this.setState({
5757
$$comments: $$comments.unshift($$comment),
58+
submitCommentError: null,
5859
isSaving: false,
5960
});
6061
})

spec/features/add_new_comment_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,45 @@
66
context "React Router", page: :main, js: true do
77
context "via Horizontal Form", form: :horizontal do
88
include_examples "New Comment Submission", true
9+
include_examples "Validation errors displaying"
910
end
1011
context "via Inline Form", form: :inline do
1112
include_examples "New Comment Submission", true
13+
include_examples "Validation errors displaying"
1214
end
1315
context "via Stacked Form", form: :stacked do
1416
include_examples "New Comment Submission", true
17+
include_examples "Validation errors displaying"
1518
end
1619
end
1720

1821
context "React/Redux", page: :react_demo, js: true do
1922
context "via Horizontal Form", form: :horizontal do
2023
include_examples "New Comment Submission", true
24+
include_examples "Validation errors displaying"
2125
end
2226
context "via Inline Form", form: :inline do
2327
include_examples "New Comment Submission", true
28+
include_examples "Validation errors displaying"
2429
end
2530
context "via Stacked Form", form: :stacked do
2631
include_examples "New Comment Submission", true
32+
include_examples "Validation errors displaying"
2733
end
2834
end
2935

3036
context "simple page", page: :simple, js: true do
3137
context "via Horizontal Form", form: :horizontal do
3238
include_examples "New Comment Submission", false
39+
include_examples "Validation errors displaying"
3340
end
3441
context "via Inline Form", form: :inline do
3542
include_examples "New Comment Submission", false
43+
include_examples "Validation errors displaying"
3644
end
3745
context "via the Stacked Form", form: :stacked, driver: js_selenium_driver do
3846
include_examples "New Comment Submission", false
47+
include_examples "Validation errors displaying"
3948
end
4049
end
4150

spec/features/shared/examples.rb

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@
1010
scenario "comment is added" do
1111
expect(page).to have_css(".js-comment-author", text: name)
1212
expect(page).to have_css(".js-comment-text", text: text)
13+
expect(page).to have_no_content("Your comment was not saved!")
1314
if expect_comment_count
1415
expect(page).to have_css("#js-comment-count",
1516
text: "Comments: #{Comment.count}")
1617
end
1718
end
1819
end
1920

20-
context "when the new comment is submmited with blank fields", blank_form_submitted: true do
21+
context "when the new comment is submitted with blank fields", blank_form_submitted: true do
2122
let!(:comments_count) { all(".comment").size }
2223

2324
scenario "comment is not added" do
@@ -38,3 +39,23 @@
3839
end
3940
end
4041
end
42+
43+
shared_examples "Validation errors displaying" do
44+
context "when the new comment is submitted with blank fields", blank_form_submitted: true do
45+
scenario "validation errors displayed" do
46+
expect(page).to have_content("Your comment was not saved!")
47+
expect(page).to have_content("Author: can't be blank")
48+
expect(page).to have_content("Text: can't be blank")
49+
50+
# with a successful consequent comment submitted
51+
# the validation warnings should disappear
52+
fill_in "Your Name", with: "Some name"
53+
fill_in "Say something using markdown...", with: "Some text"
54+
click_button "Post"
55+
56+
expect(page).to have_no_content("Your comment was not saved!")
57+
expect(page).to have_no_content("Author: can't be blank")
58+
expect(page).to have_no_content("Text: can't be blank")
59+
end
60+
end
61+
end

0 commit comments

Comments
 (0)