import React, { useCallback, useState } from 'react'; import PropTypes from 'prop-types'; import { useI18nContext } from '../../../hooks/useI18nContext'; import TextField from '../../ui/text-field'; import Button from '../../ui/button'; import CheckBox from '../../ui/check-box'; import Typography from '../../ui/typography'; import SrpInput from '../srp-input'; export default function CreateNewVault({ disabled = false, includeTerms = false, onSubmit, submitText, }) { const [confirmPassword, setConfirmPassword] = useState(''); const [confirmPasswordError, setConfirmPasswordError] = useState(''); const [password, setPassword] = useState(''); const [passwordError, setPasswordError] = useState(''); const [seedPhrase, setSeedPhrase] = useState(''); const [termsChecked, setTermsChecked] = useState(false); const t = useI18nContext(); const onPasswordChange = useCallback( (newPassword) => { let newConfirmPasswordError = ''; let newPasswordError = ''; if (newPassword && newPassword.length < 8) { newPasswordError = t('passwordNotLongEnough'); } if (confirmPassword && newPassword !== confirmPassword) { newConfirmPasswordError = t('passwordsDontMatch'); } setPassword(newPassword); setPasswordError(newPasswordError); setConfirmPasswordError(newConfirmPasswordError); }, [confirmPassword, t], ); const onConfirmPasswordChange = useCallback( (newConfirmPassword) => { let newConfirmPasswordError = ''; if (password !== newConfirmPassword) { newConfirmPasswordError = t('passwordsDontMatch'); } setConfirmPassword(newConfirmPassword); setConfirmPasswordError(newConfirmPasswordError); }, [password, t], ); const isValid = !disabled && password && confirmPassword && password === confirmPassword && seedPhrase && (!includeTerms || termsChecked) && !passwordError && !confirmPasswordError; const onImport = useCallback( async (event) => { event.preventDefault(); if (!isValid) { return; } await onSubmit(password, seedPhrase); }, [isValid, onSubmit, password, seedPhrase], ); const toggleTermsCheck = useCallback(() => { setTermsChecked((currentTermsChecked) => !currentTermsChecked); }, []); const termsOfUse = t('acceptTermsOfUse', [ {t('terms')} , ]); return (
onPasswordChange(event.target.value)} error={passwordError} autoComplete="new-password" margin="normal" largeLabel /> onConfirmPasswordChange(event.target.value)} error={confirmPasswordError} autoComplete="new-password" margin="normal" largeLabel />
{includeTerms ? (
) : null} ); } CreateNewVault.propTypes = { disabled: PropTypes.bool, includeTerms: PropTypes.bool, onSubmit: PropTypes.func.isRequired, submitText: PropTypes.string.isRequired, };