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/BlockchainReducer.js

61 lines
1.5 KiB

/* @flow */
import { BLOCKCHAIN } from 'trezor-connect';
import type { Action } from 'flowtype';
export type BlockchainNetwork = {
+shortcut: string;
connected: boolean;
}
export type State = Array<BlockchainNetwork>;
export const initialState: State = [];
const find = (state: State, shortcut: string): number => state.findIndex(b => b.shortcut === shortcut);
const connect = (state: State, action: any): State => {
const shortcut = action.payload.coin.shortcut.toLowerCase();
const network: BlockchainNetwork = {
shortcut,
connected: true,
};
const newState: State = [...state];
const index: number = find(newState, shortcut);
if (index >= 0) {
newState[index] = network;
} else {
newState.push(network);
}
return newState;
};
const disconnect = (state: State, action: any): State => {
const shortcut = action.payload.coin.shortcut.toLowerCase();
const network: BlockchainNetwork = {
shortcut,
connected: false,
};
const newState: State = [...state];
const index: number = find(newState, shortcut);
if (index >= 0) {
newState[index] = network;
} else {
newState.push(network);
}
return newState;
};
export default (state: State = initialState, action: Action): State => {
switch (action.type) {
case BLOCKCHAIN.CONNECT:
return connect(state, action);
case BLOCKCHAIN.ERROR:
return disconnect(state, action);
default:
return state;
}
};