MyCrypto/common/reducers/wallet.js

41 lines
804 B
JavaScript
Raw Normal View History

2017-06-29 16:03:11 -07:00
// @flow
2017-07-03 20:21:19 -07:00
import type {
WalletAction,
2017-07-04 05:19:04 -07:00
SaveWalletAction
// InitWalletAction
2017-07-03 20:21:19 -07:00
} from 'actions/wallet';
2017-06-29 16:03:11 -07:00
import BaseWallet from 'libs/wallet/base';
2017-07-03 16:59:27 -07:00
export type State = {
2017-07-03 20:21:19 -07:00
inst: ?BaseWallet,
balance: number,
tokens: {
[string]: number
}
2017-07-03 16:59:27 -07:00
};
2017-06-29 16:03:11 -07:00
2017-07-03 16:59:27 -07:00
const initialState: State = {
2017-07-03 20:21:19 -07:00
inst: null,
balance: 0,
tokens: {}
2017-07-03 16:59:27 -07:00
};
2017-06-29 16:03:11 -07:00
function saveWallet(state: State, action: SaveWalletAction): State {
2017-07-03 20:21:19 -07:00
return { ...state, inst: action.payload };
2017-07-03 16:59:27 -07:00
}
function initWallet(state: State): State {
2017-07-03 20:21:19 -07:00
return { ...state, balance: 0, tokens: {} };
2017-06-29 16:03:11 -07:00
}
2017-07-04 05:19:04 -07:00
export function wallet(state: State = initialState, action: WalletAction): State {
2017-07-03 20:21:19 -07:00
switch (action.type) {
case 'WALLET_SAVE':
return saveWallet(state, action);
case 'WALLET_INIT':
return initWallet(state);
default:
return state;
}
2017-06-29 16:03:11 -07:00
}