diff --git a/src/actions/AccountsActions.js b/src/actions/AccountsActions.js index 267cdb41..83b705d4 100644 --- a/src/actions/AccountsActions.js +++ b/src/actions/AccountsActions.js @@ -2,7 +2,7 @@ import * as ACCOUNT from 'actions/constants/account'; import * as NOTIFICATION from 'actions/constants/notification'; -import type { Action, TrezorDevice, Network } from 'flowtype'; +import type { Action, AsyncAction, TrezorDevice, Network, Dispatch, GetState } from 'flowtype'; import type { Account, State } from 'reducers/AccountsReducer'; import * as BlockchainActions from 'actions/ethereum/BlockchainActions'; import * as LocalStorageActions from 'actions/LocalStorageActions'; @@ -27,13 +27,17 @@ export const update = (account: Account): Action => ({ export const importAddress = ( address: string, network: Network, - device: TrezorDevice + device: ?TrezorDevice ): AsyncAction => async (dispatch: Dispatch, getState: GetState): Promise => { if (!device) return; let payload; const index = getState().accounts.filter( - a => a.imported === true && a.network === network.shortcut && a.deviceState === device.state + a => + a.imported === true && + a.network === network.shortcut && + device && + a.deviceState === device.state ).length; try { @@ -58,9 +62,22 @@ export const importAddress = ( transactions: account.transactions, empty, - networkType: network.type, + networkType: 'ethereum', nonce: account.nonce, }; + dispatch({ + type: ACCOUNT.CREATE, + payload, + }); + dispatch(LocalStorageActions.setImportedAccount(payload)); + dispatch({ + type: NOTIFICATION.ADD, + payload: { + type: 'success', + title: 'The account has been successfully imported', + cancelable: true, + }, + }); } else if (network.type === 'ripple') { const response = await TrezorConnect.rippleGetAccountInfo({ account: { @@ -93,24 +110,24 @@ export const importAddress = ( transactions: account.transactions, empty, - networkType: network.type, + networkType: 'ripple', sequence: account.sequence, reserve: toDecimalAmount(account.reserve, network.decimals), }; + dispatch({ + type: ACCOUNT.CREATE, + payload, + }); + dispatch(LocalStorageActions.setImportedAccount(payload)); + dispatch({ + type: NOTIFICATION.ADD, + payload: { + type: 'success', + title: 'The account has been successfully imported', + cancelable: true, + }, + }); } - dispatch({ - type: ACCOUNT.CREATE, - payload, - }); - dispatch(LocalStorageActions.setImportedAccount(payload)); - dispatch({ - type: NOTIFICATION.ADD, - payload: { - type: 'success', - title: 'The account has been successfully imported', - cancelable: true, - }, - }); } catch (error) { dispatch({ type: NOTIFICATION.ADD, diff --git a/src/actions/DiscoveryActions.js b/src/actions/DiscoveryActions.js index 4ecdcd0f..57e97251 100644 --- a/src/actions/DiscoveryActions.js +++ b/src/actions/DiscoveryActions.js @@ -385,7 +385,7 @@ export const addAccount = (): ThunkAction => (dispatch: Dispatch, getState: GetS export const addImportedAccounts = (): ThunkAction => (dispatch: Dispatch): void => { // get imported accounts from local storage - const importedAccounts = dispatch(LocalStorageActions.getImportedAccounts()); + const importedAccounts = LocalStorageActions.getImportedAccounts(); if (importedAccounts) { // create each account importedAccounts.forEach(account => { diff --git a/src/actions/LocalStorageActions.js b/src/actions/LocalStorageActions.js index fd801692..2a2a2736 100644 --- a/src/actions/LocalStorageActions.js +++ b/src/actions/LocalStorageActions.js @@ -323,8 +323,8 @@ export const setLocalCurrency = (): ThunkAction => ( storageUtils.set(TYPE, KEY_LOCAL_CURRENCY, JSON.stringify(localCurrency)); }; -export const setImportedAccount = (account: Account): ThunkAction => (dispatch: Dispatch): void => { - const prevImportedAccounts: ?Array = dispatch(getImportedAccounts()); +export const setImportedAccount = (account: Account): ThunkAction => (): void => { + const prevImportedAccounts: ?Array = getImportedAccounts(); let importedAccounts = [account]; if (prevImportedAccounts) { importedAccounts = importedAccounts.concat(prevImportedAccounts); @@ -332,18 +332,18 @@ export const setImportedAccount = (account: Account): ThunkAction => (dispatch: storageUtils.set(TYPE, KEY_IMPORTED_ACCOUNTS, JSON.stringify(importedAccounts)); }; -export const getImportedAccounts = (): ThunkAction => (): ?Array => { +export const getImportedAccounts = (): ?Array => { const importedAccounts: ?string = storageUtils.get(TYPE, KEY_IMPORTED_ACCOUNTS); if (importedAccounts) { return JSON.parse(importedAccounts); } - return importedAccounts; + return null; }; export const removeImportedAccounts = (device: TrezorDevice): ThunkAction => ( dispatch: Dispatch ): void => { - const importedAccounts: ?Array = dispatch(getImportedAccounts()); + const importedAccounts: ?Array = getImportedAccounts(); if (!importedAccounts) return; const deviceId = device.features ? device.features.device_id : null; diff --git a/src/reducers/AccountsReducer.js b/src/reducers/AccountsReducer.js index 304f13a6..3fe36f19 100644 --- a/src/reducers/AccountsReducer.js +++ b/src/reducers/AccountsReducer.js @@ -78,7 +78,7 @@ const createAccount = (state: State, account: Account): State => { // sort the accounts array so the imported accounts always come before discovered accounts if (account.imported) { - newState.sort((a, b) => b.imported - a.imported || a.index - b.index); + newState.sort((a, b) => Number(b.imported) - Number(a.imported) || a.index - b.index); } return newState; }; diff --git a/src/views/Wallet/views/Import/Container.js b/src/views/Wallet/views/Import/Container.js index 347d9a82..1759aa51 100644 --- a/src/views/Wallet/views/Import/Container.js +++ b/src/views/Wallet/views/Import/Container.js @@ -5,12 +5,12 @@ import { bindActionCreators } from 'redux'; import * as AccountsAction from 'actions/AccountsActions'; import type { MapStateToProps, MapDispatchToProps } from 'react-redux'; -import type { Device, State, Dispatch } from 'flowtype'; +import type { TrezorDevice, Config, State, Dispatch } from 'flowtype'; import ImportView from './index'; export type StateProps = { - transport: $ElementType<$ElementType, 'transport'>, - device: ?Device, + device: ?TrezorDevice, + config: Config, children?: React.Node, }; diff --git a/src/views/Wallet/views/Import/index.js b/src/views/Wallet/views/Import/index.js index 6f821153..4216a1ba 100644 --- a/src/views/Wallet/views/Import/index.js +++ b/src/views/Wallet/views/Import/index.js @@ -54,7 +54,7 @@ const Import = (props: Props) => { a.shortcut > b.shortcut) + .sort((a, b) => a.shortcut.localeCompare(b.shortcut)) .map(net => ({ label: net.shortcut, value: net,