Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how can I convert my routes and index.js so i use lazy load (newbie question) #17

Open
jasan-s opened this issue Sep 15, 2016 · 0 comments

Comments

@jasan-s
Copy link

jasan-s commented Sep 15, 2016

I have looked at the example but can't seem to get it to work, At the moment my index.js looks like this, I don't have any server side rendering:

import React from 'react'
import ReactDOM from 'react-dom'
import getRoutes from './config/routes'
import { createStore, applyMiddleware, compose, combineReducers } from 'redux'
import { Provider } from 'react-redux'
import ReduxThunk from 'redux-thunk'
import * as reducers from 'redux/modules'
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider'
import injectTapEventPlugin from 'react-tap-event-plugin'
import { routerReducer, syncHistoryWithStore, routerMiddleware, replace } from 'react-router-redux'
import { browserHistory } from 'react-router'

const historyMiddleware = routerMiddleware(browserHistory)

let reducer = combineReducers({...reducers, routing: routerReducer})
const store = createStore(
  reducer,
  compose(applyMiddleware(ReduxThunk, historyMiddleware),
   window.devToolsExtension ? window.devToolsExtension() : f => f))

const history = syncHistoryWithStore(browserHistory, store)

export function checkAuth () {
  console.log('Route onEnter CheckAuth function Ran')
  if (store.getState().users.isFetching === true) {
    return
  }
  const isAuthed = store.getState().users.isAuthed
  if (isAuthed === true) {
    if (store.getState().routing.locationBeforeTransitions.pathname === '/') {
      store.dispatch(replace('feed'))
      console.log('CheckAuth function changed the path to feed')
    } else {
      console.log('CheckAuth function ran & User is Authed but path is not /')
      return
    }
  } else {
    if (store.getState().routing.locationBeforeTransitions.pathname !== '/') {
      store.dispatch(replace('/'))
      console.log('CheckAuth function changed the path to /')
    }
  }
}

export function errorAuth () {
  if (store.getState().users.isFetching === true) {
    return
  }
  const isAuthed = store.getState().users.isAuthed

  if (isAuthed === true) {
    replace('/feed')
  } else {
    replace('/')
  }
}


const App = () => (
    <MuiThemeProvider>
      <Provider store = {store}>
        {getRoutes(checkAuth, history, errorAuth)}
      </Provider>
    </MuiThemeProvider>
    )

ReactDOM.render(
    <App/>,
  document.getElementById('app')
)

And my routes are declarative like this:

import React from 'react'
import { Router, Route, IndexRoute } from 'react-router'
import { MainContainer, HomeContainer, FeedContainer } from 'containers'
import {Error404} from 'components'

export default function getRoutes (checkAuth, history, errorAuth) {
  return (
      <Router history={history}>
        <Route path='/' component={MainContainer}>
          <IndexRoute component = {HomeContainer} onEnter = {checkAuth}/>
          <Route path='feed' component = {FeedContainer} onEnter = {checkAuth}/>
          <Route path='*' component = {Error404} />
        </Route>
      </Router>
  )
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant