1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-23 01:39:36 +01:00

Remove ProxyRoute

This commit is contained in:
vrde 2015-10-12 16:51:03 +02:00
parent 356e0e368a
commit 04abf31354
5 changed files with 146 additions and 270 deletions

View File

@ -1,80 +0,0 @@
'use strict';
import React from 'react';
import invariant from 'invariant';
import { createRoutes } from 'react-router';
const { string, bool, func, object, oneOfType } = React.PropTypes;
const ProxyRoute = React.createClass({
propTypes: {
path: string,
ignoreScrollBehavior: bool,
handler: oneOfType([ func, string ]),
component: oneOfType([ func, string ]),
components: oneOfType([ oneOfType([ func, string ]), object ]),
getComponents: func,
proxyHandler: func
},
statics: {
createRouteFromReactElement(element) {
/**
* Generally creating custom `Route`s is not supported by react-router.
*
* However, if we take a look at how `Route`s are declared/generated in their github repo,
* we see that it's fairly straight forward:
* - https://github.com/rackt/react-router/blob/master/modules/Route.js#L21
*
* ```
* const route = createRouteFromReactElement(element)
*
* [...]
*
* return route;
* ```
*
* Unfortunately, though `createRouteFromReactElement` is not exported by
* react-router, as can be seen here:
* - https://github.com/rackt/react-router/blob/master/modules/index.js#L19
*
* Still there is a trick we can use to call this method manually.
* We call the public method `createRoutes`:
* - (https://github.com/rackt/react-router/blob/master/modules/RouteUtils.js#L91)
* which then calls `createRoutesFromReactChildren`
*
* For each route element submitted as an array, this method checks if
* `element.type.createRouteFromReactElement` is `true` or `false`.
*
* So what we can do is just simply set our element's `type.createRouteFromReactElement`
* property to `false`, so that the if statement falls into the methods `else` case
* and calls `createRouteFromReactElement`:
* - https://github.com/rackt/react-router/blob/master/modules/RouteUtils.js#L77
*
* After returning from `createRoutes`, we set `element.type.createRouteFromReactElement`
* to its original value and replace route's `component`, with our manually inserted
* component.
*/
const createRouteFromReactElementCopy = element.type.createRouteFromReactElement;
element.type.createRouteFromReactElement = false;
const [ route ] = createRoutes(element);
element.type.createRouteFromReactElement = createRouteFromReactElementCopy;
const Component = route.component;
const ProxyHandler = element.props.proxyHandler;
route.component = ProxyHandler(Component);
return route;
}
},
render() {
invariant(
false,
'<ProxyRoute> elements are for router configuration only and should not be rendered'
);
}
});
export default ProxyRoute;

View File

