From c970ecbc35704d1fe5651edd8707f447cd4d3e52 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Fri, 10 May 2019 18:28:56 +0200 Subject: [PATCH] InputAddress should handle defaultValue of U8a (#1136) * InputAddress should handle defaultValue of U8a * Linting, my old friend --- packages/app-staking/src/Account/index.tsx | 25 ------------ packages/ui-app/src/InputAddress/index.tsx | 47 ++++++++++++---------- 2 files changed, 26 insertions(+), 46 deletions(-) diff --git a/packages/app-staking/src/Account/index.tsx b/packages/app-staking/src/Account/index.tsx index cb4b5d00e6..0df8b44ba0 100644 --- a/packages/app-staking/src/Account/index.tsx +++ b/packages/app-staking/src/Account/index.tsx @@ -93,7 +93,6 @@ class Account extends React.PureComponent { {this.renderStashId()} {this.renderSessionId()} {this.renderNominee()} - {this.renderNominators()} @@ -198,30 +197,6 @@ class Account extends React.PureComponent { ); } - private renderNominators () { - // const { staking_nominatorsFor } = this.props; - - // if (!staking_nominatorsFor) { - // return null; - // } - - // return ( - //
- // - //
- // {staking_nominatorsFor.map((nominator) => ( - // - // ))} - //
- //
- // ); - } - private renderControllerId () { const { controllerId, recentlyOffline, t } = this.props; diff --git a/packages/ui-app/src/InputAddress/index.tsx b/packages/ui-app/src/InputAddress/index.tsx index 292e286c4a..19f59d5fde 100644 --- a/packages/ui-app/src/InputAddress/index.tsx +++ b/packages/ui-app/src/InputAddress/index.tsx @@ -44,18 +44,22 @@ type State = { const STORAGE_KEY = 'options:InputAddress'; const DEFAULT_TYPE = 'all'; +const transformToAddress = (value: string | Uint8Array): string | null => { + try { + return addressToAddress(value) || null; + } catch (error) { + console.error('Unable to transform address', value); + } + + return null; +}; + const transformToAccountId = (value: string): string | null => { if (!value) { return null; } - let accountId; - - try { - accountId = addressToAddress(value); - } catch (error) { - console.error('Unable to transform address', value); - } + const accountId = transformToAddress(value); return !accountId ? null @@ -127,12 +131,21 @@ class InputAddress extends React.PureComponent { const lastValue = InputAddress.getLastValue(type); const lastOption = this.getLastOptionValue(); - const actualValue = isDisabled || (defaultValue && this.hasValue(defaultValue)) - ? defaultValue + const actualValue = transformToAddress( + isDisabled || (defaultValue && this.hasValue(defaultValue)) + ? defaultValue + : ( + this.hasValue(lastValue) + ? lastValue + : (lastOption && lastOption.value) + ) + ); + const actualOptions = options + ? options : ( - this.hasValue(lastValue) - ? lastValue - : (lastOption && lastOption.value) + isDisabled && actualValue + ? [createOption(actualValue)] + : (optionsAll ? optionsAll[type] : []) ); return ( @@ -154,15 +167,7 @@ class InputAddress extends React.PureComponent { : this.onChange } onSearch={this.onSearch} - options={ - options - ? options - : ( - isDisabled && actualValue - ? [createOption(actualValue)] - : (optionsAll ? optionsAll[type] : []) - ) - } + options={actualOptions} placeholder={placeholder} renderLabel={ isMultiple