MyCrypto/common/reducers/wallet.js

41 lines
804 B
JavaScript
Raw Normal View History

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