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-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,
|
|
|
|
reject: PropTypes.func.isRequired,
|
2016-02-13 20:58:47 +05:30
|
|
|
auth: PropTypes.shape({
|
2016-03-13 14:06:31 +05:30
|
|
|
error: PropTypes.string,
|
|
|
|
scopes: PropTypes.array
|
|
|
|
}),
|
|
|
|
user: userShape
|
2016-02-13 20:58:47 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
renderErrors() {
|
2016-03-13 14:06:31 +05:30
|
|
|
return this.context.auth.error
|
|
|
|
? <AuthError error={this.context.auth.error} onClose={this.onClearErrors} />
|
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
|
|
|
|
|
|
|
form = {};
|
|
|
|
|
|
|
|
bindField(name) {
|
|
|
|
return {
|
|
|
|
name,
|
|
|
|
ref: (el) => {
|
|
|
|
this.form[name] = el;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-03-28 10:35:18 +05:30
|
|
|
autoFocus() {
|
|
|
|
const fieldId = this.autoFocusField;
|
2016-03-12 19:53:55 +05:30
|
|
|
|
2016-03-28 10:35:18 +05:30
|
|
|
fieldId && this.form[fieldId] && this.form[fieldId].focus();
|
|
|
|
}
|
2016-03-12 19:53:55 +05:30
|
|
|
|
2016-02-13 20:58:47 +05:30
|
|
|
serialize() {
|
|
|
|
return Object.keys(this.form).reduce((acc, key) => {
|
|
|
|
acc[key] = this.form[key].getValue();
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
}
|
|
|
|
}
|