1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-11-24 09:18:09 +00:00

share router patterns between ./src/view/index and RouteActions

This commit is contained in:
Szymon Lesisz 2018-09-21 12:41:13 +02:00
parent 3a783ce38b
commit 012e222c25
3 changed files with 132 additions and 30 deletions

View File

@ -1,6 +1,7 @@
/* @flow */
import { push } from 'react-router-redux';
import { push, LOCATION_CHANGE } from 'react-router-redux';
import { routes } from 'support/routes';
import type {
RouterLocationState,
@ -80,18 +81,10 @@ export const paramsValidation = (params: RouterLocationState): PayloadAction<boo
* Filters unrecognized fields and sorting in correct order
*/
export const paramsToPath = (params: RouterLocationState): PayloadAction<?string> => (dispatch: Dispatch, getState: GetState): ?string => {
// TODO: share this patterns with /views/index.js
const patterns: Array<Array<string>> = [
['device', 'settings'],
['device', 'bootloader'],
['device', 'initialize'],
['device', 'acquire'],
['device', 'unreadable'],
['device', 'network', 'account', 'send'],
['device', 'network', 'account', 'receive'],
['device', 'network', 'account'],
['device']
];
// get patterns (fields) from routes and sort them by complexity
const patterns: Array<Array<string>> = routes.map(r => r.fields).sort((a, b) => {
return a.length > b.length ? -1 : 1;
});
// find pattern
const keys: Array<string> = Object.keys(params);
@ -292,6 +285,21 @@ export const gotoDeviceSettings = (device: TrezorDevice): ThunkAction => (dispat
* Try to redirect to initial url
*/
export const setInitialUrl = (): PayloadAction<boolean> => (dispatch: Dispatch, getState: GetState): boolean => {
// TODO
const { initialPathname } = getState().wallet;
if (typeof initialPathname === 'string' && !dispatch(isLandingPageUrl(initialPathname))) {
const valid = dispatch( getValidUrl({
type: LOCATION_CHANGE,
payload: {
pathname: initialPathname,
hash: "",
search: "",
state: {}
}
}) );
if (valid === initialPathname) {
dispatch( goto(valid) );
return true;
}
}
return false;
}

94
src/support/routes.js Normal file
View File

@ -0,0 +1,94 @@
/* @flow */
export type Route = {
+name: string;
+pattern: string;
fields: Array<string>;
}
export const routes: Array<Route> = [
{
name: 'landing-home',
pattern: '/',
fields: []
},
{
name: 'landing-bridge',
pattern: '/bridge',
fields: ['bridge']
},
{
name: 'landing-import',
pattern: '/import',
fields: ['import']
},
{
name: 'wallet-setting',
pattern: '/settings',
fields: ['settings']
},
{
name: 'wallet-acquire',
pattern: '/device/:device/acquire',
fields: ['device', 'acquire']
},
{
name: 'wallet-unreadable',
pattern: '/device/:device/unreadable',
fields: ['device', 'unreadable']
},
{
name: 'wallet-bootloader',
pattern: '/device/:device/bootloader',
fields: ['device', 'bootloader']
},
{
name: 'wallet-initialize',
pattern: '/device/:device/initialize',
fields: ['device', 'initialize']
},
{
name: 'wallet-device-settings',
pattern: '/device/:device/settings',
fields: ['device', 'settings']
},
{
name: 'wallet-dashboard',
pattern: '/device/:device',
fields: ['device']
},
{
name: 'wallet-account-summary',
pattern: '/device/:device/network/:network/account/:account',
fields: ['device', 'network', 'account']
},
{
name: 'wallet-account-send',
pattern: '/device/:device/network/:network/account/:account/send',
fields: ['device', 'network', 'account', 'send']
},
{
name: 'wallet-account-send-override',
pattern: '/device/:device/network/:network/account/:account/send/override',
fields: ['device', 'network', 'account', 'send']
},
{
name: 'wallet-account-receive',
pattern: '/device/:device/network/:network/account/:account/receive',
fields: ['device', 'network', 'account', 'receive']
},
{
name: 'wallet-account-signverify',
pattern: '/device/:device/network/:network/account/:account/signverify',
fields: ['device', 'network', 'account', 'signverify']
}
];
export const getPattern = (name: string): string => {
const entry = routes.find(r => r.name === name);
if (!entry) {
console.error(`Route for ${ name } not found`);
return '/';
}
return entry.pattern;
}

View File

@ -6,6 +6,7 @@ import { ConnectedRouter } from 'react-router-redux';
// general
import ErrorBoundary from 'support/ErrorBoundary';
import { getPattern } from 'support/routes';
import LandingContainer from 'views/Landing/Container';
// wallet views
@ -29,25 +30,24 @@ const App = () => (
<Provider store={store}>
<ConnectedRouter history={history}>
<Switch>
<Route exact path="/" component={LandingContainer} />
<Route exact path="/bridge" component={LandingContainer} />
<Route exact path="/import" component={LandingContainer} />
<Route exact path={ getPattern('landing-home') } component={LandingContainer} />
<Route exact path={ getPattern('landing-bridge') } component={LandingContainer} />
<Route exact path={ getPattern('landing-import') } component={LandingContainer} />
<Route>
<ErrorBoundary>
<WalletContainer>
<Route exact path="/settings" component={WalletSettings} />
<Route exact path="/device/:device/" component={WalletDashboard} />
<Route exact path="/device/:device/network/:network" component={WalletDashboard} />
<Route exact path="/device/:device/acquire" component={WalletAcquire} />
<Route exact path="/device/:device/unreadable" component={WalletUnreadableDevice} />
<Route exact path="/device/:device/bootloader" component={WalletBootloader} />
<Route exact path="/device/:device/initialize" component={WalletInitialize} />
<Route exact path="/device/:device/settings" component={WalletDeviceSettings} />
<Route exact path="/device/:device/network/:network/account/:account" component={AccountSummary} />
<Route path="/device/:device/network/:network/account/:account/send" component={AccountSend} />
<Route path="/device/:device/network/:network/account/:account/send/override" component={AccountSend} />
<Route path="/device/:device/network/:network/account/:account/receive" component={AccountReceive} />
<Route path="/device/:device/network/:network/account/:account/signverify" component={AccountSignVerify} />
<Route exact path={ getPattern('wallet-setting') } component={WalletSettings} />
<Route exact path={ getPattern('wallet-dashboard') } component={WalletDashboard} />
<Route exact path={ getPattern('wallet-acquire') } component={WalletAcquire} />
<Route exact path={ getPattern('wallet-unreadable') } component={WalletUnreadableDevice} />
<Route exact path={ getPattern('wallet-bootloader') } component={WalletBootloader} />
<Route exact path={ getPattern('wallet-initialize') } component={WalletInitialize} />
<Route exact path={ getPattern('wallet-device-settings') } component={WalletDeviceSettings} />
<Route exact path={ getPattern('wallet-account-summary') } component={AccountSummary} />
<Route path={ getPattern('wallet-account-send') } component={AccountSend} />
<Route path={ getPattern('wallet-account-send-override') } component={AccountSend} />
<Route path={ getPattern('wallet-account-receive') } component={AccountReceive} />
<Route path={ getPattern('wallet-account-signverify') } component={AccountSignVerify} />
</WalletContainer>
</ErrorBoundary>
</Route>