2017-08-23 00:19:50 +05:30
|
|
|
import React from 'react';
|
2019-12-08 01:13:08 +05:30
|
|
|
import clsx from 'clsx';
|
2019-12-08 00:32:00 +05:30
|
|
|
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';
|
2016-07-31 19:23:16 +05:30
|
|
|
|
|
|
|
import styles from './form.scss';
|
|
|
|
import FormInputComponent from './FormInputComponent';
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
export default class Captcha extends FormInputComponent<
|
2020-05-24 04:38:24 +05:30
|
|
|
{
|
|
|
|
delay: number;
|
|
|
|
skin: Skin;
|
|
|
|
},
|
|
|
|
{
|
|
|
|
code: string;
|
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
> {
|
2020-05-24 04:38:24 +05:30
|
|
|
elRef = React.createRef<HTMLDivElement>();
|
|
|
|
captchaId: CaptchaID;
|
2016-07-31 19:23:16 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
static defaultProps = {
|
|
|
|
skin: 'dark',
|
|
|
|
delay: 0,
|
|
|
|
};
|
2016-07-31 19:23:16 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
componentDidMount() {
|
|
|
|
setTimeout(() => {
|
|
|
|
const { current: el } = this.elRef;
|
2019-06-30 19:02:50 +05:30
|
|
|
|
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);
|
|
|
|
}
|
2016-07-31 19:23:16 +05:30
|
|
|
|
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>
|
2016-08-14 15:40:59 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
<div ref={this.elRef} className={clsx(styles.captcha, styles[`${skin}Captcha`])} />
|
2016-08-14 15:40:59 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
{this.renderError()}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2016-07-31 19:23:16 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
reset() {
|
|
|
|
captcha.reset(this.captchaId);
|
|
|
|
}
|
2016-08-14 15:40:59 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
getValue() {
|
|
|
|
return this.state && this.state.code;
|
|
|
|
}
|
2016-08-14 15:40:59 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
onFormInvalid() {
|
|
|
|
this.reset();
|
|
|
|
}
|
2016-08-14 15:40:59 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
setCode = (code: string) => this.setState({ code });
|
2016-07-31 19:23:16 +05:30
|
|
|
}
|