2020-01-18 02:07:52 +05:30
|
|
|
import React, { ReactNode } from 'react';
|
|
|
|
import { RouteComponentProps } from 'react-router-dom';
|
|
|
|
|
2019-12-08 00:32:00 +05:30
|
|
|
import AuthError from 'app/components/auth/authError/AuthError';
|
|
|
|
import { FormModel } from 'app/components/ui/form';
|
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<
|
2020-05-24 04:38:24 +05:30
|
|
|
// TODO: this may be converted to generic type RouteComponentProps<T>
|
|
|
|
RouteComponentProps<Record<string, any>>
|
2019-12-07 16:58:52 +05:30
|
|
|
> {
|
2020-05-24 04:38:24 +05:30
|
|
|
static contextType = Context;
|
2020-10-11 22:03:55 +05:30
|
|
|
declare context: React.ContextType<typeof Context>;
|
2020-05-24 04:38:24 +05:30
|
|
|
prevErrors: AuthContext['auth']['error'];
|
2016-08-14 15:40:59 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
autoFocusField: string | null = '';
|
2019-12-07 16:58:52 +05:30
|
|
|
|
2020-05-24 04:38:24 +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();
|
|
|
|
}
|
2019-12-13 12:56:29 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
this.prevErrors = this.context.auth.error;
|
2016-02-13 20:58:47 +05:30
|
|
|
}
|
2019-12-13 12:56:29 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
renderErrors(): ReactNode {
|
|
|
|
const error = this.form.getFirstError();
|
2016-02-13 20:58:47 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
if (error === null) {
|
|
|
|
return null;
|
|
|
|
}
|
2019-12-07 16:58:52 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
return <AuthError error={error} onClose={this.onClearErrors} />;
|
2020-01-18 02:07:52 +05:30
|
|
|
}
|
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
onFormSubmit() {
|
|
|
|
this.context.resolve(this.serialize());
|
|
|
|
}
|
2016-02-13 20:58:47 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
onClearErrors = () => this.context.clearErrors();
|
2016-02-13 20:58:47 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
form = new FormModel({
|
|
|
|
renderErrors: false,
|
|
|
|
});
|
2017-04-19 23:21:04 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
bindField(name: string) {
|
|
|
|
return this.form.bindField(name);
|
|
|
|
}
|
2016-02-13 20:58:47 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
serialize() {
|
|
|
|
return this.form.serialize();
|
|
|
|
}
|
2016-03-12 19:53:55 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
autoFocus() {
|
|
|
|
const fieldId = this.autoFocusField;
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
if (fieldId && this.form.hasField(fieldId)) {
|
|
|
|
this.form.focus(fieldId);
|
|
|
|
}
|
2019-12-13 12:56:29 +05:30
|
|
|
}
|
2016-02-13 20:58:47 +05:30
|
|
|
}
|
2019-12-13 12:56:29 +05:30
|
|
|
|
|
|
|
export default BaseAuthBody;
|