1
0
mirror of https://github.com/ascribe/onion.git synced 2025-01-03 10:25:08 +01:00
onion/js/components/app_base.js

84 lines
2.3 KiB
JavaScript
Raw Normal View History

2016-02-05 10:38:59 +01:00
import React from 'react';
import UserActions from '../actions/user_actions';
import UserStore from '../stores/user_store';
import WhitelabelActions from '../actions/whitelabel_actions';
import WhitelabelStore from '../stores/whitelabel_store';
import GlobalNotification from './global_notification';
2016-06-08 14:54:05 +02:00
import { currentUserShape, locationShape, whitelabelShape } from './prop_types';
2016-02-05 10:38:59 +01:00
import { safeMerge } from '../utils/general';
2016-02-05 10:38:59 +01:00
export default function AppBase(App) {
return React.createClass({
displayName: 'AppBase',
propTypes: {
children: React.PropTypes.element.isRequired,
2016-06-08 14:54:05 +02:00
location: locationShape.isRequired,
2016-02-05 10:38:59 +01:00
routes: React.PropTypes.arrayOf(React.PropTypes.object).isRequired
},
childContextTypes: {
currentUser: currentUserShape,
2016-06-08 14:54:05 +02:00
location: locationShape,
whitelabel: whitelabelShape
},
2016-02-05 10:38:59 +01:00
getInitialState() {
return safeMerge(
2016-02-05 10:38:59 +01:00
UserStore.getState(),
WhitelabelStore.getState()
);
},
getChildContext() {
const { currentUser, whitelabel } = this.state;
2016-06-08 14:54:05 +02:00
return {
currentUser,
whitelabel,
location: this.props.location
};
},
2016-02-05 10:38:59 +01:00
componentDidMount() {
UserStore.listen(this.onChange);
WhitelabelStore.listen(this.onChange);
UserActions.fetchCurrentUser();
WhitelabelActions.fetchWhitelabel();
},
componentWillUnmount() {
UserStore.unlisten(this.onChange);
WhitelabelActions.unlisten(this.onChange);
},
onChange(state) {
this.setState(state);
},
render() {
const { routes } = this.props;
// The second element of the routes prop given to us by react-router is always the
// active second-level component object (ie. after App).
const activeRoute = routes[1];
return (
<div>
<App
{...this.props}
activeRoute={activeRoute} />
2016-02-05 10:38:59 +01:00
<GlobalNotification />
<div id="modal" className="container" />
</div>
);
}
});
}