1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-12-28 18:08:08 +00:00

remove leftovers from RouterService

This commit is contained in:
Szymon Lesisz 2018-09-20 20:25:34 +02:00
parent 900c3961cd
commit ae9b12f35d
2 changed files with 32 additions and 79 deletions

View File

@ -71,10 +71,35 @@ export const paramsValidation = (params: RouterLocationState): PayloadAction<boo
// if (params.hasOwnProperty('account')) {
// }
return true;
}
export const deviceModeValidation = (current: RouterLocationState, requested: RouterLocationState): PayloadAction<boolean> => (dispatch: Dispatch, getState: GetState): boolean => {
// allow url change if requested device is not the same as current state
if (current.device !== requested.device) return true;
// find device
const { devices } = getState();
let device: ?TrezorDevice;
if (requested.hasOwnProperty('deviceInstance')) {
device = devices.find(d => d.features && d.features.device_id === requested.device && d.instance === parseInt(requested.deviceInstance, 10));
} else {
device = devices.find(d => d.path === requested.device || (d.features && d.features.device_id === requested.device));
}
if (!device) return false;
if (!device.features) return false;
if (device.firmware === 'required') return false;
return true;
}
/*
* Composing url string from given RouterLocationState object
* Filters unrecognized fields and sorting in correct order
*/
export const paramsToPath = (params: RouterLocationState): PayloadAction<string> => (dispatch: Dispatch, getState: GetState): string => {
return "/";
}
/*
* Utility used in "selectDevice" and "selectFirstAvailableDevice"
* sorting device array by "ts" (timestamp) field
@ -87,7 +112,7 @@ const sortDevices = (devices: Array<TrezorDevice>): Array<TrezorDevice> => devic
});
/*
* Compose url from requested device
* Compose url from given device object and redirect
*/
export const selectDevice = (device: TrezorDevice | Device): ThunkAction => (dispatch: Dispatch, getState: GetState): void => {
let url: ?string;
@ -122,7 +147,7 @@ export const selectDevice = (device: TrezorDevice | Device): ThunkAction => (dis
/*
* Try to find first available device using order:
* 1. First Unacquired
* 1. First unacquired
* 2. First connected
* 3. Saved with latest timestamp
* OR redirect to landing page
@ -153,7 +178,7 @@ const goto = (url: string): ThunkAction => (dispatch: Dispatch, getState: GetSta
}
/*
* Check if requested OR current (from RouterReducer) url is landing page
* Check if requested OR current url is landing page
*/
export const isLandingPageUrl = (url?: string): PayloadAction<boolean> => (dispatch: Dispatch, getState: GetState): boolean => {
if (typeof url !== 'string') {
@ -167,7 +192,6 @@ export const isLandingPageUrl = (url?: string): PayloadAction<boolean> => (dispa
* Try to redirect to landing page
*/
export const gotoLandingPage = (): ThunkAction => (dispatch: Dispatch, getState: GetState): void => {
// check if current url is not valid landing page url
const isLandingPage = dispatch( isLandingPageUrl() );
if (!isLandingPage) {
dispatch( goto('/') );

View File

@ -1,8 +1,6 @@
/* @flow */
import { LOCATION_CHANGE, replace } from 'react-router-redux';
import * as CONNECT from 'actions/constants/TrezorConnect';
import * as WALLET from 'actions/constants/wallet';
import * as NotificationActions from 'actions/NotificationActions';
import * as RouterActions from 'actions/RouterActions';
import type {
@ -15,69 +13,10 @@ import type {
TrezorDevice,
} from 'flowtype';
/*
const validation = (api: MiddlewareAPI, params: RouterLocationState): boolean => {
if (params.hasOwnProperty('device')) {
const { devices } = api.getState();
let device: ?TrezorDevice;
if (params.hasOwnProperty('deviceInstance')) {
device = devices.find(d => d.features && d.features.device_id === params.device && d.instance === parseInt(params.deviceInstance, 10));
} else {
device = devices.find(d => d.path === params.device || (d.features && d.features.device_id === params.device));
}
if (!device) return false;
}
if (params.hasOwnProperty('network')) {
const { config } = api.getState().localStorage;
const coin = config.coins.find(c => c.network === params.network);
if (!coin) return false;
if (!params.account) return false;
}
// if (params.account) {
// }
return true;
};
*/
const deviceModeValidation = (api: MiddlewareAPI, current: RouterLocationState, requested: RouterLocationState): boolean => {
// allow url change if requested device is not the same as current state
if (current.device !== requested.device) return true;
// find device
const { devices } = api.getState();
let device: ?TrezorDevice;
if (requested.hasOwnProperty('deviceInstance')) {
device = devices.find(d => d.features && d.features.device_id === requested.device && d.instance === parseInt(requested.deviceInstance, 10));
} else {
device = devices.find(d => d.path === requested.device || (d.features && d.features.device_id === requested.device));
}
if (!device) return false;
if (!device.features) return false;
if (device.firmware === 'required') return false;
return true;
}
/**
* Redux Middleware used for managing router path
* This middleware couldn't use async/await because LOCATION_CHANGE action is also synchronized with RouterReducer (react-router-redux)
*/
const RouterService1: Middleware = (api: MiddlewareAPI) => (next: MiddlewareDispatch) => (action: Action): Action => {
if (action.type !== LOCATION_CHANGE) {
return next(action);
}
action.payload.state = api.dispatch( RouterActions.pathToParams(action.payload.pathname) );
next(action);
return action;
}
const RouterService: Middleware = (api: MiddlewareAPI) => (next: MiddlewareDispatch) => (action: Action): Action => {
// make sure that middleware should process this action
@ -135,23 +74,17 @@ const RouterService: Middleware = (api: MiddlewareAPI) => (next: MiddlewareDispa
if (isLandingPageUrl) {
// Corner case: disallow displaying landing page
// redirect to previous url
// TODO: && currentParamsAreValid
// TODO: make sure that currentParamsAreValid, otherwise selectFirstAvailableDevice
redirectUrl = location.pathname;
} else if (!requestedParamsAreValid) {
// Corner case: requested params are not valid
// Neither device or network doesn't exists
postActions.push( RouterActions.selectFirstAvailableDevice() );
redirectUrl = location.pathname;
// postActions.push( RouterActions.selectFirstAvailableDevice() );
} else if (requestedParams.device) {
if (!deviceModeValidation(api, currentParams, requestedParams)) {
if (!api.dispatch( RouterActions.deviceModeValidation(currentParams, requestedParams) )) {
redirectUrl = location.pathname;
console.warn('Device is not in valid mode');
} else if (requestedParams.network !== currentParams.network) {
postActions.push({
type: CONNECT.COIN_CHANGED,
payload: {
network: requestedParams.network,
},
});
}
}
}
@ -180,11 +113,7 @@ const RouterService: Middleware = (api: MiddlewareAPI) => (next: MiddlewareDispa
api.dispatch(a);
});
// TODO: move this to wallet service?
api.dispatch(NotificationActions.clear(currentParams, requestedParams));
return action;
};
export default RouterService;