From e5bdadfcb25de49c1c58a46b51685018c39ac1dc Mon Sep 17 00:00:00 2001 From: Szymon Lesisz Date: Tue, 22 May 2018 19:42:03 +0200 Subject: [PATCH] added WalletService to middlewares --- src/js/index.js | 2 +- src/js/services/LocalStorageService.js | 16 +++--- src/js/services/WalletService.js | 70 ++++++++++++++++++++++++++ src/js/services/index.js | 2 + 4 files changed, 81 insertions(+), 9 deletions(-) create mode 100644 src/js/services/WalletService.js diff --git a/src/js/index.js b/src/js/index.js index 2c624e4a..03907538 100644 --- a/src/js/index.js +++ b/src/js/index.js @@ -22,4 +22,4 @@ if (typeof module !== undefined && module.hasOwnProperty('hot')) { module.hot.accept(); } -// Application life cycle starts in ./services/LocalStorageService.js \ No newline at end of file +// Application life cycle starts in ./services/WalletService.js \ No newline at end of file diff --git a/src/js/services/LocalStorageService.js b/src/js/services/LocalStorageService.js index 96d82f06..9ef94c3e 100644 --- a/src/js/services/LocalStorageService.js +++ b/src/js/services/LocalStorageService.js @@ -89,14 +89,14 @@ const save = (dispatch: Dispatch, getState: GetState): void => { const LocalStorageService: Middleware = (api: MiddlewareAPI) => (next: MiddlewareDispatch) => (action: Action): Action => { // Application live cycle starts here - if (action.type === LOCATION_CHANGE) { - const { location } = api.getState().router; - if (!location) { - api.dispatch( WalletActions.init() ); - // load data from config.json and local storage - api.dispatch( LocalStorageActions.loadData() ); - } - } + // if (action.type === LOCATION_CHANGE) { + // const { location } = api.getState().router; + // if (!location) { + // api.dispatch( WalletActions.init() ); + // // load data from config.json and local storage + // api.dispatch( LocalStorageActions.loadData() ); + // } + // } next(action); diff --git a/src/js/services/WalletService.js b/src/js/services/WalletService.js new file mode 100644 index 00000000..c8c52a6f --- /dev/null +++ b/src/js/services/WalletService.js @@ -0,0 +1,70 @@ +/* @flow */ +'use strict'; + +import { LOCATION_CHANGE } from 'react-router-redux'; +import * as WALLET from '../actions/constants/wallet'; + +import * as WalletActions from '../actions/WalletActions'; +import * as LocalStorageActions from '../actions/LocalStorageActions'; + +import type { + Middleware, + MiddlewareAPI, + MiddlewareDispatch, + State, + Dispatch, + Action, + GetState, + TrezorDevice, +} from '~/flowtype'; + + +const getSelectedDevice = (state: State): ?TrezorDevice => { + const locationState = state.router.location.state; + if (!locationState.device) return null; + + const instance: ?number = locationState.deviceInstance ? parseInt(locationState.deviceInstance) : undefined; + return state.connect.devices.find(d => d.features && d.features.device_id === locationState.device && d.instance === instance); +} + +/** + * Middleware + */ +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) { + api.dispatch( WalletActions.init() ); + // load data from config.json and local storage + api.dispatch( LocalStorageActions.loadData() ); + } + } + + // pass action + next(action); + + const state = api.getState(); + + // listening devices state change + if (locationChange || prevState.connect.devices !== state.connect.devices) { + const device = getSelectedDevice(state); + if (state.wallet.selectedDevice !== device) { + console.warn("UPDATE SELECTED DEVICE!", state.router.location.state) + api.dispatch({ + type: WALLET.SET_SELECTED_DEVICE, + device + }) + } + } + + return action; +}; + + + +export default WalletService; \ No newline at end of file diff --git a/src/js/services/index.js b/src/js/services/index.js index e74b1870..f5b6e7da 100644 --- a/src/js/services/index.js +++ b/src/js/services/index.js @@ -1,6 +1,7 @@ /* @flow */ 'use strict'; +import WalletService from './WalletService'; import LogService from './LogService'; import RouterService from './RouterService'; import LocalStorageService from './LocalStorageService'; @@ -9,6 +10,7 @@ import CoinmarketcapService from './CoinmarketcapService'; import TrezorConnectService from './TrezorConnectService'; export default [ + WalletService, LogService, RouterService, LocalStorageService,