import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { BrowserRouter, Route, Switch } from 'react-router-dom';
import { observable } from 'mobx';
import { observer } from 'mobx-react';
import routerStore from 'stores/routerStore';
import authStore from 'stores/authStore';
import DataStore from 'stores/DataStore';
import { Router } from 'react-router-mobx';
import DocumentTitle from 'react-document-title';
import { initModals } from './initialize';

const styles = {
	container: {
		height: '100%',
	},
};

@observer
export default class AdminContext extends Component {
	static propTypes = {
		appConfig: PropTypes.object,
	};

	static childContextTypes = {
		appConfig: PropTypes.object,
		authStore: PropTypes.object,
		DataStore: PropTypes.func,
	};

	getChildContext() {
		return {
			appConfig: this._appConfig,
			authStore,
			DataStore,
		};
	}

	componentWillMount() {
		this._appConfig = observable(this.props.appConfig);
		authStore.set(this._appConfig);
		DataStore.setup(this._appConfig, authStore);
		initModals(this._appConfig.modals);
	}

	componentWillReceiveProps({ appConfig }) {
		// this._appConfig = appConfig;
		Object.assign(this._appConfig, appConfig);
		authStore.set(this._appConfig);
		// DataStore.hotUpdate(appConfig);
	}

	render() {
		const {
			props: {
				appConfig: { title, navigator: { frame: Frame, login: Login, routes } },
			},
		} = this;

		const handleEnter = async (nextState, replace, next) => {
			const { pathname, search } = nextState.location;
			const isOk = await authStore.auth();
			if (!isOk) {
				replace({
					pathname: '/login',
					query: {
						ref: pathname + search,
					},
				});
			}
			next();
		};
		return (
			<DocumentTitle title={title}>
				<Router component={BrowserRouter} routerStore={routerStore}>
					<div style={styles.container}>
						<Switch>
							<Route path="/login" component={Login} />
							<Route
								onEnter={handleEnter}
								render={() => <Frame>{routes}</Frame>}
							/>
						</Switch>
					</div>
				</Router>
			</DocumentTitle>
		);
	}
}

@@ -1311,41 +1311,18 @@
 %0A%09%09%09
-props: %7B%0A%09%09%09%09appConfig: %7B 
 title,
- 
+%0A%09%09%09
 navi
@@ -1370,16 +1370,8 @@
 es %7D
- %7D,%0A%09%09%09%7D
 ,%0A%09%09
@@ -1378,16 +1378,32 @@
 %7D = this
+.props.appConfig
 ;%0A%0A%09%09con
@@ -1959,19 +1959,64 @@
 r=%7B(
-) =%3E %3CFrame
+%7B location %7D) =%3E (%0A%09%09%09%09%09%09%09%09%09%3CFrame locKey=%7Blocation.key%7D
 %3E%7Bro
@@ -2028,16 +2028,26 @@
 %3C/Frame%3E
+%0A%09%09%09%09%09%09%09%09)
 %7D%0A%09%09%09%09%09%09

fix route update blocked bug