pull/495/head
slowbackspace 5 years ago
parent 27f35b097d
commit bb4a3268f7

@ -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<void> => {
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,

@ -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 => {

@ -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<Account> = dispatch(getImportedAccounts());
export const setImportedAccount = (account: Account): ThunkAction => (): void => {
const prevImportedAccounts: ?Array<Account> = 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<Account> => {
export const getImportedAccounts = (): ?Array<Account> => {
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<Account> = dispatch(getImportedAccounts());
const importedAccounts: ?Array<Account> = getImportedAccounts();
if (!importedAccounts) return;
const deviceId = device.features ? device.features.device_id : null;

@ -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;
};

@ -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<State, 'connect'>, 'transport'>,
device: ?Device,
device: ?TrezorDevice,
config: Config,
children?: React.Node,
};

@ -54,7 +54,7 @@ const Import = (props: Props) => {
<StyledSelect
value={selectedNetwork}
options={networks
.sort((a, b) => a.shortcut > b.shortcut)
.sort((a, b) => a.shortcut.localeCompare(b.shortcut))
.map(net => ({
label: net.shortcut,
value: net,

Loading…
Cancel
Save