2016-02-13 20:58:47 +05:30
|
|
|
/**
|
|
|
|
* Helps with form fields binding, form serialization and errors rendering
|
|
|
|
*/
|
|
|
|
import React, { Component, PropTypes } from 'react';
|
|
|
|
|
2016-03-13 14:20:09 +05:30
|
|
|
import AuthError from 'components/auth/authError/AuthError';
|
2016-03-13 14:06:31 +05:30
|
|
|
import { userShape } from 'components/user/User';
|
2016-05-02 15:08:45 +05:30
|
|
|
import { FormModel } from 'components/ui/form';
|
2016-02-13 20:58:47 +05:30
|
|
|
|
|
|
|
export default class BaseAuthBody extends Component {
|
2016-03-13 14:06:31 +05:30
|
|
|
static contextTypes = {
|
2016-02-13 20:58:47 +05:30
|
|
|
clearErrors: PropTypes.func.isRequired,
|
2016-03-02 02:06:14 +05:30
|
|
|
resolve: PropTypes.func.isRequired,
|
2016-05-22 18:37:51 +05:30
|
|
|
requestRedraw: PropTypes.func.isRequired,
|
2016-02-13 20:58:47 +05:30
|
|
|
auth: PropTypes.shape({
|
2016-05-22 19:31:31 +05:30
|
|
|
error: PropTypes.oneOfType([PropTypes.string, PropTypes.shape({
|
|
|
|
type: PropTypes.string,
|
|
|
|
payload: PropTypes.object
|
|
|
|
})]),
|
2016-03-13 14:06:31 +05:30
|
|
|
scopes: PropTypes.array
|
2016-08-14 15:40:59 +05:30
|
|
|
}).isRequired,
|
2016-03-13 14:06:31 +05:30
|
|
|
user: userShape
|
2016-02-13 20:58:47 +05:30
|
|
|
};
|
|
|
|
|
2016-08-14 15:40:59 +05:30
|
|
|
componentWillReceiveProps(nextProps, nextContext) {
|
2016-08-23 00:48:11 +05:30
|
|
|
if (nextContext.auth.error !== this.context.auth.error) {
|
|
|
|
this.form.setErrors(nextContext.auth.error || {});
|
2016-08-14 15:40:59 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-13 20:58:47 +05:30
|
|
|
renderErrors() {
|
2016-08-23 00:48:11 +05:30
|
|
|
return this.form.hasErrors()
|
|
|
|
? <AuthError error={this.form.getFirstError()} onClose={this.onClearErrors} />
|
2016-05-22 19:31:31 +05:30
|
|
|
: null
|
2016-02-13 20:58:47 +05:30
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2016-03-02 02:06:14 +05:30
|
|
|
onFormSubmit() {
|
2016-03-13 14:06:31 +05:30
|
|
|
this.context.resolve(this.serialize());
|
2016-03-02 02:06:14 +05:30
|
|
|
}
|
|
|
|
|
2016-03-13 14:06:31 +05:30
|
|
|
onClearErrors = this.context.clearErrors;
|
2016-02-13 20:58:47 +05:30
|
|
|
|
2016-08-23 00:48:11 +05:30
|
|
|
form = new FormModel({
|
|
|
|
renderErrors: false
|
|
|
|
});
|
2016-02-13 20:58:47 +05:30
|
|
|
|
2016-05-01 12:48:34 +05:30
|
|
|
bindField = this.form.bindField.bind(this.form);
|
2017-04-19 23:21:04 +05:30
|
|
|
|
|
|
|
serialize() {
|
|
|
|
return this.form.serialize();
|
|
|
|
}
|
2016-02-13 20:58:47 +05:30
|
|
|
|
2016-03-28 10:35:18 +05:30
|
|
|
autoFocus() {
|
|
|
|
const fieldId = this.autoFocusField;
|
2016-03-12 19:53:55 +05:30
|
|
|
|
2016-05-01 12:48:34 +05:30
|
|
|
fieldId && this.form.focus(fieldId);
|
2016-02-13 20:58:47 +05:30
|
|
|
}
|
|
|
|
}
|