|
4 | 4 | //
|
5 | 5 |
|
6 | 6 | class AddReleaseYearForm extends React.Component {
|
7 |
| - |
| 7 | + constructor(props) { |
| 8 | + super(props); |
| 9 | + this.state = { |
| 10 | + year: null, |
| 11 | + hasServerError: false, |
| 12 | + validationErrors: [], |
| 13 | + isDisabled: false, |
| 14 | + }; |
| 15 | + this.handleSubmit = this.handleSubmit.bind(this); |
| 16 | + this.handleChange = this.handleChange.bind(this); |
| 17 | + } |
| 18 | + |
| 19 | + generateRange(start, end) { |
| 20 | + return new Array(end - start + 1) |
| 21 | + .fill() |
| 22 | + .map((_, idx) => start + idx); |
| 23 | + } |
| 24 | + |
| 25 | + handleChange(event) { |
| 26 | + event.preventDefault(); |
| 27 | + this.setState({ |
| 28 | + year: event.target.value |
| 29 | + }); |
| 30 | + } |
| 31 | + |
| 32 | + handleSubmit(event) { |
| 33 | + event.preventDefault(); |
| 34 | + |
| 35 | + this.setState({ |
| 36 | + isDisabled: true, |
| 37 | + hasServerError: false, |
| 38 | + validationErrors: [] |
| 39 | + }); |
| 40 | + |
| 41 | + axios.patch( |
| 42 | + this.props.url, |
| 43 | + [ |
| 44 | + { |
| 45 | + op: 'add', |
| 46 | + path: '/release_year', |
| 47 | + value: this.state.year |
| 48 | + } |
| 49 | + ], |
| 50 | + { |
| 51 | + headers: { |
| 52 | + [this.props.csrfHeaderName]: this.props.csrfTokenValue, |
| 53 | + 'Cache-Control': 'no-store' |
| 54 | + }, |
| 55 | + validateStatus: status => { |
| 56 | + return status == 204 || status == 400; |
| 57 | + } |
| 58 | + } |
| 59 | + ) |
| 60 | + .then(response => { |
| 61 | + const data = response.data; |
| 62 | + if (data.hasOwnProperty('fieldErrors')) { |
| 63 | + const fieldErrors = []; |
| 64 | + if (data.fieldErrors.year) { |
| 65 | + fieldErrors.push(...data.fieldErrors.year); |
| 66 | + } |
| 67 | + this.setState({ |
| 68 | + isDisabled: false, |
| 69 | + validationErrors: fieldErrors |
| 70 | + }); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + // no need to reset the state as page will be reloaded |
| 75 | + window.location.reload(); |
| 76 | + }) |
| 77 | + .catch(error => { |
| 78 | + console.error(error); |
| 79 | + this.setState({ isDisabled: false, hasServerError: true }); |
| 80 | + }); |
| 81 | + } |
| 82 | + |
8 | 83 | render() {
|
| 84 | + const rangeOfYears = this.generateRange( |
| 85 | + this.props.sinceYear, |
| 86 | + this.props.tillYear |
| 87 | + ); |
| 88 | + const hasValidationErrors = this.state.validationErrors.length > 0; |
9 | 89 | return (
|
10 |
| - <div></div> |
11 |
| - ) |
| 90 | + <div className="col-sm-12 form-group"> |
| 91 | + <form className={`form-horizontal ${hasValidationErrors ? 'has-error' : ''}`} onSubmit={this.handleSubmit}> |
| 92 | + <div |
| 93 | + id="add-release-year-failed-msg" |
| 94 | + className={`alert alert-danger text-center col-sm-8 col-sm-offset-2 ${ this.state.hasServerError ? '' : 'hidden' }`}> |
| 95 | + { this.props.l10n['t_server_error'] || 'Server error' } |
| 96 | + </div> |
| 97 | + <div className="form-group form-group-sm"> |
| 98 | + <label className="control-label col-sm-3"> |
| 99 | + { this.props.l10n['t_release_year'] || 'Release year' } |
| 100 | + </label> |
| 101 | + <div className="col-sm-6"> |
| 102 | + <select |
| 103 | + id="release-year" |
| 104 | + name="release-year" |
| 105 | + className="form-control" |
| 106 | + required="required" |
| 107 | + onChange={this.handleChange}> |
| 108 | + <option value=""></option> |
| 109 | + {rangeOfYears.map(year => ( |
| 110 | + <option key={year.toString()} value={year}> |
| 111 | + { year } |
| 112 | + </option> |
| 113 | + ))} |
| 114 | + </select> |
| 115 | + </div> |
| 116 | + </div> |
| 117 | + <div className="col-sm-offset-3 col-sm-4"> |
| 118 | + <span |
| 119 | + id="release-year.errors" |
| 120 | + className={`help-block ${hasValidationErrors ? '' : 'hidden'}`}> |
| 121 | + { this.state.validationErrors.join(', ') } |
| 122 | + </span> |
| 123 | + <button |
| 124 | + type="submit" |
| 125 | + className="btn btn-primary btn-sm" |
| 126 | + disabled={ this.state.isDisabled }> |
| 127 | + { this.props.l10n['t_add'] || 'Add' } |
| 128 | + </button> |
| 129 | + </div> |
| 130 | + </form> |
| 131 | + </div> |
| 132 | + ); |
12 | 133 | }
|
13 | 134 | }
|
0 commit comments