Skip to content

Commit 660e616

Browse files
committed
task: implement AddCommentForm
Fix #1338
1 parent f0500d9 commit 660e616

File tree

2 files changed

+150
-3
lines changed

2 files changed

+150
-3
lines changed

src/main/frontend/src/components/AddCommentForm.js

+107-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,114 @@
44
//
55

66
class AddCommentForm extends React.Component {
7-
7+
constructor(props) {
8+
super(props);
9+
this.state = {
10+
comment: '',
11+
validationErrors: [],
12+
hasServerError: false,
13+
isDisabled: false
14+
};
15+
this.handleSubmit = this.handleSubmit.bind(this);
16+
this.handleChange = this.handleChange.bind(this);
17+
}
18+
19+
handleChange(event) {
20+
event.preventDefault();
21+
this.setState({
22+
comment: event.target.value
23+
});
24+
}
25+
26+
handleSubmit(event) {
27+
event.preventDefault();
28+
29+
this.setState({
30+
isDisabled: true,
31+
hasServerError: false,
32+
validationErrors: []
33+
});
34+
35+
axios.patch(
36+
this.props.url, [
37+
{
38+
op: 'add',
39+
path: '/comment',
40+
value: this.state.comment
41+
}
42+
],
43+
{
44+
headers: {
45+
[this.props.csrfHeaderName]: this.props.csrfTokenValue,
46+
'Cache-Control': 'no-store'
47+
},
48+
validateStatus: status => {
49+
return status == 204 || status == 400;
50+
}
51+
})
52+
.then(response => {
53+
const data = response.data;
54+
55+
if (data.hasOwnProperty('fieldErrors')) {
56+
const fieldErrors = [];
57+
if (data.fieldErrors.comment) {
58+
fieldErrors.push(...data.fieldErrors.comment);
59+
}
60+
this.setState({
61+
isDisabled: false,
62+
validationErrors: fieldErrors
63+
});
64+
return;
65+
}
66+
67+
// no need to reset the state as page will be reloaded
68+
window.location.reload();
69+
})
70+
.catch(error => {
71+
console.error(error);
72+
this.setState({ isDisabled: false, hasServerError: true });
73+
});
74+
}
875
render() {
76+
const hasValidationErrors = this.state.validationErrors.length > 0;
977
return (
10-
<div></div>
11-
)
78+
<div className="col-sm-12 form-group">
79+
<form className={`form-horizontal ${hasValidationErrors ? 'has-error' : ''}`} onSubmit={this.handleSubmit}>
80+
<div
81+
id="add-comment-failed-msg"
82+
className={`alert alert-danger text-center col-sm-8 col-sm-offset-2 ${this.state.hasServerError ? '' : 'hidden'}`}>
83+
{ this.props.l10n['t_server_error'] || 'Server error' }
84+
</div>
85+
<div className="form-group form-group-sm">
86+
<label className="control-label col-sm-3">
87+
{ this.props.l10n['t_comment'] || 'Comment' }
88+
</label>
89+
<div className="col-sm-6">
90+
<textarea
91+
id="comment"
92+
className="form-control"
93+
cols="22"
94+
rows="3"
95+
required="required"
96+
onChange={this.handleChange}>
97+
</textarea>
98+
</div>
99+
</div>
100+
<div className="col-sm-offset-3 col-sm-4">
101+
<span
102+
id="comment.errors"
103+
className={`help-block ${hasValidationErrors ? '' : 'hidden'}`}>
104+
{ this.state.validationErrors.join(', ') }
105+
</span>
106+
<button
107+
type="submit"
108+
className="btn btn-primary btn-sm"
109+
disabled={ this.state.isDisabled }>
110+
{ this.props.l10n['t_add'] || 'Add' }
111+
</button>
112+
</div>
113+
</form>
114+
</div>
115+
);
12116
}
13117
}

src/main/webapp/WEB-INF/views/series/info.html

+43
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,49 @@ <h5 th:text="#{t_add_info_who_selling_series}">Add info about selling/buying thi
894894
'altCurrency' : 'CZK'
895895
}
896896
}
897+
}
898+
};
899+
var stubResponse;
900+
901+
switch (outcome) {
902+
case 'success':
903+
case 'failOnField':
904+
stubResponse = possibleResponses[url][outcome];
905+
break;
906+
case 'failOnForm':
907+
default:
908+
stubResponse = {
909+
status: 500,
910+
statusText: 'Fake Server Error'
911+
};
912+
}
913+
914+
responseCount++;
915+
916+
return new Promise(function delayExecution(resolve) {
917+
setTimeout(resolve, 500 /* 0.5 second */);
918+
919+
}).then(function returnResponse() {
920+
return stubResponse.status == 500 ? Promise.reject(stubResponse) : Promise.resolve(stubResponse);
921+
});
922+
},
923+
patch: function (url) {
924+
var possibleOutcomes = ['failOnForm', 'failOnField', 'success'];
925+
var outcome = possibleOutcomes[responseCount % possibleOutcomes.length];
926+
var possibleResponses = {
927+
'/series/100': {
928+
'failOnField': {
929+
status: 400,
930+
data: {
931+
'fieldErrors': {
932+
'comment': ['Comment error'],
933+
}
934+
}
935+
},
936+
'success': {
937+
status: 200,
938+
data: {}
939+
}
897940
}
898941
};
899942
var stubResponse;

0 commit comments

Comments
 (0)