Source: index.js

/** @module AutoMate */

// Import dependencies
import React from 'react'
import ReactDOM from 'react-dom'
import { browserHistory, Router, Route, IndexRoute } from 'react-router'
import { createStore, combineReducers, bindActionCreators, compose } from 'redux'
import { Provider, connect } from 'react-redux'
import { syncHistoryWithStore, routerReducer } from 'react-router-redux'

// Import the main module and the CSS.
import Main from './Main'
import './index.css'

// Import the submodules.
import project from './project'
import registermap from './registermap'
import demo_module from './demo_module'

/**
 * Redux Section
 **/

/** The root reducer is combined from all sub-module reducers. */
const rootReducer = combineReducers({
  project: project.reducer,
  registermap: registermap.reducer,
  demo_module: demo_module.reducer,
  routing: routerReducer
})
console.log('Root reducer:', rootReducer)

/** The default state is combined from all sub-module states. */
const defaultState = {
  project: project.state,
  registermap: registermap.state,
  demo_module: demo_module.state
}
console.log('Default state:', defaultState)

/** The enhancer allows to use Redux development tools in Chrome. */
const enhancers = compose(
    window.devToolsExtension ? window.devToolsExtension() : f => f
)
console.log('Enhancers:', enhancers)

/** Build the Redux store from the rootReducer, the defualtState and the enhancers. */
const store = createStore(rootReducer, defaultState, enhancers)
console.log('store:', store)
const history = syncHistoryWithStore(browserHistory, store)
console.log('history:', history)

/** Collect the action creators from all modules in actionCreators */
const actionCreators = { project: project.actions, registermap: registermap.actions, demo_module: demo_module.actionCreators }

/** Creates a function  */
function mapStateToProps (state) {
  const propState = {}
  Object.keys(state).forEach(key => {
    propState[key] = state[key]
  })
  console.log('Mapping state to props:', state, propState)
  return propState
}

function mapDispatchToProps (dispatch) {
  const boundActionCreators = {}
  Object.keys(actionCreators).forEach(key => {
    boundActionCreators[`${key}Actions`] = bindActionCreators(actionCreators[key], dispatch)
  })
  console.log('Mapping dispatch to props:', dispatch, boundActionCreators)
  return boundActionCreators
}

const App = connect(mapStateToProps, mapDispatchToProps)(Main)

/**
 * React-Router Section
 **/

/** Combine the routes from all modules. */
const router = (
  <Provider store={store}>
    <Router history={history}>
      <Route component={App}>
        <Route path='/'>
          <IndexRoute component={demo_module.components.DemoModule} />
          <Route path='/project/:projectId' component={project.components.Project} />
          <Route path='/project' component={project.components.Project} />
          <Route path='/registermap/:registermapId' component={registermap.components.Registermap} />
          <Route path='/registermap' component={registermap.components.Registermap} />
        </Route>
        {demo_module.routes}
        <Route path='/login' />
      </Route>
    </Router>
  </Provider>
)

/**
 * Render the app
 **/
ReactDOM.render(
  router,
  document.getElementById('root')
)