1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-12-04 14:18:12 +00:00
trezor-wallet/src/js/reducers/AccountsReducer.js

98 lines
3.0 KiB
JavaScript
Raw Normal View History

2018-02-20 09:30:36 +00:00
/* @flow */
'use strict';
import * as CONNECT from '../actions/constants/TrezorConnect';
2018-04-11 10:13:38 +00:00
import * as ADDRESS from '../actions/constants/address';
2018-02-20 09:30:36 +00:00
export type Account = {
loaded: boolean;
+network: string;
+deviceID: string;
+deviceState: ?string;
2018-02-20 09:30:36 +00:00
+index: number;
+addressPath: Array<number>;
+address: string;
balance: string;
nonce: number;
}
const initialState: Array<Account> = [];
const createAccount = (state: Array<Account>, action: any): Array<Account> => {
// TODO check with device_id
2018-02-20 09:30:36 +00:00
// check if account was created before
const exist: ?Account = state.find((account: Account) => account.address === action.address && account.network === action.network && account.deviceID === action.device.features.device_id);
console.warn("MAM?", exist, action)
2018-02-20 09:30:36 +00:00
if (exist) {
return state;
}
const address: Account = {
loaded: false,
network: action.network,
deviceID: action.device.features.device_id,
deviceState: action.device.state,
2018-02-20 09:30:36 +00:00
index: action.index,
addressPath: action.path,
address: action.address,
balance: '0',
nonce: 0,
}
const newState: Array<Account> = [ ...state ];
newState.push(address);
return newState;
}
const removeAccounts = (state: Array<Account>, action: any): Array<Account> => {
return state.filter(account => account.deviceID !== action.device.features.device_id);
2018-02-20 09:30:36 +00:00
}
const forgetAccounts = (state: Array<Account>, action: any): Array<Account> => {
return state.filter(account => action.accounts.indexOf(account) === -1);
2018-02-20 09:30:36 +00:00
}
const setBalance = (state: Array<Account>, action: any): Array<Account> => {
const index: number = state.findIndex(account => account.address === action.address && account.network === action.network);
2018-02-20 09:30:36 +00:00
const newState: Array<Account> = [ ...state ];
newState[index].loaded = true;
newState[index].balance = action.balance;
return newState;
}
const setNonce = (state: Array<Account>, action: any): Array<Account> => {
const index: number = state.findIndex(account => account.address === action.address && account.network === action.network);
2018-02-20 09:30:36 +00:00
const newState: Array<Account> = [ ...state ];
newState[index].loaded = true;
newState[index].nonce = action.nonce;
return newState;
}
export default (state: Array<Account> = initialState, action: any): Array<Account> => {
switch (action.type) {
case ADDRESS.CREATE :
return createAccount(state, action);
case CONNECT.FORGET :
case CONNECT.FORGET_SINGLE :
return removeAccounts(state, action);
//case CONNECT.FORGET_SINGLE :
// return forgetAccounts(state, action);
case ADDRESS.SET_BALANCE :
return setBalance(state, action);
case ADDRESS.SET_NONCE :
return setNonce(state, action);
case ADDRESS.FROM_STORAGE :
return action.payload;
default:
return state;
}
}