accounts-frontend/packages/app/components/ui/form/Captcha.tsx

80 lines
1.9 KiB
TypeScript
Raw Permalink Normal View History

import React from 'react';
2019-12-08 01:13:08 +05:30
import clsx from 'clsx';
import { CaptchaID } from 'app/services/captcha';
import { Skin } from 'app/components/ui';
import captcha from 'app/services/captcha';
import logger from 'app/services/logger';
import { ComponentLoader } from 'app/components/ui/loader';
import styles from './form.scss';
import FormInputComponent from './FormInputComponent';
export default class Captcha extends FormInputComponent<
2020-05-24 04:38:24 +05:30
{
delay: number;
skin: Skin;
},
{
code: string;
}
> {
2020-05-24 04:38:24 +05:30
elRef = React.createRef<HTMLDivElement>();
captchaId: CaptchaID;
2020-05-24 04:38:24 +05:30
static defaultProps = {
skin: 'dark',
delay: 0,
};
2020-05-24 04:38:24 +05:30
componentDidMount() {
setTimeout(() => {
const { current: el } = this.elRef;
2020-05-24 04:38:24 +05:30
el &&
captcha
.render(el, {
skin: this.props.skin,
onSetCode: this.setCode,
})
.then((captchaId) => {
this.captchaId = captchaId;
})
.catch((error) => {
logger.error('Failed rendering captcha', {
error,
});
});
}, this.props.delay);
}
2020-05-24 04:38:24 +05:30
render() {
const { skin } = this.props;
2016-08-05 11:28:24 +05:30
2020-05-24 04:38:24 +05:30
return (
<div className={styles.captchaContainer}>
<div className={styles.captchaLoader}>
<ComponentLoader />
</div>
2020-05-24 04:38:24 +05:30
<div ref={this.elRef} className={clsx(styles.captcha, styles[`${skin}Captcha`])} />
2020-05-24 04:38:24 +05:30
{this.renderError()}
</div>
);
}
2020-05-24 04:38:24 +05:30
reset() {
captcha.reset(this.captchaId);
}
2020-05-24 04:38:24 +05:30
getValue() {
return this.state && this.state.code;
}
2020-05-24 04:38:24 +05:30
onFormInvalid() {
this.reset();
}
2020-05-24 04:38:24 +05:30
setCode = (code: string) => this.setState({ code });
}