// @flow import React, { useState, useEffect } from 'react'; import { Form, FormField, Submit } from 'component/common/form'; import Button from 'component/button'; import UserEmail from 'component/userEmail'; import * as ICONS from 'constants/icons'; import { getSavedPassword, setSavedPassword, deleteSavedPassword } from 'util/saved-passwords'; type Props = { // wallet statuses walletEncryptSucceeded: boolean, walletEncryptPending: boolean, walletDecryptSucceeded: boolean, walletDecryptPending: boolean, updateWalletStatus: boolean, walletEncrypted: boolean, // wallet methods encryptWallet: (?string) => void, decryptWallet: (?string) => void, updateWalletStatus: () => void, // housekeeping setPasswordSaved: () => void, syncEnabled: boolean, setClientSetting: (string, boolean | string) => void, isPasswordSaved: boolean, // data user: any, // sync statuses hasSyncedWallet: boolean, getSyncIsPending?: boolean, syncApplyErrorMessage?: string, hashChanged: boolean, // sync data syncData: string | null, syncHash: string | null, // sync methods syncApply: (string | null, string | null, string) => void, checkSync: () => void, setDefaultAccount: () => void, hasTransactions: boolean, }; type State = { newPassword: string, newPasswordConfirm: string, passwordMatch: boolean, understandConfirmed: boolean, understandError: boolean, submitted: boolean, failMessage: ?string, rememberPassword: boolean, showEmailReg: boolean, failed: boolean, enableSync: boolean, encryptWallet: boolean, obscurePassword: boolean, advancedMode: boolean, showPasswordFields: boolean, }; function WalletSecurityAndSync(props: Props) { const { // walletEncryptSucceeded, // walletEncryptPending, // walletDecryptSucceeded, // walletDecryptPending, // updateWalletStatus, walletEncrypted, encryptWallet, decryptWallet, syncEnabled, user, hasSyncedWallet, getSyncIsPending, syncApplyErrorMessage, hashChanged, syncData, syncHash, syncApply, checkSync, hasTransactions, setDefaultAccount, } = props; const defaultComponentState: State = { newPassword: '', newPasswordConfirm: '', passwordMatch: false, understandConfirmed: false, understandError: false, submitted: false, // Prior actions could be marked complete failMessage: undefined, rememberPassword: false, showEmailReg: false, failed: false, enableSync: syncEnabled, encryptWallet: walletEncrypted, obscurePassword: true, advancedMode: false, showPasswordFields: false, }; const [componentState, setComponentState] = useState(defaultComponentState); const safeToSync = !hasTransactions || !hashChanged; // on mount useEffect(() => { checkSync(); getSavedPassword().then(p => { if (p) { setComponentState({ ...componentState, newPassword: p, newPasswordConfirm: p, showPasswordFields: true, rememberPassword: true, }); } }); }, []); useEffect(() => { setComponentState({ ...componentState, passwordMatch: componentState.newPassword === componentState.newPasswordConfirm, }); }, [componentState.newPassword, componentState.newPasswordConfirm]); const isEmailVerified = user && user.primary_email && user.has_verified_email; // const syncDisabledMessage = 'You cannot sync without an email'; function onChangeNewPassword(event: SyntheticInputEvent<>) { setComponentState({ ...componentState, newPassword: event.target.value || '' }); } function onChangeRememberPassword(event: SyntheticInputEvent<>) { if (componentState.rememberPassword) { deleteSavedPassword(); } setComponentState({ ...componentState, rememberPassword: event.target.checked }); } function onChangeNewPasswordConfirm(event: SyntheticInputEvent<>) { setComponentState({ ...componentState, newPasswordConfirm: event.target.value || '' }); } function onChangeUnderstandConfirm(event: SyntheticInputEvent<>) { setComponentState({ ...componentState, understandConfirmed: /^.?i understand.?$/i.test(event.target.value) }); } function onChangeSync(event: SyntheticInputEvent<>) { if (componentState.enableSync) { setComponentState({ ...componentState, enableSync: false, newPassword: '', newPasswordConfirm: '' }); setComponentState({ ...componentState, enableSync: false, newPassword: '', newPasswordConfirm: '' }); } if (!(walletEncrypted || syncApplyErrorMessage || componentState.advancedMode)) { easyApply(); } else { setComponentState({ ...componentState, enableSync: true }); } } function onChangeEncrypt(event: SyntheticInputEvent<>) { setComponentState({ ...componentState, encryptWallet: event.target.checked }); } async function easyApply() { return new Promise((resolve, reject) => { return syncApply(syncHash, syncData, componentState.newPassword); }) .then(() => { setComponentState({ ...componentState, enableSync: event.target.checked }); }) .catch(); } async function apply() { setComponentState({ ...componentState, failed: false }); await checkSync(); if (componentState.enableSync) { await syncApply(syncHash, syncData, componentState.newPassword); if (syncApplyErrorMessage) { setComponentState({ ...componentState, failed: true }); } } if (walletEncrypted) { await decryptWallet(); } if (componentState.encryptWallet && !componentState.failed) { await encryptWallet(componentState.newPassword) .then(() => {}) .catch(() => { setComponentState({ ...componentState, failed: false }); }); } if (componentState.rememberPassword && !componentState.failed) { setSavedPassword(componentState.newPassword); } } return (

{__('Wallet Sync and Security')}

{!isEmailVerified && (

{__(`It looks like we don't have your email.`)}{' '}

{/* Testing stuff and Diagnostics */}
); } export default WalletSecurityAndSync;