From c49ab94e0e4776cefe61a9755f3041f5685d392f Mon Sep 17 00:00:00 2001 From: Tobiasz Cudnik Date: Fri, 28 Jun 2019 00:04:49 +0200 Subject: [PATCH] delegate dialog #229 --- .../content/RegisterDelegateDialogContent.tsx | 216 ++++++++++++++---- .../wallet/RegisterDelegateDialog.tsx | 15 +- 2 files changed, 178 insertions(+), 53 deletions(-) diff --git a/src/components/content/RegisterDelegateDialogContent.tsx b/src/components/content/RegisterDelegateDialogContent.tsx index 1f73c08..c20e201 100644 --- a/src/components/content/RegisterDelegateDialogContent.tsx +++ b/src/components/content/RegisterDelegateDialogContent.tsx @@ -8,6 +8,7 @@ import { WithStyles, withStyles } from '@material-ui/core/styles'; +import { Rise } from 'dpos-offline'; import React from 'react'; import { ChangeEvent, FormEvent, ReactEventHandler } from 'react'; import { @@ -16,6 +17,7 @@ import { InjectedIntlProps, injectIntl } from 'react-intl'; +import AccountIcon from '../AccountIcon'; import { DialogContentProps, SetDialogContent, @@ -26,7 +28,9 @@ import { RawAmount } from '../../utils/amounts'; import { normalizeAddress, normalizeUsername, - formatAmount + formatAmount, + normalizeMnemonic, + AccountIDVersion } from '../../utils/utils'; const styles = (theme: Theme) => @@ -34,6 +38,10 @@ const styles = (theme: Theme) => content: { padding: theme.spacing.unit * 2, textAlign: 'center' + }, + accountContainer: { + display: 'flex', + alignItems: 'center' } }); @@ -51,12 +59,16 @@ interface Props extends BaseProps, ICloseInterruptFormProps { username: string; onUsernameChange: (username: string) => void; error?: null | 'already-registered' | 'insufficient-funds'; + getPublicKey(mnemonic: string): string; } type DecoratedProps = Props & InjectedIntlProps; interface State { usernameInvalid: boolean; + address: string | null; + mnemonic: string; + mnemonicInvalid: boolean; } const messages = defineMessages({ @@ -92,6 +104,11 @@ const messages = defineMessages({ defaultMessage: 'Invalid delegate username. The username cannot contain ' + 'uppercase characters' + }, + invalidMnemonicGeneric: { + id: 'forms-register-delegate.invalid-mnemonic-generic', + description: 'Error label for invalid mnemonic text input', + defaultMessage: 'Invalid mnemonic. A valid mnemonic is a list of 12 words.' } }); @@ -102,15 +119,14 @@ class RegisterDelegateDialogContent extends React.Component< @autoId dialogContentId: string; state: State = { - usernameInvalid: false + usernameInvalid: false, + address: null, + mnemonic: null, + mnemonicInvalid: null }; constructor(props: DecoratedProps) { super(props); - - this.state = { - usernameInvalid: false - }; } handleUsernameChange = (ev: ChangeEvent) => { @@ -123,13 +139,13 @@ class RegisterDelegateDialogContent extends React.Component< onUsernameChange(username); onFormChanged(Boolean(username)); - } + }; handleUsernameBlur = () => { const { username } = this.props; const usernameInvalid = !!username && !!this.usernameError(); this.setState({ usernameInvalid }); - } + }; handleFormSubmit = (ev: FormEvent) => { ev.preventDefault(); @@ -145,7 +161,33 @@ class RegisterDelegateDialogContent extends React.Component< } onSubmit(); - } + }; + + handleMnemonicChange = (ev: React.ChangeEvent) => { + const mnemonic = ev.target.value; + + this.setState({ + address: this.getAddressFromMnemonic(mnemonic), + mnemonic, + mnemonicInvalid: false + }); + }; + + getAddressFromMnemonic = (mnemonic: string) => { + const { getPublicKey } = this.props; + const normalized = normalizeMnemonic(mnemonic); + if (normalized) { + return getPublicKey(mnemonic); + } + return null; + }; + + handleMnemonicBlur = () => { + const { mnemonic } = this.state; + if (!this.getAddressFromMnemonic(mnemonic)) { + this.setState({ mnemonicInvalid: true }); + } + }; usernameError(): string | null { const { intl, username } = this.props; @@ -172,6 +214,17 @@ class RegisterDelegateDialogContent extends React.Component< }); } + mnemonicError(): string | null { + const { intl } = this.props; + const { address } = this.state; + + if (address) { + return null; + } + + return intl.formatMessage(messages.invalidMnemonicGeneric); + } + render() { const { intl, @@ -212,7 +265,7 @@ class RegisterDelegateDialogContent extends React.Component< id="forms-register-delegate.insufficient-funds-error" description="Error about not having enough funds to register as a delegate" defaultMessage={ - 'You don\'t have enough funds in your account to pay the network fee ' + + "You don't have enough funds in your account to pay the network fee " + 'of {fee} for registering as a delegate!' } values={{ fee: formatAmount(intl, delegateFee) }} @@ -220,43 +273,8 @@ class RegisterDelegateDialogContent extends React.Component< )} - {error === 'already-registered' && ( - - - - - - )} - {!error && ( - - - } - value={username} - fullWidth={true} - error={usernameInvalid} - FormHelperTextProps={{ - error: usernameInvalid - }} - helperText={usernameInvalid ? this.usernameError() || '' : ''} - onChange={this.handleUsernameChange} - onBlur={this.handleUsernameBlur} - /> - - )} + {this.renderUsername()} + {this.renderForgingKey()} {!error && (