|
| 1 | +// |
| 2 | +// IMPORTANT: |
| 3 | +// You must update ResourceUrl.RESOURCES_VERSION each time whenever you're modified this file! |
| 4 | +// |
| 5 | + |
| 6 | +class AddCatalogNumbersForm extends React.Component { |
| 7 | + constructor(props) { |
| 8 | + super(props); |
| 9 | + this.state = { |
| 10 | + numbers: null, |
| 11 | + catalog: 'michel', |
| 12 | + hasServerError: false, |
| 13 | + validationErrors: [], |
| 14 | + isDisabled: false |
| 15 | + }; |
| 16 | + this.handleSubmit = this.handleSubmit.bind(this); |
| 17 | + this.handleChangeNumbers = this.handleChangeNumbers.bind(this); |
| 18 | + this.handleChangeCatalog = this.handleChangeCatalog.bind(this); |
| 19 | + } |
| 20 | + |
| 21 | + handleChangeNumbers(event) { |
| 22 | + event.preventDefault(); |
| 23 | + this.setState({ |
| 24 | + numbers: event.target.value |
| 25 | + }); |
| 26 | + } |
| 27 | + |
| 28 | + handleChangeCatalog(event) { |
| 29 | + event.preventDefault(); |
| 30 | + this.setState({ |
| 31 | + catalog: event.target.value |
| 32 | + }); |
| 33 | + } |
| 34 | + |
| 35 | + handleSubmit(event) { |
| 36 | + event.preventDefault(); |
| 37 | + |
| 38 | + this.setState({ |
| 39 | + isDisabled: true, |
| 40 | + hasServerError: false, |
| 41 | + validationErrors: [] |
| 42 | + }); |
| 43 | + |
| 44 | + axios.patch( |
| 45 | + this.props.url, |
| 46 | + [ |
| 47 | + { |
| 48 | + op: 'add', |
| 49 | + path: `/${this.state.catalog}_numbers`, |
| 50 | + value: this.state.numbers.split(',') |
| 51 | + } |
| 52 | + ], |
| 53 | + { |
| 54 | + headers: { |
| 55 | + [this.props.csrfHeaderName]: this.props.csrfTokenValue, |
| 56 | + 'Cache-Control': 'no-store' |
| 57 | + }, |
| 58 | + validateStatus: status => { |
| 59 | + return status == 204 || status == 400; |
| 60 | + } |
| 61 | + } |
| 62 | + ) |
| 63 | + .then(response => { |
| 64 | + const data = response.data; |
| 65 | + if (data.hasOwnProperty('fieldErrors')) { |
| 66 | + const fieldErrors = []; |
| 67 | + if (data.fieldErrors.numbers) { |
| 68 | + fieldErrors.push(...data.fieldErrors.numbers); |
| 69 | + } |
| 70 | + |
| 71 | + this.setState({ |
| 72 | + isDisabled: false, |
| 73 | + validationErrors: fieldErrors |
| 74 | + }); |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + // no need to reset the state as page will be reloaded |
| 79 | + window.location.reload(); |
| 80 | + }) |
| 81 | + .catch(error => { |
| 82 | + console.error(error); |
| 83 | + this.setState({ isDisabled: false, hasServerError: true }); |
| 84 | + }); |
| 85 | + } |
| 86 | + render() { |
| 87 | + const hasValidationErrors = this.state.validationErrors.length > 0; |
| 88 | + return ( |
| 89 | + <div className="col-sm-12 form-group"> |
| 90 | + <form className={`form-horizontal ${hasValidationErrors ? 'has-error' : ''}`} onSubmit={this.handleSubmit}> |
| 91 | + <div |
| 92 | + id="add-catalog-numbers-failed-msg" |
| 93 | + className={`alert alert-danger text-center col-sm-8 col-sm-offset-2 ${this.state.hasServerError ? '' : 'hidden'}`}> |
| 94 | + { this.props.l10n['t_server_error'] || 'Server error' } |
| 95 | + </div> |
| 96 | + <div className="form-group form-group-sm"> |
| 97 | + <label className="control-label col-sm-3"> |
| 98 | + { this.props.l10n['t_catalog'] || 'Catalog' } |
| 99 | + </label> |
| 100 | + <div className="col-sm-6"> |
| 101 | + <select |
| 102 | + id="catalog-name" |
| 103 | + name="catalogName" |
| 104 | + className="form-control" |
| 105 | + onChange={this.handleChangeCatalog}> |
| 106 | + <option value="michel"> |
| 107 | + { this.props.l10n['t_michel'] || 'Michel' } |
| 108 | + </option> |
| 109 | + <option value="scott"> |
| 110 | + { this.props.l10n['t_scott'] || 'Scott' } |
| 111 | + </option> |
| 112 | + <option value="yvert"> |
| 113 | + { this.props.l10n['t_yvert'] || 'Yvert et Tellier' } |
| 114 | + </option> |
| 115 | + <option value="gibbons"> |
| 116 | + { this.props.l10n['t_sg'] || 'Stanley Gibbons' } |
| 117 | + </option> |
| 118 | + <option value="solovyov"> |
| 119 | + { this.props.l10n['t_solovyov'] || 'Solovyov' } |
| 120 | + </option> |
| 121 | + <option value="zagorski"> |
| 122 | + { this.props.l10n['t_zagorski'] || 'Zagorski' } |
| 123 | + </option> |
| 124 | + </select> |
| 125 | + </div> |
| 126 | + </div> |
| 127 | + <div className="form-group form-group-sm"> |
| 128 | + <label className="control-label col-sm-3"> |
| 129 | + { this.props.l10n['t_numbers'] || 'Numbers' } |
| 130 | + </label> |
| 131 | + <div className="row"> |
| 132 | + <div className="col-sm-6"> |
| 133 | + <input |
| 134 | + id="catalog-numbers" |
| 135 | + type="text" |
| 136 | + className="form-control" |
| 137 | + size="5" |
| 138 | + required="required" |
| 139 | + onChange={ this.handleChangeNumbers } /> |
| 140 | + </div> |
| 141 | + </div> |
| 142 | + </div> |
| 143 | + <div className="col-sm-offset-3 col-sm-4"> |
| 144 | + <span |
| 145 | + id="catalog-numbers.errors" |
| 146 | + className={`help-block ${hasValidationErrors ? '' : 'hidden'}`}> |
| 147 | + { this.state.validationErrors.join(', ') } |
| 148 | + </span> |
| 149 | + <button |
| 150 | + type="submit" |
| 151 | + className="btn btn-primary btn-sm" |
| 152 | + disabled={ this.state.isDisabled }> |
| 153 | + { this.props.l10n['t_add'] || 'Add' } |
| 154 | + </button> |
| 155 | + </div> |
| 156 | + </form> |
| 157 | + </div> |
| 158 | + ); |
| 159 | + } |
| 160 | +} |
0 commit comments