const bindAll = require('lodash.bindall'); const classNames = require('classnames'); const React = require('react'); const PropTypes = require('prop-types'); import {Formik} from 'formik'; const {injectIntl, intlShape} = require('react-intl'); const emailValidator = require('email-validator'); const FormattedMessage = require('react-intl').FormattedMessage; const JoinFlowStep = require('./join-flow-step.jsx'); const FormikInput = require('../../components/formik-forms/formik-input.jsx'); const FormikCheckbox = require('../../components/formik-forms/formik-checkbox.jsx'); require('./join-flow-steps.scss'); class EmailStep extends React.Component { constructor (props) { super(props); bindAll(this, [ 'handleSetEmailRef', 'handleValidSubmit', 'validateEmailIfPresent', 'validateForm', 'setCaptchaRef', 'captchaSolved' ]); } componentDidMount () { // automatically start with focus on username field if (this.emailInput) this.emailInput.focus(); this.grecaptcha = window.grecaptcha; if (!this.grecaptcha) { // Captcha doesn't exist on the window. There must have been a // problem downloading the script. There isn't much we can do about it though. // TODO: put up the error screen when we have it. return; } // TODO: Add in error callback for this once we have an error screen. this.widgetId = this.grecaptcha.render(this.captchaRef, { callback: this.captchaSolved, sitekey: '' }, true); } handleSetEmailRef (emailInputRef) { this.emailInput = emailInputRef; } validateEmail (email) { if (!email) return this.props.intl.formatMessage({id: 'general.required'}); const isValidLocally = emailValidator.validate(email); if (isValidLocally) { return null; // TODO: validate email address remotely } return this.props.intl.formatMessage({id: 'registration.validationEmailInvalid'}); } validateForm () { return {}; } handleValidSubmit (formData, formikBag) { this.formData = formData; this.formikBag = formikBag; this.grecaptcha.execute(this.widgetId); } captchaSolved (token) { this.formData['g-recaptcha-response'] = token; this.formikBag.setSubmitting(false); this.props.onNextStep(this.formData); } setCaptchaRef (ref) { this.captchaRef = ref; } render () { return ( {props => { const { errors, handleSubmit, isSubmitting, setFieldError, validateField } = props; return ( ) }} /> )} headerImgSrc="/images/join-flow/email-header.png" innerClassName="join-flow-inner-email-step" nextButton={this.props.intl.formatMessage({id: 'registration.createAccount'})} title={this.props.intl.formatMessage({id: 'registration.emailStepTitle'})} waiting={isSubmitting} onSubmit={handleSubmit} > validateField('email')} onFocus={() => setFieldError('email', null)} /* eslint-enable react/jsx-no-bind */ onSetRef={this.handleSetEmailRef} />
); }} ); } } EmailStep.propTypes = { intl: intlShape, onNextStep: PropTypes.func }; module.exports = injectIntl(EmailStep);