import React, { useState, useMemo } from 'react'; import PropTypes from 'prop-types'; import { useHistory } from 'react-router-dom'; import { useNewMetricEvent } from '../../../hooks/useMetricEvent'; import { useI18nContext } from '../../../hooks/useI18nContext'; import Button from '../../../components/ui/button'; import Typography from '../../../components/ui/typography'; import { TEXT_ALIGN, TYPOGRAPHY, JUSTIFY_CONTENT, FONT_WEIGHT, ALIGN_ITEMS, } from '../../../helpers/constants/design-system'; import { ONBOARDING_COMPLETION_ROUTE, ONBOARDING_SECURE_YOUR_WALLET_ROUTE, } from '../../../helpers/constants/routes'; import FormField from '../../../components/ui/form-field'; import Box from '../../../components/ui/box'; import CheckBox from '../../../components/ui/check-box'; import { ThreeStepProgressBar, threeStepStages, TwoStepProgressBar, twoStepStages, } from '../../../components/app/step-progress-bar'; export default function CreatePassword({ createNewAccount, importWithRecoveryPhrase, secretRecoveryPhrase, }) { const t = useI18nContext(); const [confirmPassword, setConfirmPassword] = useState(''); const [password, setPassword] = useState(''); const [passwordError, setPasswordError] = useState(''); const [confirmPasswordError, setConfirmPasswordError] = useState(''); const [termsChecked, setTermsChecked] = useState(false); const [showPassword, setShowPassword] = useState(false); const history = useHistory(); const submitPasswordEvent = useNewMetricEvent({ event: 'Submit Password', category: 'Onboarding', }); const isValid = useMemo(() => { if (!password || !confirmPassword || password !== confirmPassword) { return false; } if (password.length < 8) { return false; } return !passwordError && !confirmPasswordError; }, [password, confirmPassword, passwordError, confirmPasswordError]); const handlePasswordChange = (passwordInput) => { let error = ''; let confirmError = ''; if (passwordInput && passwordInput.length < 8) { error = t('passwordNotLongEnough'); } if (confirmPassword && passwordInput !== confirmPassword) { confirmError = t('passwordsDontMatch'); } setPassword(passwordInput); setPasswordError(error); setConfirmPasswordError(confirmError); }; const handleConfirmPasswordChange = (confirmPasswordInput) => { let error = ''; if (password !== confirmPasswordInput) { error = t('passwordsDontMatch'); } setConfirmPassword(confirmPasswordInput); setConfirmPasswordError(error); }; const handleCreate = async (event) => { event.preventDefault(); if (!isValid) { return; } // If secretRecoveryPhrase is defined we are in import wallet flow if (secretRecoveryPhrase) { await importWithRecoveryPhrase(password, secretRecoveryPhrase); history.push(ONBOARDING_COMPLETION_ROUTE); } else { // Otherwise we are in create new wallet flow try { if (createNewAccount) { await createNewAccount(password); } submitPasswordEvent(); history.push(ONBOARDING_SECURE_YOUR_WALLET_ROUTE); } catch (error) { setPasswordError(error.message); } } }; return (