lbry-desktop/src/ui/modal/modalWalletEncrypt/view.jsx

159 lines
4.7 KiB
React
Raw Normal View History

2018-07-18 21:48:30 +02:00
// @flow
import React from 'react';
2019-02-13 17:27:20 +01:00
import { Form, FormField } from 'component/common/form';
2018-07-18 21:48:30 +02:00
import { Modal } from 'modal/modal';
import Button from 'component/button';
type Props = {
closeModal: () => void,
walletEncryptSucceded: boolean,
updateWalletStatus: boolean,
2019-01-19 19:54:06 +01:00
encryptWallet: (?string) => void,
updateWalletStatus: () => void,
2018-07-18 21:48:30 +02:00
};
type State = {
newPassword: ?string,
newPasswordConfirm: ?string,
passwordMismatch: boolean,
understandConfirmed: boolean,
understandError: boolean,
submitted: boolean,
2019-01-19 19:54:06 +01:00
failMessage: ?string,
};
class ModalWalletEncrypt extends React.PureComponent<Props, State> {
2018-07-18 21:48:30 +02:00
state = {
newPassword: null,
newPasswordConfirm: null,
passwordMismatch: false,
understandConfirmed: false,
understandError: false,
submitted: false, // Prior actions could be marked complete
2019-01-19 19:54:06 +01:00
failMessage: undefined,
2018-07-18 21:48:30 +02:00
};
componentDidUpdate() {
const { props, state } = this;
if (state.submitted) {
if (props.walletEncryptSucceded === true) {
props.closeModal();
props.updateWalletStatus();
} else if (props.walletEncryptSucceded === false) {
// See https://github.com/lbryio/lbry/issues/1307
// eslint-disable-next-line react/no-did-update-set-state
this.setState({ failMessage: 'Unable to encrypt wallet.' });
}
}
}
onChangeNewPassword(event: SyntheticInputEvent<>) {
2018-07-18 21:48:30 +02:00
this.setState({ newPassword: event.target.value });
}
onChangeNewPasswordConfirm(event: SyntheticInputEvent<>) {
2018-07-18 21:48:30 +02:00
this.setState({ newPasswordConfirm: event.target.value });
}
onChangeUnderstandConfirm(event: SyntheticInputEvent<>) {
2018-07-18 21:48:30 +02:00
this.setState({
understandConfirmed: /^.?i understand.?$/i.test(event.target.value),
});
}
submitEncryptForm() {
const { state } = this;
let invalidEntries = false;
if (state.newPassword !== state.newPasswordConfirm) {
this.setState({ passwordMismatch: true });
invalidEntries = true;
}
if (state.understandConfirmed === false) {
this.setState({ understandError: true });
invalidEntries = true;
}
if (invalidEntries === true) {
return;
}
this.setState({ submitted: true });
this.props.encryptWallet(state.newPassword);
}
render() {
const { closeModal } = this.props;
const { passwordMismatch, understandError, failMessage } = this.state;
return (
<Modal
isOpen
2018-09-26 19:48:07 +02:00
title={__('Encrypt Wallet')}
2018-07-18 21:48:30 +02:00
contentLabel={__('Encrypt Wallet')}
type="confirm"
confirmButtonLabel={__('Encrypt Wallet')}
abortButtonLabel={__('Cancel')}
onConfirmed={() => this.submitEncryptForm()}
onAborted={closeModal}
>
2019-02-13 17:27:20 +01:00
<Form className="card__content" onSubmit={() => this.submitEncryptForm()}>
<p>
{__(
'Encrypting your wallet will require a password to access your local wallet data when LBRY starts. Please enter a new password for your wallet.'
)}{' '}
<Button
button="link"
label={__('Learn more')}
href="https://lbry.io/faq/wallet-encryption"
2019-03-05 05:46:57 +01:00
/>
.
2019-02-13 17:27:20 +01:00
</p>
<fieldset-section>
<FormField
autoFocus
error={passwordMismatch === true ? 'Passwords do not match' : false}
label={__('Password')}
placeholder={__('Shh...')}
type="password"
name="wallet-new-password"
onChange={event => this.onChangeNewPassword(event)}
/>
</fieldset-section>
<fieldset-section>
<FormField
error={passwordMismatch === true ? 'Passwords do not match' : false}
label={__('Confirm Password')}
placeholder={__('Your eyes only')}
type="password"
name="wallet-new-password-confirm"
onChange={event => this.onChangeNewPasswordConfirm(event)}
/>
</fieldset-section>
<div className="help help--warning">
{__(
'If your password is lost, it cannot be recovered. You will not be able to access your wallet without a password.'
)}
</div>
<FormField
error={understandError === true ? 'You must enter "I understand"' : false}
label={__('Enter "I understand"')}
placeholder={__('Dear computer, I understand')}
type="text"
name="wallet-understand"
onChange={event => this.onChangeUnderstandConfirm(event)}
/>
{failMessage && <div className="error-text">{__(failMessage)}</div>}
</Form>
2018-07-18 21:48:30 +02:00
</Modal>
);
}
}
export default ModalWalletEncrypt;