1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-11-28 03:08:30 +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')) { // 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; 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" * Utility used in "selectDevice" and "selectFirstAvailableDevice"
* sorting device array by "ts" (timestamp) field * 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 => { export const selectDevice = (device: TrezorDevice | Device): ThunkAction => (dispatch: Dispatch, getState: GetState): void => {
let url: ?string; let url: ?string;
@ -122,7 +147,7 @@ export const selectDevice = (device: TrezorDevice | Device): ThunkAction => (dis
/* /*
* Try to find first available device using order: * Try to find first available device using order:
* 1. First Unacquired * 1. First unacquired
* 2. First connected * 2. First connected
* 3. Saved with latest timestamp * 3. Saved with latest timestamp
* OR redirect to landing page * 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 => { export const isLandingPageUrl = (url?: string): PayloadAction<boolean> => (dispatch: Dispatch, getState: GetState): boolean => {
if (typeof url !== 'string') { if (typeof url !== 'string') {
@ -167,7 +192,6 @@ export const isLandingPageUrl = (url?: string): PayloadAction<boolean> => (dispa
* Try to redirect to landing page * Try to redirect to landing page
*/ */
export const gotoLandingPage = (): ThunkAction => (dispatch: Dispatch, getState: GetState): void => { export const gotoLandingPage = (): ThunkAction => (dispatch: Dispatch, getState: GetState): void => {
// check if current url is not valid landing page url
const isLandingPage = dispatch( isLandingPageUrl() ); const isLandingPage = dispatch( isLandingPageUrl() );
if (!isLandingPage) { if (!isLandingPage) {
dispatch( goto('/') ); dispatch( goto('/') );

View File

@ -1,8 +1,6 @@
/* @flow */ /* @flow */
import { LOCATION_CHANGE, replace } from 'react-router-redux'; import { LOCATION_CHANGE, replace } from 'react-router-redux';
import * as CONNECT from 'actions/constants/TrezorConnect';
import * as WALLET from 'actions/constants/wallet'; import * as WALLET from 'actions/constants/wallet';
import * as NotificationActions from 'actions/NotificationActions';
import * as RouterActions from 'actions/RouterActions'; import * as RouterActions from 'actions/RouterActions';
import type { import type {
@ -15,69 +13,10 @@ import type {
TrezorDevice, TrezorDevice,
} from 'flowtype'; } 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 * 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) * 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 => { const RouterService: Middleware = (api: MiddlewareAPI) => (next: MiddlewareDispatch) => (action: Action): Action => {
// make sure that middleware should process this action // make sure that middleware should process this action
@ -135,23 +74,17 @@ const RouterService: Middleware = (api: MiddlewareAPI) => (next: MiddlewareDispa
if (isLandingPageUrl) { if (isLandingPageUrl) {
// Corner case: disallow displaying landing page // Corner case: disallow displaying landing page
// redirect to previous url // redirect to previous url
// TODO: && currentParamsAreValid // TODO: make sure that currentParamsAreValid, otherwise selectFirstAvailableDevice
redirectUrl = location.pathname; redirectUrl = location.pathname;
} else if (!requestedParamsAreValid) { } else if (!requestedParamsAreValid) {
// Corner case: requested params are not valid // Corner case: requested params are not valid
// Neither device or network doesn't exists // Neither device or network doesn't exists
postActions.push( RouterActions.selectFirstAvailableDevice() ); redirectUrl = location.pathname;
// postActions.push( RouterActions.selectFirstAvailableDevice() );
} else if (requestedParams.device) { } else if (requestedParams.device) {
if (!deviceModeValidation(api, currentParams, requestedParams)) { if (!api.dispatch( RouterActions.deviceModeValidation(currentParams, requestedParams) )) {
redirectUrl = location.pathname; redirectUrl = location.pathname;
console.warn('Device is not in valid mode'); 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); api.dispatch(a);
}); });
// TODO: move this to wallet service?
api.dispatch(NotificationActions.clear(currentParams, requestedParams));
return action; return action;
}; };
export default RouterService; export default RouterService;