2017-12-13 11:01:37 +00:00
|
|
|
/* @flow */
|
|
|
|
|
2018-09-21 12:01:41 +00:00
|
|
|
import {
|
|
|
|
TRANSPORT, DEVICE, BLOCKCHAIN,
|
2018-07-30 10:52:13 +00:00
|
|
|
} from 'trezor-connect';
|
2018-08-14 13:11:52 +00:00
|
|
|
import * as TrezorConnectActions from 'actions/TrezorConnectActions';
|
2018-09-12 11:25:21 +00:00
|
|
|
import * as BlockchainActions from 'actions/BlockchainActions';
|
2018-09-20 16:26:45 +00:00
|
|
|
import * as RouterActions from 'actions/RouterActions';
|
2018-08-14 13:11:52 +00:00
|
|
|
import * as ModalActions from 'actions/ModalActions';
|
|
|
|
import * as STORAGE from 'actions/constants/localStorage';
|
|
|
|
import * as CONNECT from 'actions/constants/TrezorConnect';
|
2018-09-13 12:40:41 +00:00
|
|
|
import { READY as BLOCKCHAIN_READY } from 'actions/constants/blockchain';
|
2017-12-13 11:01:37 +00:00
|
|
|
|
2018-07-30 10:52:13 +00:00
|
|
|
import type {
|
2018-04-16 21:19:50 +00:00
|
|
|
Middleware,
|
|
|
|
MiddlewareAPI,
|
|
|
|
MiddlewareDispatch,
|
|
|
|
State,
|
|
|
|
Action,
|
2018-08-14 12:56:47 +00:00
|
|
|
} from 'flowtype';
|
2017-12-13 11:01:37 +00:00
|
|
|
|
2018-04-16 21:19:50 +00:00
|
|
|
const TrezorConnectService: Middleware = (api: MiddlewareAPI) => (next: MiddlewareDispatch) => (action: Action): Action => {
|
2018-09-06 15:04:28 +00:00
|
|
|
// const prevState: $ElementType<State, 'connect'> = api.getState().connect;
|
2018-04-16 21:19:50 +00:00
|
|
|
const prevModalState: $ElementType<State, 'modal'> = api.getState().modal;
|
2018-09-06 15:04:28 +00:00
|
|
|
// const prevRouterState: $ElementType<State, 'router'> = api.getState().router;
|
2018-02-20 09:30:36 +00:00
|
|
|
|
2017-12-13 11:01:37 +00:00
|
|
|
next(action);
|
|
|
|
|
2018-02-20 09:30:36 +00:00
|
|
|
if (action.type === STORAGE.READY) {
|
2018-07-30 10:52:13 +00:00
|
|
|
api.dispatch(TrezorConnectActions.init());
|
2018-02-20 09:30:36 +00:00
|
|
|
} else if (action.type === TRANSPORT.ERROR) {
|
2018-04-11 10:06:46 +00:00
|
|
|
// TODO: check if modal is open
|
2018-09-20 16:26:45 +00:00
|
|
|
// api.dispatch( RouterActions.gotoLandingPage() );
|
2018-09-03 15:43:17 +00:00
|
|
|
} else if (action.type === TRANSPORT.START) {
|
2018-09-13 12:40:41 +00:00
|
|
|
api.dispatch(BlockchainActions.init());
|
|
|
|
} else if (action.type === BLOCKCHAIN_READY) {
|
2018-07-30 10:52:13 +00:00
|
|
|
api.dispatch(TrezorConnectActions.postInit());
|
2018-02-20 09:30:36 +00:00
|
|
|
} else if (action.type === DEVICE.DISCONNECT) {
|
2018-07-30 10:52:13 +00:00
|
|
|
api.dispatch(TrezorConnectActions.deviceDisconnect(action.device));
|
2018-03-08 16:10:53 +00:00
|
|
|
} else if (action.type === CONNECT.REMEMBER_REQUEST) {
|
2018-07-30 10:52:13 +00:00
|
|
|
api.dispatch(ModalActions.onRememberRequest(prevModalState));
|
2018-02-20 09:30:36 +00:00
|
|
|
} else if (action.type === CONNECT.FORGET) {
|
2018-04-16 21:19:50 +00:00
|
|
|
//api.dispatch( TrezorConnectActions.forgetDevice(action.device) );
|
2018-09-21 12:01:41 +00:00
|
|
|
api.dispatch(RouterActions.selectFirstAvailableDevice());
|
2018-02-20 09:30:36 +00:00
|
|
|
} else if (action.type === CONNECT.FORGET_SINGLE) {
|
2018-05-23 08:57:36 +00:00
|
|
|
if (api.getState().devices.length < 1 && action.device.connected) {
|
2018-05-05 11:52:03 +00:00
|
|
|
// prompt disconnect device info in LandingPage
|
2018-04-16 21:19:50 +00:00
|
|
|
api.dispatch({
|
2018-02-20 09:30:36 +00:00
|
|
|
type: CONNECT.DISCONNECT_REQUEST,
|
2018-07-30 10:52:13 +00:00
|
|
|
device: action.device,
|
2017-12-13 11:01:37 +00:00
|
|
|
});
|
2018-09-21 12:01:41 +00:00
|
|
|
api.dispatch(RouterActions.gotoLandingPage());
|
2018-02-20 09:30:36 +00:00
|
|
|
} else {
|
2018-09-21 12:01:41 +00:00
|
|
|
api.dispatch(RouterActions.selectFirstAvailableDevice());
|
2018-02-20 09:30:36 +00:00
|
|
|
}
|
|
|
|
} else if (action.type === DEVICE.CONNECT || action.type === DEVICE.CONNECT_UNACQUIRED) {
|
2018-07-30 10:52:13 +00:00
|
|
|
api.dispatch(ModalActions.onDeviceConnect(action.device));
|
2018-02-20 09:30:36 +00:00
|
|
|
} else if (action.type === CONNECT.DUPLICATE) {
|
2018-09-20 16:26:45 +00:00
|
|
|
api.dispatch(RouterActions.selectDevice(action.device));
|
2018-09-12 11:25:21 +00:00
|
|
|
} else if (action.type === BLOCKCHAIN.BLOCK) {
|
2018-09-13 12:40:41 +00:00
|
|
|
api.dispatch(BlockchainActions.onBlockMined(action.payload.coin));
|
2018-09-12 11:25:21 +00:00
|
|
|
} else if (action.type === BLOCKCHAIN.NOTIFICATION) {
|
|
|
|
// api.dispatch(BlockchainActions.onNotification(action.payload));
|
2018-09-13 12:40:41 +00:00
|
|
|
} else if (action.type === BLOCKCHAIN.ERROR) {
|
2018-09-21 12:01:41 +00:00
|
|
|
api.dispatch(BlockchainActions.error(action.payload));
|
2017-12-13 11:01:37 +00:00
|
|
|
}
|
2018-04-16 21:19:50 +00:00
|
|
|
|
|
|
|
return action;
|
2018-07-30 10:52:13 +00:00
|
|
|
};
|
2017-12-13 11:01:37 +00:00
|
|
|
|
|
|
|
export default TrezorConnectService;
|