|
3 | 3 | // You must update ResourceUrl.RESOURCES_VERSION each time whenever you're modified this file!
|
4 | 4 | //
|
5 | 5 |
|
| 6 | +// @todo #1057 SeriesSaleImportForm: add tests |
| 7 | +class SeriesSaleImportForm extends React.Component { |
| 8 | + |
| 9 | + constructor(props) { |
| 10 | + super(props); |
| 11 | + this.state = { |
| 12 | + url: '', |
| 13 | + isDisabled: false, |
| 14 | + hasServerError: false, |
| 15 | + validationErrors: [] |
| 16 | + }; |
| 17 | + this.handleSubmit = this.handleSubmit.bind(this); |
| 18 | + this.handleChange = this.handleChange.bind(this); |
| 19 | + } |
| 20 | + |
| 21 | + handleChange(event) { |
| 22 | + event.preventDefault(); |
| 23 | + this.setState({ |
| 24 | + url: event.target.value |
| 25 | + }); |
| 26 | + } |
| 27 | + |
| 28 | + handleSubmit(event) { |
| 29 | + event.preventDefault(); |
| 30 | + |
| 31 | + if (this.state.url == '') { |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + // @todo #1057 SeriesSaleImportForm: wait until setState() finishes |
| 36 | + // (see https://reactjs.org/docs/react-component.html#setstate) |
| 37 | + this.setState({ |
| 38 | + isDisabled: true, |
| 39 | + hasServerError: false, |
| 40 | + validationErrors: [] |
| 41 | + }); |
| 42 | + |
| 43 | + const headers = new Headers(); |
| 44 | + headers.set('Content-Type', 'application/json; charset=UTF-8'); |
| 45 | + headers.set(this.props.csrfHeaderName, this.props.csrfTokenValue); |
| 46 | + |
| 47 | + const request = new Request( |
| 48 | + this.props.url, |
| 49 | + { |
| 50 | + method: 'POST', |
| 51 | + headers, |
| 52 | + body: JSON.stringify({ 'url': this.state.url }), |
| 53 | + cache: 'no-store' |
| 54 | + } |
| 55 | + ); |
| 56 | + |
| 57 | + fetch(request) |
| 58 | + .then(response => { |
| 59 | + if (response.ok || response.status == 400) { |
| 60 | + return response.json(); |
| 61 | + } |
| 62 | + throw new Error(response.statusText); |
| 63 | + }) |
| 64 | + .then(data => { |
| 65 | + if (data.hasOwnProperty('fieldErrors')) { |
| 66 | + this.setState({ |
| 67 | + isDisabled: false, |
| 68 | + validationErrors: data.fieldErrors.url |
| 69 | + }); |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + const today = DateUtils.formatDateToDdMmYyyy(new Date()); |
| 74 | + document.getElementById('date').value = today; |
| 75 | + |
| 76 | + document.getElementById('url').value = this.state.url; |
| 77 | + document.getElementById('price').value = data.price; |
| 78 | + document.getElementById('currency').value = data.currency; |
| 79 | + if (data.sellerId) { |
| 80 | + document.getElementById('seller').value = data.sellerId; |
| 81 | + } |
| 82 | + this.setState({ isDisabled: false, url: '' }); |
| 83 | + }) |
| 84 | + .catch(error => { |
| 85 | + console.error(error); |
| 86 | + this.setState({ |
| 87 | + isDisabled: false, |
| 88 | + hasServerError: true |
| 89 | + }); |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | + render() { |
| 94 | + const hasValidationErrors = this.state.validationErrors.length > 0; |
| 95 | + |
| 96 | + return ( |
| 97 | + <div className="row"> |
| 98 | + <div className="col-sm-12"> |
| 99 | + <div className="row"> |
| 100 | + <div className="col-sm-12"> |
| 101 | + <h5> |
| 102 | + { this.props.l10n['t_import_info_who_selling_series'] || 'Import info about selling this series' } |
| 103 | + </h5> |
| 104 | + </div> |
| 105 | + </div> |
| 106 | + <div className="row"> |
| 107 | + <div id="import-series-sale-failed-msg" |
| 108 | + className={`alert alert-danger text-center col-sm-8 col-sm-offset-2 ${this.state.hasServerError ? '' : 'hidden'}`}> |
| 109 | + { this.props.l10n['t_could_not_import_info'] || 'Could not import information from this page' } |
| 110 | + </div> |
| 111 | + </div> |
| 112 | + <div className="row"> |
| 113 | + <div className="col-sm-12"> |
| 114 | + <form id="import-series-sale-form" |
| 115 | + className={`form-horizontal ${hasValidationErrors ? 'has-error' : ''}`} |
| 116 | + onSubmit={this.handleSubmit}> |
| 117 | + |
| 118 | + <div className="form-group form-group-sm"> |
| 119 | + <label htmlFor="series-sale-url" className="control-label col-sm-3"> |
| 120 | + { this.props.l10n['t_url'] || 'URL' } |
| 121 | + <span className="required_field"> *</span> |
| 122 | + </label> |
| 123 | + <div className="col-sm-6"> |
| 124 | + <input id="series-sale-url" |
| 125 | + name="url" |
| 126 | + type="url" |
| 127 | + className="form-control" |
| 128 | + required="required" |
| 129 | + value={this.state.url} |
| 130 | + onChange={this.handleChange} |
| 131 | + disabled={this.state.isDisabled} /> |
| 132 | + <span id="series-sale-url.errors" |
| 133 | + className={`help-block ${hasValidationErrors ? '' : 'hidden'}`}> |
| 134 | + { this.state.validationErrors.join(', ') } |
| 135 | + </span> |
| 136 | + </div> |
| 137 | + </div> |
| 138 | + |
| 139 | + <div className="form-group form-group-sm"> |
| 140 | + <div className="col-sm-offset-3 col-sm-4"> |
| 141 | + <button type="submit" |
| 142 | + className="btn btn-primary" |
| 143 | + disabled={this.state.isDisabled}> |
| 144 | + { this.props.l10n['t_import_info'] || 'Import info' } |
| 145 | + </button> |
| 146 | + </div> |
| 147 | + </div> |
| 148 | + |
| 149 | + </form> |
| 150 | + </div> |
| 151 | + </div> |
| 152 | + </div> |
| 153 | + </div> |
| 154 | + ) |
| 155 | + } |
| 156 | +} |
0 commit comments