Skip to content

add forms issues #1338, #1342, #1344 #1387

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 153 additions & 3 deletions src/main/frontend/src/components/AddCatalogPriceForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,162 @@
// IMPORTANT:
// You must update ResourceUrl.RESOURCES_VERSION each time whenever you're modified this file!
//
// @todo #1342 AddCatalogPriceForm: show a 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
});
}

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;
return (
<div></div>
)
<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-6">
<div className="row">
<div className="col-xs-6">
<input
className="form-control"
id="catalog-price"
type="text"
size="5"
required="required"
onChange={ this.handleChangePrice }/>
</div>
</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>
);
}
}
110 changes: 107 additions & 3 deletions src/main/frontend/src/components/AddCommentForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,114 @@
//

class AddCommentForm extends React.Component {

constructor(props) {
super(props);
this.state = {
comment: '',
validationErrors: [],
hasServerError: false,
isDisabled: false
};
this.handleSubmit = this.handleSubmit.bind(this);
this.handleChange = this.handleChange.bind(this);
}

handleChange(event) {
event.preventDefault();
this.setState({
comment: event.target.value
});
}

handleSubmit(event) {
event.preventDefault();

this.setState({
isDisabled: true,
hasServerError: false,
validationErrors: []
});

axios.patch(
this.props.url, [
{
op: 'add',
path: '/comment',
value: this.state.comment
}
],
{
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.comment) {
fieldErrors.push(...data.fieldErrors.comment);
}
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;
return (
<div></div>
)
<div className="col-sm-12 form-group">
<form className={`form-horizontal ${hasValidationErrors ? 'has-error' : ''}`} onSubmit={this.handleSubmit}>
<div
id="add-comment-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_comment'] || 'Comment' }
</label>
<div className="col-sm-6">
<textarea
id="comment"
className="form-control"
cols="22"
rows="3"
required="required"
onChange={this.handleChange}>
</textarea>
</div>
</div>
<div className="col-sm-offset-3 col-sm-4">
<span
id="comment.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>
);
}
}
Loading