From 97fd639c30bf699f819b6f32ab629eeceec83d3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Daubensch=C3=BCtz?= Date: Tue, 6 Oct 2015 10:20:36 +0200 Subject: [PATCH] Implement ProxyRoute --- js/components/ascribe_app.js | 21 ++++------ .../ascribe_routes/auth_component.js | 33 +++++++++++++++ js/components/ascribe_routes/proxy_route.js | 41 +++++++++++++++++++ js/routes.js | 9 +++- 4 files changed, 90 insertions(+), 14 deletions(-) create mode 100644 js/components/ascribe_routes/auth_component.js create mode 100644 js/components/ascribe_routes/proxy_route.js diff --git a/js/components/ascribe_app.js b/js/components/ascribe_app.js index 966bd464..0c9cd703 100644 --- a/js/components/ascribe_app.js +++ b/js/components/ascribe_app.js @@ -2,8 +2,6 @@ import React from 'react'; -import TransactionManager from './ascribe_transaction_manager/transaction_manager'; - import Header from '../components/header'; import Footer from '../components/footer'; import GlobalNotification from './global_notification'; @@ -22,17 +20,14 @@ let AscribeApp = React.createClass({ let { children, routes } = this.props; return ( - -
-
- {/* Routes are injected here */} - {children} -
- - -
-
+
+
+ {/* Routes are injected here */} + {children} +
+ + +
); } }); diff --git a/js/components/ascribe_routes/auth_component.js b/js/components/ascribe_routes/auth_component.js new file mode 100644 index 00000000..d761230e --- /dev/null +++ b/js/components/ascribe_routes/auth_component.js @@ -0,0 +1,33 @@ +'use strict'; + +import React from 'react'; + +import UserStore from '../../stores/user_store'; +import UserActions from '../../actions/user_actions'; + +export function AuthComponent(Component) { + return React.createClass({ + getInitialState() { + return UserStore.getState(); + }, + + componentDidMount() { + UserStore.listen(this.onChange); + UserActions.fetchCurrentUser(); + }, + + componentWillUnmount() { + UserStore.unlisten(this.onChange); + }, + + onChange(state) { + this.setState(state); + }, + + render() { + return ( + + ); + } + }); +} \ No newline at end of file diff --git a/js/components/ascribe_routes/proxy_route.js b/js/components/ascribe_routes/proxy_route.js new file mode 100644 index 00000000..7cb0c30b --- /dev/null +++ b/js/components/ascribe_routes/proxy_route.js @@ -0,0 +1,41 @@ +'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) { + element.type.createRouteFromReactElement = false; + const [ route ] = createRoutes(element); + + const Component = route.component; + const { proxyHandler } = element.props; + route.component = proxyHandler(Component); + + return route; + } + }, + + render() { + invariant( + false, + 'Some error message' + ); + } +}); + +export default ProxyRoute; \ No newline at end of file diff --git a/js/routes.js b/js/routes.js index c12e49d9..727c0b43 100644 --- a/js/routes.js +++ b/js/routes.js @@ -25,6 +25,9 @@ import ErrorNotFoundPage from './components/error_not_found_page'; import RegisterPiece from './components/register_piece'; +import ProxyRoute from './components/ascribe_routes/proxy_route'; +import { AuthComponent } from './components/ascribe_routes/auth_component'; + import AppConstants from './constants/application_constants'; @@ -32,8 +35,12 @@ let baseUrl = AppConstants.baseUrl; const COMMON_ROUTES = ( + -