/** @jsx jsx */ import * as React from 'react' import { jsx } from '@emotion/core' import Reaptcha from 'reaptcha' import { emit, on } from '@/scripts/event' import { t } from '@/scripts/i18n' import * as cssUtils from '@/styles/utils' const eventId = Symbol() type State = { value: string time: number sitekey: string invisible: boolean } class Captcha extends React.Component<{}, State> { state: State ref: React.MutableRefObject constructor(props: {}) { super(props) this.state = { value: '', time: Date.now(), sitekey: blessing.extra.recaptcha, invisible: blessing.extra.invisible, } this.ref = React.createRef() } execute = async () => { const recaptcha = this.ref.current if (recaptcha && this.state.invisible) { return new Promise((resolve) => { const off = on(eventId, (value: string) => { resolve(value) off() }) recaptcha.execute() }) } return this.state.value } reset = () => { const recaptcha = this.ref.current if (recaptcha) { recaptcha.reset() } else { this.setState({ time: Date.now() }) } } handleValueChange = (event: React.ChangeEvent) => { this.setState({ value: event.target.value }) } handleVerify = (value: string) => { emit(eventId, value) this.setState({ value }) } handleRefresh = () => { this.setState({ time: Date.now() }) } render() { return this.state.sitekey ? (
) : (
{t('auth.captcha')}
) } } export default Captcha