This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const { ...theUsual, thunk, initialDispatch } = connectRoutes( | |
| history, | |
| routesMap, { | |
| ...options, | |
| initialDispatch: isServer ? false : true | |
| } | |
| ) | |
| ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export default async (req, res) => { | |
| const jwToken = req.cookies.jwToken | |
| const preLoadedState = { jwToken } | |
| const history = createHistory({ initialEntries: [req.path] }) | |
| const { store, thunk } = configureStore(history, preLoadedState) | |
| ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // src/options.js | |
| const options = { | |
| onBeforeChange: (dispatch, getState, action) => { | |
| const state = getState() | |
| const user = userFromState(action.type, state) | |
| if (!user) { | |
| const action = redirect({ type: 'LOGIN' }) | |
| dispatch(action) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { myThunkA, myThunkB } from '../src/actions/async' | |
| export default async (req, res) => { | |
| const history = createHistory({ initialEntries: [req.path] }) | |
| const { store, thunk } = configureStore(history) | |
| await Promise.all([ | |
| store.dispatch(myThunkA), | |
| store.dispatch(myThunkB) | |
| ]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import createHistory from 'history/createMemoryHistory' | |
| import { NOT_FOUND } from 'redux-first-router' | |
| import configureStore from '../src/configureStore' | |
| export default async (req, res) => { | |
| const history = createHistory({ initialEntries: [req.path] }) | |
| const { store, thunk } = configureStore(history) | |
| let location = store.getState().location | |
| if (doesRedirect(location, res)) return false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { redirect } from 'redux-first-router' | |
| import { isAllowed } from './utils' | |
| const options = { | |
| onBeforeChange: (dispatch, getState, action) => { | |
| const allowed = isAllowed(action.type, getState()) | |
| if (!allowed) { | |
| const action = redirect({ type: 'LOGIN' }) | |
| dispatch(action) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export default () => async (req, res, next) => { | |
| const store = await configureStore(req, res) | |
| if (!store) return | |
| const app = <Provider store={store}><App /></Provider> | |
| ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import createHistory from 'history/createMemoryHistory' | |
| import { NOT_FOUND } from 'redux-first-router' | |
| import configureStore from '../src/configureStore' | |
| export default async (req, res) => { | |
| const history = createHistory({ initialEntries: [req.path] }) | |
| const { store, thunk } = configureStore(history) | |
| await thunk(store) // THE PAYOFF! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const routesMap = { | |
| HOME: '/', | |
| LIST: { | |
| path: '/list/:category', | |
| thunk: async (dispatch, getState) => { | |
| const { payload: { category } } = getState().location | |
| const videos = await fetchData(`/api/videos/${category}`) | |
| dispatch({ type: 'VIDEOS_FETCHED', payload: { videos, category } }) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { createStore, applyMiddleware, compose, combineReducers } from 'redux' | |
| import { connectRoutes } from 'redux-first-router' | |
| import routesMap from './routesMap' | |
| import options from './options' | |
| import * as reducers from './reducers' | |
| export default (history, preLoadedState) => { | |
| const { reducer, middleware, enhancer, thunk } = connectRoutes( | |
| history, |