80 lines
1.9 KiB
TypeScript
Raw Normal View History

import React from 'react';
2019-12-07 21:43:08 +02:00
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 02:08:24 +03:00
{
delay: number;
skin: Skin;
},
{
code: string;
}
> {
2020-05-24 02:08:24 +03:00
elRef = React.createRef<HTMLDivElement>();
captchaId: CaptchaID;
2020-05-24 02:08:24 +03:00
static defaultProps = {
skin: 'dark',
delay: 0,
};
2020-05-24 02:08:24 +03:00
componentDidMount() {
setTimeout(() => {
const { current: el } = this.elRef;
2020-05-24 02:08:24 +03:00
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 02:08:24 +03:00
render() {
const { skin } = this.props;
2016-08-05 08:58:24 +03:00
2020-05-24 02:08:24 +03:00
return (
<div className={styles.captchaContainer}>
<div className={styles.captchaLoader}>
<ComponentLoader />
</div>
2020-05-24 02:08:24 +03:00
<div ref={this.elRef} className={clsx(styles.captcha, styles[`${skin}Captcha`])} />
2020-05-24 02:08:24 +03:00
{this.renderError()}
</div>
);
}
2020-05-24 02:08:24 +03:00
reset() {
captcha.reset(this.captchaId);
}
2020-05-24 02:08:24 +03:00
getValue() {
return this.state && this.state.code;
}
2020-05-24 02:08:24 +03:00
onFormInvalid() {
this.reset();
}
2020-05-24 02:08:24 +03:00
setCode = (code: string) => this.setState({ code });
}