const React = require('react');
const ReactDOM = require('react-dom');
const CreateStore = require('./wiring/create-store');
const AppContainer = require('./containers/App');
const createHistory = require('history').createBrowserHistory;
const History = createHistory();

// Create redux store and sync history with react-router-redux

const initialState = window.__INITIAL_STATE__;
const store = CreateStore(initialState);

// Developer Tools Setup

if (__DEBUG__) {
    if (window.devToolsExtension) {
        window.devToolsExtension.open();
    }
}

// Render Setup

const MOUNT_NODE = document.getElementById('root');

let render = () => {
  
    const Routes = require('./routes')(store);

    ReactDOM.render(

       <AppContainer 
        store={store} 
        routes={Routes}
        history={History}
      />,
        MOUNT_NODE
    );
};

// Enable HMR and catch runtime errors in RedBox
// This code is excluded from production bundle

if (__DEV__ && module.hot) {

    const RedBox = require('redbox-react').default;
    const renderApp = render;
    const renderError = (error) => {

        ReactDOM.render(<RedBox error={error} />, MOUNT_NODE);
    };

    render = () => {

        try {
            renderApp(Math.random());
        }
        catch (error) {
            console.error(error.stack);
            renderError(error);
        }
    };

    module.hot.accept(['./routes/index'], () => render());
}

// Go!
render();

@@ -638,18 +638,16 @@
 () =%3E %7B%0A
-  
 %0A    con
@@ -716,16 +716,17 @@
 %0A       
+ 
 %3CAppCont
@@ -730,18 +730,21 @@
 ontainer
- 
 %0A
+    
         
@@ -760,18 +760,18 @@
 ore%7D
- 
 %0A
+ 
         
 rout
@@ -766,16 +766,19 @@
         
+   
 routes=%7B
@@ -785,16 +785,20 @@
 Routes%7D%0A
+    
         
@@ -821,16 +821,18 @@
 %7D%0A      
+ 
+ 
 /%3E,%0A    
@@ -857,16 +857,122 @@
  );%0A%7D;%0A%0A
+// when history changes, reload AppContainer%0AHistory.listen((location, action) =%3E %7B%0A  %0A    render();%0A%7D);%0A%0A
 // Enabl

add listener to history, for reloading AppContainer when url changes