2019-12-07 16:58:52 +05:30
|
|
|
import React from 'react';
|
2019-12-08 00:32:00 +05:30
|
|
|
import AuthError from 'app/components/auth/authError/AuthError';
|
|
|
|
import { FormModel } from 'app/components/ui/form';
|
2019-12-07 16:58:52 +05:30
|
|
|
import { RouteComponentProps } from 'react-router-dom';
|
2016-02-13 20:58:47 +05:30
|
|
|
|
2019-12-13 12:56:29 +05:30
|
|
|
import Context, { AuthContext } from './Context';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helps with form fields binding, form serialization and errors rendering
|
|
|
|
*/
|
|
|
|
|
|
|
|
class BaseAuthBody extends React.Component<
|
2019-12-07 16:58:52 +05:30
|
|
|
// TODO: this may be converted to generic type RouteComponentProps<T>
|
|
|
|
RouteComponentProps<{ [key: string]: any }>
|
|
|
|
> {
|
2019-12-13 12:56:29 +05:30
|
|
|
static contextType = Context;
|
|
|
|
/* TODO: use declare */ context: React.ContextType<typeof Context>;
|
|
|
|
prevErrors: AuthContext['auth']['error'];
|
2016-08-14 15:40:59 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
autoFocusField: string | null = '';
|
|
|
|
|
2019-12-13 12:56:29 +05:30
|
|
|
componentDidMount() {
|
|
|
|
this.prevErrors = this.context.auth.error;
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate() {
|
|
|
|
if (this.context.auth.error !== this.prevErrors) {
|
|
|
|
this.form.setErrors(this.context.auth.error || {});
|
|
|
|
this.context.requestRedraw();
|
2016-02-13 20:58:47 +05:30
|
|
|
}
|
2019-12-13 12:56:29 +05:30
|
|
|
|
|
|
|
this.prevErrors = this.context.auth.error;
|
2019-11-27 14:33:32 +05:30
|
|
|
}
|
2016-02-13 20:58:47 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
renderErrors() {
|
2019-12-07 16:58:52 +05:30
|
|
|
const error = this.form.getFirstError();
|
|
|
|
|
|
|
|
return error && <AuthError error={error} onClose={this.onClearErrors} />;
|
2019-11-27 14:33:32 +05:30
|
|
|
}
|
2016-03-02 02:06:14 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
onFormSubmit() {
|
|
|
|
this.context.resolve(this.serialize());
|
|
|
|
}
|
2016-02-13 20:58:47 +05:30
|
|
|
|
2019-12-13 12:56:29 +05:30
|
|
|
onClearErrors = () => this.context.clearErrors();
|
2016-02-13 20:58:47 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
form = new FormModel({
|
|
|
|
renderErrors: false,
|
|
|
|
});
|
2017-04-19 23:21:04 +05:30
|
|
|
|
2019-12-08 03:23:22 +05:30
|
|
|
bindField(name: string) {
|
|
|
|
return this.form.bindField(name);
|
|
|
|
}
|
2016-02-13 20:58:47 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
serialize() {
|
|
|
|
return this.form.serialize();
|
|
|
|
}
|
2016-03-12 19:53:55 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
autoFocus() {
|
|
|
|
const fieldId = this.autoFocusField;
|
|
|
|
|
2019-12-13 12:56:29 +05:30
|
|
|
if (fieldId && this.form.hasField(fieldId)) {
|
|
|
|
this.form.focus(fieldId);
|
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
}
|
2016-02-13 20:58:47 +05:30
|
|
|
}
|
2019-12-13 12:56:29 +05:30
|
|
|
|
|
|
|
export default BaseAuthBody;
|