@ -12,7 +12,7 @@ import AppConstants from '../../../constants/application_constants';
const { object } = React.PropTypes; const { object } = React.PropTypes;
/** /**
* Can be used in combination with `ProxyRoute` as an intermediate Handler * Can be used in combination with `Route` as an intermediate Handler
* between the actual component we want to display dependent on a certain state * between the actual component we want to display dependent on a certain state
* that is required to display that component. * that is required to display that component.
* *

View File

@ -19,7 +19,6 @@ import ErrorNotFoundPage from '../../../components/error_not_found_page';
import App from './prize_app'; import App from './prize_app';
import AppConstants from '../../../constants/application_constants'; import AppConstants from '../../../constants/application_constants';
import ProxyRoute from '../../../components/ascribe_routes/proxy_route';
import RedirectProxyHandler from '../../../components/ascribe_routes/proxy_routes/redirect_proxy_handler'; import RedirectProxyHandler from '../../../components/ascribe_routes/proxy_routes/redirect_proxy_handler';
@ -29,41 +28,34 @@ function getRoutes() {
return ( return (
<Route path={baseUrl} component={App}> <Route path={baseUrl} component={App}>
<IndexRoute component={Landing} /> <IndexRoute component={Landing} />
<ProxyRoute <Route
path="login" path='login'
proxyHandler={RedirectProxyHandler({to: '/collection', when: 'loggedIn'})} component={RedirectProxyHandler({to: '/collection', when: 'loggedIn'})(LoginContainer)} />
component={LoginContainer} /> <Route
<ProxyRoute path='logout'
path="logout" component={RedirectProxyHandler({to: '/', when: 'loggedOut'})(LogoutContainer)}/>
proxyHandler={RedirectProxyHandler({to: '/', when: 'loggedOut'})} <Route
component={LogoutContainer}/> path='signup'
<ProxyRoute component={RedirectProxyHandler({to: '/collection', when: 'loggedIn'})(SignupContainer)} />
path="signup" <Route
proxyHandler={RedirectProxyHandler({to: '/collection', when: 'loggedIn'})} path='password_reset'
component={SignupContainer} /> component={RedirectProxyHandler({to: '/collection', when: 'loggedIn'})(PasswordResetContainer)} />
<ProxyRoute <Route
path="password_reset" path='settings'
proxyHandler={RedirectProxyHandler({to: '/collection', when: 'loggedIn'})} component={RedirectProxyHandler({to: '/login', when: 'loggedOut'})(SettingsContainer)}/>
component={PasswordResetContainer} /> <Route
<ProxyRoute path='register_piece'
path="settings" component={RedirectProxyHandler({to: '/login', when: 'loggedOut'})(PrizeRegisterPiece)}
proxyHandler={RedirectProxyHandler({to: '/login', when: 'loggedOut'})} headerTitle='+ NEW WORK'/>
component={SettingsContainer}/> <Route
path='collection'
component={RedirectProxyHandler({to: '/login', when: 'loggedOut'})(PrizePieceList)}
headerTitle='COLLECTION'/>
<ProxyRoute <Route path='pieces/:pieceId' component={PrizePieceContainer} />
path="register_piece" <Route path='editions/:editionId' component={EditionContainer} />
proxyHandler={RedirectProxyHandler({to: '/login', when: 'loggedOut'})} <Route path='verify' component={CoaVerifyContainer} />
component={PrizeRegisterPiece} <Route path='*' component={ErrorNotFoundPage} />
headerTitle="+ NEW WORK"/>
<ProxyRoute
path="collection"
proxyHandler={RedirectProxyHandler({to: '/login', when: 'loggedOut'})}
component={PrizePieceList}
headerTitle="COLLECTION"/>
<Route path="pieces/:pieceId" component={PrizePieceContainer} />
<Route path="editions/:editionId" component={EditionContainer} />
<Route path="verify" component={CoaVerifyContainer} />
<Route path="*" component={ErrorNotFoundPage} />
</Route> </Route>
); );
} }

View File

@ -30,7 +30,6 @@ import IkonotvContractNotifications from './components/ikonotv/ikonotv_contract_
import CCRegisterPiece from './components/cc/cc_register_piece'; import CCRegisterPiece from './components/cc/cc_register_piece';
import ProxyRoute from '../../../components/ascribe_routes/proxy_route';
import RedirectProxyHandler from '../../../components/ascribe_routes/proxy_routes/redirect_proxy_handler'; import RedirectProxyHandler from '../../../components/ascribe_routes/proxy_routes/redirect_proxy_handler';
import WalletApp from './wallet_app'; import WalletApp from './wallet_app';
@ -43,140 +42,114 @@ let ROUTES = {
'cyland': ( 'cyland': (
<Route path={baseUrl} component={WalletApp}> <Route path={baseUrl} component={WalletApp}>
<IndexRoute component={CylandLanding} /> <IndexRoute component={CylandLanding} />
<ProxyRoute <Route
path="login" path='login'
proxyHandler={RedirectProxyHandler({to: '/collection', when: 'loggedIn'})} component={RedirectProxyHandler({to: '/collection', when: 'loggedIn'})(LoginContainer)} />
component={LoginContainer} /> <Route
<ProxyRoute path='logout'
path="logout" component={RedirectProxyHandler({to: '/', when: 'loggedOut'})(LogoutContainer)}/>
proxyHandler={RedirectProxyHandler({to: '/', when: 'loggedOut'})} <Route
component={LogoutContainer}/> path='signup'
<ProxyRoute component={RedirectProxyHandler({to: '/collection', when: 'loggedIn'})(SignupContainer)} />
path="signup" <Route
proxyHandler={RedirectProxyHandler({to: '/collection', when: 'loggedIn'})} path='password_reset'
component={SignupContainer} /> component={RedirectProxyHandler({to: '/collection', when: 'loggedIn'})(PasswordResetContainer)} />
<ProxyRoute <Route
path="password_reset" path='settings'
proxyHandler={RedirectProxyHandler({to: '/collection', when: 'loggedIn'})} component={RedirectProxyHandler({to: '/login', when: 'loggedOut'})(SettingsContainer)}/>
component={PasswordResetContainer} /> <Route
<ProxyRoute path='contract_settings'
path="settings" component={RedirectProxyHandler({to: '/login', when: 'loggedOut'})(ContractSettings)}/>
proxyHandler={RedirectProxyHandler({to: '/login', when: 'loggedOut'})} <Route
component={SettingsContainer}/> path='register_piece'
<ProxyRoute component={RedirectProxyHandler({to: '/login', when: 'loggedOut'})(CylandRegisterPiece)}
path="contract_settings" headerTitle='+ NEW WORK'/>
proxyHandler={RedirectProxyHandler({to: '/login', when: 'loggedOut'})} <Route
component={ContractSettings}/> path='collection'
<ProxyRoute component={RedirectProxyHandler({to: '/login', when: 'loggedOut'})(CylandPieceList)}
path="register_piece" headerTitle='COLLECTION'/>
proxyHandler={RedirectProxyHandler({to: '/login', when: 'loggedOut'})} <Route path='editions/:editionId' component={EditionContainer} />
component={CylandRegisterPiece} <Route path='verify' component={CoaVerifyContainer} />
headerTitle="+ NEW WORK"/> <Route path='pieces/:pieceId' component={CylandPieceContainer} />
<ProxyRoute <Route path='*' component={ErrorNotFoundPage} />
path="collection"
proxyHandler={RedirectProxyHandler({to: '/login', when: 'loggedOut'})}
component={CylandPieceList}
headerTitle="COLLECTION"/>
<Route path="editions/:editionId" component={EditionContainer} />
<Route path="verify" component={CoaVerifyContainer} />
<Route path="pieces/:pieceId" component={CylandPieceContainer} />
<Route path="*" component={ErrorNotFoundPage} />
</Route> </Route>
), ),
'cc': ( 'cc': (
<Route path={baseUrl} component={WalletApp}> <Route path={baseUrl} component={WalletApp}>
<ProxyRoute <Route
path="login" path='login'
proxyHandler={RedirectProxyHandler({to: '/collection', when: 'loggedIn'})} component={RedirectProxyHandler({to: '/collection', when: 'loggedIn'})(LoginContainer)} />
component={LoginContainer} /> <Route
<ProxyRoute path='logout'
path="logout" component={RedirectProxyHandler({to: '/', when: 'loggedOut'})(LogoutContainer)}/>
proxyHandler={RedirectProxyHandler({to: '/', when: 'loggedOut'})} <Route
component={LogoutContainer}/> path='signup'
<ProxyRoute component={RedirectProxyHandler({to: '/collection', when: 'loggedIn'})(SignupContainer)} />
path="signup" <Route
proxyHandler={RedirectProxyHandler({to: '/collection', when: 'loggedIn'})} path='password_reset'
component={SignupContainer} /> component={RedirectProxyHandler({to: '/collection', when: 'loggedIn'})(PasswordResetContainer)} />
<ProxyRoute <Route
path="password_reset" path='settings'
proxyHandler={RedirectProxyHandler({to: '/collection', when: 'loggedIn'})} component={RedirectProxyHandler({to: '/login', when: 'loggedOut'})(SettingsContainer)}/>
component={PasswordResetContainer} /> <Route
<ProxyRoute path='contract_settings'
path="settings" component={RedirectProxyHandler({to: '/login', when: 'loggedOut'})(ContractSettings)}/>
proxyHandler={RedirectProxyHandler({to: '/login', when: 'loggedOut'})} <Route
component={SettingsContainer}/> path='register_piece'
<ProxyRoute component={RedirectProxyHandler({to: '/login', when: 'loggedOut'})(CCRegisterPiece)}
path="contract_settings" headerTitle='+ NEW WORK'/>
proxyHandler={RedirectProxyHandler({to: '/login', when: 'loggedOut'})} <Route
component={ContractSettings}/> path='collection'
<ProxyRoute component={RedirectProxyHandler({to: '/login', when: 'loggedOut'})(PieceList)}
path="register_piece" headerTitle='COLLECTION'/>
proxyHandler={RedirectProxyHandler({to: '/login', when: 'loggedOut'})} <Route path='pieces/:pieceId' component={PieceContainer} />
component={CCRegisterPiece} <Route path='editions/:editionId' component={EditionContainer} />
headerTitle="+ NEW WORK"/> <Route path='verify' component={CoaVerifyContainer} />
<ProxyRoute <Route path='*' component={ErrorNotFoundPage} />
path="collection"
proxyHandler={RedirectProxyHandler({to: '/login', when: 'loggedOut'})}
component={PieceList}
headerTitle="COLLECTION"/>
<Route path="pieces/:pieceId" component={PieceContainer} />
<Route path="editions/:editionId" component={EditionContainer} />
<Route path="verify" component={CoaVerifyContainer} />
<Route path="*" component={ErrorNotFoundPage} />
</Route> </Route>
), ),
'ikonotv': ( 'ikonotv': (
<Route path={baseUrl} component={WalletApp}> <Route path={baseUrl} component={WalletApp}>
<IndexRoute component={IkonotvLanding} /> <IndexRoute component={IkonotvLanding} />
<ProxyRoute <Route
path="login" path='login'
proxyHandler={RedirectProxyHandler({to: '/collection', when: 'loggedIn'})} component={RedirectProxyHandler({to: '/collection', when: 'loggedIn'})(LoginContainer)} />
component={LoginContainer} /> <Route
<ProxyRoute path='logout'
path="logout" component={RedirectProxyHandler({to: '/', when: 'loggedOut'})(LogoutContainer)}/>
proxyHandler={RedirectProxyHandler({to: '/', when: 'loggedOut'})} <Route
component={LogoutContainer}/> path='signup'
<ProxyRoute component={RedirectProxyHandler({to: '/collection', when: 'loggedIn'})(SignupContainer)} />
path="signup" <Route
proxyHandler={RedirectProxyHandler({to: '/collection', when: 'loggedIn'})} path='password_reset'
component={SignupContainer} /> component={RedirectProxyHandler({to: '/collection', when: 'loggedIn'})(PasswordResetContainer)} />
<ProxyRoute <Route
path="password_reset" path='settings'
proxyHandler={RedirectProxyHandler({to: '/collection', when: 'loggedIn'})} component={RedirectProxyHandler({to: '/login', when: 'loggedOut'})(SettingsContainer)}/>
component={PasswordResetContainer} /> <Route
<ProxyRoute path='contract_settings'
path="settings" component={RedirectProxyHandler({to: '/login', when: 'loggedOut'})(ContractSettings)}/>
proxyHandler={RedirectProxyHandler({to: '/login', when: 'loggedOut'})} <Route
component={SettingsContainer}/> path='request_loan'
<ProxyRoute component={RedirectProxyHandler({to: '/login', when: 'loggedOut'})(ContractAgreementForm)}
path="contract_settings" headerTitle='SEND NEW CONTRACT'
proxyHandler={RedirectProxyHandler({to: '/login', when: 'loggedOut'})} aclName='acl_create_contractagreement'/>
component={ContractSettings}/> <Route
<ProxyRoute path='register_piece'
path="request_loan" component={RedirectProxyHandler({to: '/login', when: 'loggedOut'})(IkonotvRegisterPiece)}
proxyHandler={RedirectProxyHandler({to: '/login', when: 'loggedOut'})} headerTitle='+ NEW WORK'
component={ContractAgreementForm} aclName='acl_create_piece'/>
headerTitle="SEND NEW CONTRACT" <Route
aclName="acl_create_contractagreement"/> path='collection'
<ProxyRoute component={RedirectProxyHandler({to: '/login', when: 'loggedOut'})(IkonotvPieceList)}
path="register_piece" headerTitle='COLLECTION'/>
proxyHandler={RedirectProxyHandler({to: '/login', when: 'loggedOut'})} <Route
component={IkonotvRegisterPiece} path='contract_notifications'
headerTitle="+ NEW WORK" component={RedirectProxyHandler({to: '/login', when: 'loggedOut'})(IkonotvContractNotifications)}/>
aclName="acl_create_piece"/> <Route path='pieces/:pieceId' component={IkonotvPieceContainer} />
<ProxyRoute <Route path='editions/:editionId' component={EditionContainer} />
path="collection" <Route path='verify' component={CoaVerifyContainer} />
proxyHandler={RedirectProxyHandler({to: '/login', when: 'loggedOut'})} <Route path='*' component={ErrorNotFoundPage} />
component={IkonotvPieceList}
headerTitle="COLLECTION"/>
<ProxyRoute
path="contract_notifications"
proxyHandler={RedirectProxyHandler({to: '/login', when: 'loggedOut'})}
component={IkonotvContractNotifications}/>
<Route path="pieces/:pieceId" component={IkonotvPieceContainer} />
<Route path="editions/:editionId" component={EditionContainer} />
<Route path="verify" component={CoaVerifyContainer} />
<Route path="*" component={ErrorNotFoundPage} />
</Route> </Route>
) )
}; };

View File

@ -25,7 +25,6 @@ import ErrorNotFoundPage from './components/error_not_found_page';
import RegisterPiece from './components/register_piece'; import RegisterPiece from './components/register_piece';
import ProxyRoute from './components/ascribe_routes/proxy_route';
import RedirectProxyHandler from './components/ascribe_routes/proxy_routes/redirect_proxy_handler'; import RedirectProxyHandler from './components/ascribe_routes/proxy_routes/redirect_proxy_handler';
import AppConstants from './constants/application_constants'; import AppConstants from './constants/application_constants';
@ -35,42 +34,34 @@ let baseUrl = AppConstants.baseUrl;
let COMMON_ROUTES = ( let COMMON_ROUTES = (
<Route path={baseUrl} component={App}> <Route path={baseUrl} component={App}>
<ProxyRoute <Route
path="login" path="login"
proxyHandler={RedirectProxyHandler({to: '/collection', when: 'loggedIn'})} component={RedirectProxyHandler({to: '/collection', when: 'loggedIn'})(LoginContainer)} />
component={LoginContainer} /> <Route
<ProxyRoute
path="register_piece" path="register_piece"
proxyHandler={RedirectProxyHandler({to: '/login', when: 'loggedOut'})} component={RedirectProxyHandler({to: '/login', when: 'loggedOut'})(RegisterPiece)}
component={RegisterPiece}
headerTitle="+ NEW WORK"/> headerTitle="+ NEW WORK"/>
<ProxyRoute <Route
path="collection" path="collection"
proxyHandler={RedirectProxyHandler({to: '/login', when: 'loggedOut'})} component={RedirectProxyHandler({to: '/login', when: 'loggedOut'})(PieceList)}
component={PieceList}
headerTitle="COLLECTION"/> headerTitle="COLLECTION"/>
<ProxyRoute <Route
path="signup" path="signup"
proxyHandler={RedirectProxyHandler({to: '/collection', when: 'loggedIn'})} component={RedirectProxyHandler({to: '/collection', when: 'loggedIn'})(SignupContainer)} />
component={SignupContainer} /> <Route
<ProxyRoute
path="logout" path="logout"
proxyHandler={RedirectProxyHandler({to: '/', when: 'loggedOut'})} component={RedirectProxyHandler({to: '/', when: 'loggedOut'})(LogoutContainer)}/>
component={LogoutContainer}/>
<Route path="pieces/:pieceId" component={PieceContainer} /> <Route path="pieces/:pieceId" component={PieceContainer} />
<Route path="editions/:editionId" component={EditionContainer} /> <Route path="editions/:editionId" component={EditionContainer} />
<ProxyRoute <Route
path="password_reset" path="password_reset"
proxyHandler={RedirectProxyHandler({to: '/collection', when: 'loggedIn'})} component={RedirectProxyHandler({to: '/collection', when: 'loggedIn'})(PasswordResetContainer)} />
component={PasswordResetContainer} /> <Route
<ProxyRoute
path="settings" path="settings"
proxyHandler={RedirectProxyHandler({to: '/login', when: 'loggedOut'})} component={RedirectProxyHandler({to: '/login', when: 'loggedOut'})(SettingsContainer)}/>
component={SettingsContainer}/> <Route
<ProxyRoute
path="contract_settings" path="contract_settings"
proxyHandler={RedirectProxyHandler({to: '/login', when: 'loggedOut'})} component={RedirectProxyHandler({to: '/login', when: 'loggedOut'})(ContractSettings)}/>
component={ContractSettings}/>
<Route path="coa_verify" component={CoaVerifyContainer} /> <Route path="coa_verify" component={CoaVerifyContainer} />
<Route path="*" component={ErrorNotFoundPage} /> <Route path="*" component={ErrorNotFoundPage} />
</Route> </Route>