mirror of https://github.com/trezor/trezor-wallet
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.
62 lines
1.9 KiB
62 lines
1.9 KiB
6 years ago
|
/* @flow */
|
||
6 years ago
|
|
||
6 years ago
|
|
||
6 years ago
|
import { DEVICE } from 'trezor-connect';
|
||
6 years ago
|
import { LOCATION_CHANGE } from 'react-router-redux';
|
||
6 years ago
|
import * as WALLET from 'actions/constants/wallet';
|
||
6 years ago
|
|
||
6 years ago
|
import * as WalletActions from 'actions/WalletActions';
|
||
|
import * as LocalStorageActions from 'actions/LocalStorageActions';
|
||
|
import * as TrezorConnectActions from 'actions/TrezorConnectActions';
|
||
|
import * as SelectedAccountActions from 'actions/SelectedAccountActions';
|
||
6 years ago
|
|
||
6 years ago
|
import type {
|
||
6 years ago
|
Middleware,
|
||
|
MiddlewareAPI,
|
||
|
MiddlewareDispatch,
|
||
|
Action,
|
||
6 years ago
|
} from 'flowtype';
|
||
6 years ago
|
|
||
|
/**
|
||
6 years ago
|
* Middleware
|
||
6 years ago
|
*/
|
||
|
const WalletService: Middleware = (api: MiddlewareAPI) => (next: MiddlewareDispatch) => (action: Action): Action => {
|
||
|
const prevState = api.getState();
|
||
|
const locationChange: boolean = action.type === LOCATION_CHANGE;
|
||
|
|
||
|
// Application live cycle starts here
|
||
|
if (locationChange) {
|
||
|
const { location } = api.getState().router;
|
||
|
if (!location) {
|
||
6 years ago
|
api.dispatch(WalletActions.init());
|
||
6 years ago
|
// load data from config.json and local storage
|
||
6 years ago
|
api.dispatch(LocalStorageActions.loadData());
|
||
6 years ago
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
// pass action
|
||
6 years ago
|
next(action);
|
||
|
|
||
6 years ago
|
if (action.type === DEVICE.CONNECT) {
|
||
6 years ago
|
api.dispatch(WalletActions.clearUnavailableDevicesData(prevState, action.device));
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
// update common values in WallerReducer
|
||
6 years ago
|
api.dispatch(WalletActions.updateSelectedValues(prevState, action));
|
||
6 years ago
|
|
||
6 years ago
|
// update common values in SelectedAccountReducer
|
||
6 years ago
|
api.dispatch(SelectedAccountActions.updateSelectedValues(prevState, action));
|
||
6 years ago
|
|
||
6 years ago
|
// selected device changed
|
||
|
if (action.type === WALLET.SET_SELECTED_DEVICE) {
|
||
|
if (action.device) {
|
||
6 years ago
|
api.dispatch(TrezorConnectActions.getSelectedDeviceState());
|
||
6 years ago
|
} else {
|
||
6 years ago
|
api.dispatch(TrezorConnectActions.switchToFirstAvailableDevice());
|
||
6 years ago
|
}
|
||
|
}
|
||
|
|
||
|
return action;
|
||
|
};
|
||
|
|
||
|
export default WalletService;
|