|
4 | 4 | //
|
5 | 5 |
|
6 | 6 | 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 | + } |
8 | 75 | render() {
|
| 76 | + const hasValidationErrors = this.state.validationErrors.length > 0; |
9 | 77 | 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 | + ); |
12 | 116 | }
|
13 | 117 | }
|
0 commit comments