You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
trezor-wallet/src/reducers/TokensReducer.js

77 lines
2.0 KiB

6 years ago
/* @flow */
import * as CONNECT from 'actions/constants/TrezorConnect';
import * as WALLET from 'actions/constants/wallet';
import * as TOKEN from 'actions/constants/token';
6 years ago
import type { Action, TrezorDevice } from 'flowtype';
6 years ago
export type Token = {
5 years ago
loaded: boolean,
+deviceState: string,
+network: string,
+name: string,
+symbol: string,
+address: string,
+ethAddress: string, // foreign key
+decimals: number,
balance: string,
};
6 years ago
export type State = Array<Token>;
const initialState: State = [];
6 years ago
const create = (state: State, token: Token): State => {
const newState: State = [...state];
6 years ago
newState.push(token);
return newState;
};
6 years ago
5 years ago
const forget = (state: State, device: TrezorDevice): State =>
state.filter(t => t.deviceState !== device.state);
6 years ago
const clear = (state: State, devices: Array<TrezorDevice>): State => {
let newState: State = [...state];
5 years ago
devices.forEach(d => {
newState = forget(newState, d);
});
return newState;
};
5 years ago
const remove = (state: State, token: Token): State =>
state.filter(
t =>
!(
t.ethAddress === token.ethAddress &&
t.address === token.address &&
t.deviceState === token.deviceState
)
);
6 years ago
export default (state: State = initialState, action: Action): State => {
6 years ago
switch (action.type) {
case TOKEN.FROM_STORAGE:
return action.payload;
case TOKEN.ADD:
6 years ago
return create(state, action.payload);
case TOKEN.REMOVE:
return remove(state, action.token);
case TOKEN.SET_BALANCE:
6 years ago
return action.payload;
6 years ago
case CONNECT.FORGET:
case CONNECT.FORGET_SINGLE:
case CONNECT.FORGET_SILENT:
case CONNECT.RECEIVE_WALLET_TYPE:
return forget(state, action.device);
6 years ago
case WALLET.CLEAR_UNAVAILABLE_DEVICE_DATA:
return clear(state, action.devices);
6 years ago
default:
return state;
}
5 years ago
};