1
0
mirror of https://github.com/trezor/trezor-wallet synced 2025-01-09 15:40:55 +00:00
trezor-wallet/src/reducers/TokensReducer.js

77 lines
2.0 KiB
JavaScript
Raw Normal View History

2018-02-20 09:30:36 +00:00
/* @flow */
2018-07-30 10:52:13 +00:00
2018-08-14 13:11:52 +00:00
import * as CONNECT from 'actions/constants/TrezorConnect';
import * as WALLET from 'actions/constants/wallet';
import * as TOKEN from 'actions/constants/token';
2018-02-20 09:30:36 +00:00
2018-08-14 12:56:47 +00:00
import type { Action, TrezorDevice } from 'flowtype';
2018-04-16 21:19:50 +00:00
2018-02-20 09:30:36 +00:00
export type Token = {
2019-03-04 12:33:02 +00:00
loaded: boolean,
+deviceState: string,
+network: string,
+name: string,
+symbol: string,
+address: string,
+ethAddress: string, // foreign key
+decimals: number,
balance: string,
};
2018-02-20 09:30:36 +00:00
2018-04-16 21:19:50 +00:00
export type State = Array<Token>;
const initialState: State = [];
2018-04-23 10:20:15 +00:00
const create = (state: State, token: Token): State => {
2018-07-30 10:52:13 +00:00
const newState: State = [...state];
2018-02-20 09:30:36 +00:00
newState.push(token);
return newState;
2018-07-30 10:52:13 +00:00
};
2018-02-20 09:30:36 +00:00
2019-03-04 12:33:02 +00:00
const forget = (state: State, device: TrezorDevice): State =>
state.filter(t => t.deviceState !== device.state);
2018-02-20 09:30:36 +00:00
const clear = (state: State, devices: Array<TrezorDevice>): State => {
2018-07-30 10:52:13 +00:00
let newState: State = [...state];
2019-03-04 12:33:02 +00:00
devices.forEach(d => {
newState = forget(newState, d);
});
return newState;
2018-07-30 10:52:13 +00:00
};
2019-03-04 12:33:02 +00:00
const remove = (state: State, token: Token): State =>
state.filter(
t =>
!(
t.ethAddress === token.ethAddress &&
t.address === token.address &&
t.deviceState === token.deviceState
)
);
2018-03-08 16:10:53 +00:00
2018-04-16 21:19:50 +00:00
export default (state: State = initialState, action: Action): State => {
2018-02-20 09:30:36 +00:00
switch (action.type) {
2018-07-30 10:52:13 +00:00
case TOKEN.FROM_STORAGE:
2018-04-16 21:19:50 +00:00
return action.payload;
2018-07-30 10:52:13 +00:00
case TOKEN.ADD:
2018-02-20 09:30:36 +00:00
return create(state, action.payload);
2018-07-30 10:52:13 +00:00
case TOKEN.REMOVE:
2018-04-16 21:19:50 +00:00
return remove(state, action.token);
2018-07-30 10:52:13 +00:00
case TOKEN.SET_BALANCE:
2018-04-23 10:20:15 +00:00
return action.payload;
2018-02-20 09:30:36 +00:00
2018-07-30 10:52:13 +00:00
case CONNECT.FORGET:
case CONNECT.FORGET_SINGLE:
case CONNECT.FORGET_SILENT:
case CONNECT.RECEIVE_WALLET_TYPE:
2018-04-16 21:19:50 +00:00
return forget(state, action.device);
2018-02-20 09:30:36 +00:00
2018-07-30 10:52:13 +00:00
case WALLET.CLEAR_UNAVAILABLE_DEVICE_DATA:
return clear(state, action.devices);
2018-02-20 09:30:36 +00:00
default:
return state;
}
2019-03-04 12:33:02 +00:00
};