2018-04-19 01:19:10 +05:30
|
|
|
// @flow
|
2017-08-23 00:19:50 +05:30
|
|
|
import React from 'react';
|
2016-08-05 11:28:24 +05:30
|
|
|
import classNames from 'classnames';
|
2018-04-19 01:19:10 +05:30
|
|
|
import type { CaptchaID } from 'services/captcha';
|
|
|
|
import type { Skin } from 'components/ui';
|
2016-07-31 19:23:16 +05:30
|
|
|
import captcha from 'services/captcha';
|
2018-02-13 02:24:56 +05:30
|
|
|
import logger from 'services/logger';
|
2016-08-14 14:29:23 +05:30
|
|
|
import { ComponentLoader } from 'components/ui/loader';
|
2016-07-31 19:23:16 +05:30
|
|
|
|
|
|
|
import styles from './form.scss';
|
|
|
|
import FormInputComponent from './FormInputComponent';
|
|
|
|
|
2018-04-19 01:19:10 +05:30
|
|
|
export default class Captcha extends FormInputComponent<{
|
|
|
|
delay: number,
|
|
|
|
skin: Skin,
|
|
|
|
}, {
|
|
|
|
code: string,
|
|
|
|
}> {
|
2019-06-30 19:02:50 +05:30
|
|
|
elRef = React.createRef<HTMLDivElement>();
|
2018-04-19 01:19:10 +05:30
|
|
|
captchaId: CaptchaID;
|
2016-07-31 19:23:16 +05:30
|
|
|
|
|
|
|
static defaultProps = {
|
2018-04-19 01:19:10 +05:30
|
|
|
skin: 'dark',
|
2016-08-14 14:29:23 +05:30
|
|
|
delay: 0
|
2016-07-31 19:23:16 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
componentDidMount() {
|
2017-08-23 02:30:10 +05:30
|
|
|
setTimeout(() => {
|
2019-06-30 19:02:50 +05:30
|
|
|
const {current: el} = this.elRef;
|
|
|
|
|
|
|
|
el && captcha.render(el, {
|
2016-08-14 15:40:59 +05:30
|
|
|
skin: this.props.skin,
|
|
|
|
onSetCode: this.setCode
|
2018-04-19 01:19:10 +05:30
|
|
|
})
|
|
|
|
.then((captchaId) => {this.captchaId = captchaId;})
|
|
|
|
.catch((error) => {
|
|
|
|
logger.error('Failed rendering captcha', {
|
|
|
|
error
|
|
|
|
});
|
|
|
|
});
|
2017-08-23 02:30:10 +05:30
|
|
|
}, this.props.delay);
|
2016-07-31 19:23:16 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2016-08-05 11:28:24 +05:30
|
|
|
const {skin} = this.props;
|
|
|
|
|
2016-07-31 19:23:16 +05:30
|
|
|
return (
|
2016-08-14 14:29:23 +05:30
|
|
|
<div className={styles.captchaContainer}>
|
|
|
|
<div className={styles.captchaLoader}>
|
|
|
|
<ComponentLoader />
|
|
|
|
</div>
|
2016-08-14 15:40:59 +05:30
|
|
|
|
2019-06-30 19:02:50 +05:30
|
|
|
<div ref={this.elRef} className={classNames(
|
2016-08-05 11:28:24 +05:30
|
|
|
styles.captcha,
|
|
|
|
styles[`${skin}Captcha`]
|
|
|
|
)} />
|
2016-08-14 15:40:59 +05:30
|
|
|
|
2016-08-05 11:11:33 +05:30
|
|
|
{this.renderError()}
|
|
|
|
</div>
|
2016-07-31 19:23:16 +05:30
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-08-14 15:40:59 +05:30
|
|
|
reset() {
|
|
|
|
captcha.reset(this.captchaId);
|
|
|
|
}
|
|
|
|
|
2016-08-23 00:48:11 +05:30
|
|
|
getValue() {
|
|
|
|
return this.state && this.state.code;
|
|
|
|
}
|
2016-08-14 15:40:59 +05:30
|
|
|
|
2016-08-23 00:48:11 +05:30
|
|
|
onFormInvalid() {
|
2016-08-14 15:40:59 +05:30
|
|
|
this.reset();
|
|
|
|
}
|
|
|
|
|
2018-04-19 01:19:10 +05:30
|
|
|
setCode = (code: string) => this.setState({code});
|
2016-07-31 19:23:16 +05:30
|
|
|
}
|