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