From a66577eda4412e7b5f649d7c71a9049d2d132ad7 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Thu, 19 Sep 2019 17:57:32 +0200 Subject: [PATCH] More functional component conversions (#1633) * More functional component conversions * Cleanups * functional Elapsed * Status -> functional * Couple more * One more tiny one --- packages/app-accounts/src/modals/Create.tsx | 198 +++++++----------- .../src/modals/CreateConfirmation.tsx | 57 +++++ packages/apps/src/Content/NotFound.tsx | 12 +- packages/apps/src/Content/Status.tsx | 73 +++---- packages/apps/src/Content/index.tsx | 110 +++++----- packages/apps/src/overlays/Accounts.tsx | 80 +++---- packages/apps/src/overlays/Base.tsx | 61 +++--- .../react-components/src/Status/Queue.tsx | 28 +-- packages/react-components/src/Status/types.ts | 2 +- packages/react-query/src/Elapsed.tsx | 104 +++++---- 10 files changed, 346 insertions(+), 379 deletions(-) create mode 100644 packages/app-accounts/src/modals/CreateConfirmation.tsx diff --git a/packages/app-accounts/src/modals/Create.tsx b/packages/app-accounts/src/modals/Create.tsx index c81ce5b186..3efe6b78f9 100644 --- a/packages/app-accounts/src/modals/Create.tsx +++ b/packages/app-accounts/src/modals/Create.tsx @@ -21,6 +21,7 @@ import { isHex, u8aToHex } from '@polkadot/util'; import { keyExtractSuri, mnemonicGenerate, mnemonicValidate, randomAsU8a } from '@polkadot/util-crypto'; import translate from '../translate'; +import CreateConfirmation from './CreateConfirmation'; interface Props extends ModalProps, ApiProps, I18nProps { seed?: string; @@ -34,20 +35,23 @@ interface SeedOption { value: SeedType; } -interface State { +interface AddressState { address: string; deriveError: string | null; derivePath: string; - isNameValid: boolean; isSeedValid: boolean; + pairType: KeypairType; + seed: string; + seedType: SeedType; +} + +interface State extends AddressState { + isNameValid: boolean; isPassValid: boolean; isValid: boolean; name: string; - pairType: KeypairType; password: string; - seed: string; seedOptions: SeedOption[]; - seedType: SeedType; showWarning: boolean; tags: string[]; } @@ -87,6 +91,30 @@ function addressFromSeed (phrase: string, derivePath: string, pairType: KeypairT .address; } +function generateSeed (_seed: string | null, derivePath: string, seedType: SeedType, pairType: KeypairType): AddressState { + const seed = ((): string => { + switch (seedType) { + case 'bip': + return mnemonicGenerate(); + case 'dev': + return DEV_PHRASE; + default: + return _seed || u8aToHex(randomAsU8a()); + } + })(); + const address = addressFromSeed(seed, derivePath, pairType); + + return { + address, + deriveError: null, + derivePath, + isSeedValid: true, + pairType, + seedType, + seed + }; +} + class Create extends React.PureComponent { public state: State; @@ -103,14 +131,25 @@ class Create extends React.PureComponent { seedOptions.push({ value: 'dev', text: t('Development') }); } + const pairType = type || DEFAULT_TYPE; + const seedType = seed ? 'raw' : 'bip'; + this.state = { - ...(this.emptyState(seed || null, '', type || DEFAULT_TYPE) as State), - seedOptions + ...generateSeed(seed || null, '', seedType, pairType), + isNameValid: true, + isPassValid: false, + isValid: false, + name: 'new account', + password: '', + seedOptions, + showWarning: false, + tags: [] }; } public render (): React.ReactNode { const { className, t } = this.props; + const { address, isValid, showWarning } = this.state; return ( { open > {t('Add an account via seed')} - {this.renderModal()} + {showWarning && ( + + )} {this.renderInput()} - {this.renderButtons()} + + +