import React, { PureComponent } from 'react'; import PropTypes from 'prop-types'; import Button from '../../../../components/ui/button'; import { INITIALIZE_SEED_PHRASE_INTRO_ROUTE, INITIALIZE_SELECT_ACTION_ROUTE, } from '../../../../helpers/constants/routes'; import TextField from '../../../../components/ui/text-field'; import { EVENT } from '../../../../../shared/constants/metametrics'; export default class NewAccount extends PureComponent { static contextTypes = { trackEvent: PropTypes.func, t: PropTypes.func, }; static propTypes = { onSubmit: PropTypes.func.isRequired, history: PropTypes.object.isRequired, }; state = { password: '', confirmPassword: '', passwordError: '', confirmPasswordError: '', termsChecked: false, }; isValid() { const { password, confirmPassword, passwordError, confirmPasswordError } = this.state; if (!password || !confirmPassword || password !== confirmPassword) { return false; } if (password.length < 8) { return false; } return !passwordError && !confirmPasswordError; } handlePasswordChange(password) { const { t } = this.context; this.setState((state) => { const { confirmPassword } = state; let passwordError = ''; let confirmPasswordError = ''; if (password && password.length < 8) { passwordError = t('passwordNotLongEnough'); } if (confirmPassword && password !== confirmPassword) { confirmPasswordError = t('passwordsDontMatch'); } return { password, passwordError, confirmPasswordError, }; }); } handleConfirmPasswordChange(confirmPassword) { const { t } = this.context; this.setState((state) => { const { password } = state; let confirmPasswordError = ''; if (password !== confirmPassword) { confirmPasswordError = t('passwordsDontMatch'); } return { confirmPassword, confirmPasswordError, }; }); } handleCreate = async (event) => { event.preventDefault(); if (!this.isValid()) { return; } const { password } = this.state; const { onSubmit, history } = this.props; try { await onSubmit(password); this.context.trackEvent({ category: EVENT.CATEGORIES.ONBOARDING, event: 'Submit Password', properties: { action: 'Create Password', legacy_event: true, }, }); history.push(INITIALIZE_SEED_PHRASE_INTRO_ROUTE); } catch (error) { this.setState({ passwordError: error.message }); } }; toggleTermsCheck = () => { this.context.trackEvent({ category: EVENT.CATEGORIES.ONBOARDING, event: 'Check ToS', properties: { action: 'Create Password', legacy_event: true, }, }); this.setState((prevState) => ({ termsChecked: !prevState.termsChecked, })); }; onTermsKeyPress = ({ key }) => { if (key === ' ' || key === 'Enter') { this.toggleTermsCheck(); } }; render() { const { t } = this.context; const { password, confirmPassword, passwordError, confirmPasswordError, termsChecked, } = this.state; return (
); } }