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

153 lines
4.6 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
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')}
2019-03-20 22:43:00 +01:00
type="confirm"
2018-07-18 21:48:30 +02:00
confirmButtonLabel={__('Encrypt Wallet')}
abortButtonLabel={__('Cancel')}
onConfirmed={() => this.submitEncryptForm()}
onAborted={closeModal}
>
2019-03-20 22:43:00 +01:00
<Form className="card__content" onSubmit={() => this.submitEncryptForm()}>
2019-02-13 17:27:20 +01:00
<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.'
)}{' '}
2019-05-07 23:38:29 +02:00
<Button button="link" label={__('Learn more')} href="https://lbry.com/faq/wallet-encryption" />.
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...')}
2019-03-20 22:43:00 +01:00
type="password"
name="wallet-new-password"
2019-02-13 17:27:20 +01:00
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')}
2019-03-20 22:43:00 +01:00
type="password"
name="wallet-new-password-confirm"
2019-02-13 17:27:20 +01:00
onChange={event => this.onChangeNewPasswordConfirm(event)}
/>
</fieldset-section>
2019-03-20 22:43:00 +01:00
<div className="help help--warning">
2019-02-13 17:27:20 +01:00
{__(
'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')}
2019-03-20 22:43:00 +01:00
type="text"
name="wallet-understand"
2019-02-13 17:27:20 +01:00
onChange={event => this.onChangeUnderstandConfirm(event)}
/>
2019-03-20 22:43:00 +01:00
{failMessage && <div className="error-text">{__(failMessage)}</div>}
</Form>
2018-07-18 21:48:30 +02:00
</Modal>
);
}
}
export default ModalWalletEncrypt;