#311: fix validation in profile forms

This commit is contained in:
SleepWalker 2017-02-27 07:47:31 +02:00
parent 359ac19cc0
commit 1debe774ae
8 changed files with 49 additions and 9 deletions

View File

@ -323,12 +323,20 @@ export default class ChangeEmail extends Component {
onFormSubmit = () => { onFormSubmit = () => {
const {activeStep} = this.state; const {activeStep} = this.state;
const promise = this.props.onSubmit(activeStep, this.props.stepForms[activeStep]); const form = this.props.stepForms[activeStep];
const promise = this.props.onSubmit(activeStep, form);
if (!promise || !promise.then) { if (!promise || !promise.then) {
throw new Error('Expecting promise from onSubmit'); throw new Error('Expecting promise from onSubmit');
} }
promise.then(() => this.nextStep(), () => this.forceUpdate()); promise.then(() => this.nextStep(), (resp) => {
if (resp.errors) {
form.setErrors(resp.errors);
this.forceUpdate();
} else {
return Promise.reject(resp);
}
});
}; };
} }

View File

@ -95,6 +95,15 @@ export default class ChangePassword extends Component {
} }
onFormSubmit = () => { onFormSubmit = () => {
this.props.onSubmit(this.props.form); const {form} = this.props;
this.props.onSubmit(form)
.catch((resp) => {
if (resp.errors) {
form.setErrors(resp.errors);
} else {
return Promise.reject(resp);
}
});
}; };
} }

View File

@ -80,6 +80,15 @@ export default class ChangeUsername extends Component {
}; };
onFormSubmit = () => { onFormSubmit = () => {
this.props.onSubmit(this.props.form); const {form} = this.props;
this.props.onSubmit(form)
.catch((resp) => {
if (resp.errors) {
form.setErrors(resp.errors);
} else {
return Promise.reject(resp);
}
});
}; };
} }

View File

@ -13,5 +13,11 @@ export default function FormError({error}) {
} }
FormError.propTypes = { FormError.propTypes = {
error: PropTypes.string error: PropTypes.oneOfType([
PropTypes.string,
PropTypes.shape({
type: PropTypes.string.isRequired,
payload: PropTypes.object.isRequired
})
])
}; };

View File

@ -25,7 +25,13 @@ export default class Input extends FormInputComponent {
}), }),
PropTypes.string PropTypes.string
]), ]),
error: PropTypes.string, error: PropTypes.oneOfType([
PropTypes.string,
PropTypes.shape({
type: PropTypes.string.isRequired,
payload: PropTypes.object.isRequired
})
]),
icon: PropTypes.string, icon: PropTypes.string,
skin: PropTypes.oneOf(skins), skin: PropTypes.oneOf(skins),
color: PropTypes.oneOf(colors), color: PropTypes.oneOf(colors),

View File

@ -26,7 +26,7 @@ class ChangePasswordPage extends Component {
onSubmit = () => { onSubmit = () => {
const {form} = this; const {form} = this;
this.context.onSubmit({ return this.context.onSubmit({
form, form,
sendData: () => accounts.changePassword(form.serialize()) sendData: () => accounts.changePassword(form.serialize())
}).then(() => { }).then(() => {

View File

@ -45,10 +45,10 @@ class ChangeUsernamePage extends Component {
const {form} = this; const {form} = this;
if (this.actualUsername === this.props.username) { if (this.actualUsername === this.props.username) {
this.context.goToProfile(); this.context.goToProfile();
return; return Promise.resolve();
} }
this.context.onSubmit({ return this.context.onSubmit({
form, form,
sendData: () => accounts.changeUsername(form.serialize()) sendData: () => accounts.changeUsername(form.serialize())
}).then(() => { }).then(() => {

View File

@ -86,6 +86,8 @@ export default connect(null, {
logger.warn('Unexpected profile editing error', { logger.warn('Unexpected profile editing error', {
resp resp
}); });
} else {
return Promise.reject(resp);
} }
}) })
.finally(() => form.endLoading()); .finally(() => form.endLoading());