forked from supasate/connected-react-router
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
320 additions
and
132 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('./lib/immutable') |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import * as actions from './actions' | ||
import createConnectedRouter from './ConnectedRouter' | ||
import createConnectRouter from './reducer' | ||
import routerMiddleware from './middleware' | ||
|
||
const createAll = structure => ({ | ||
...actions, | ||
ConnectedRouter: createConnectedRouter(structure), | ||
connectRouter: createConnectRouter(structure), | ||
routerMiddleware, | ||
}) | ||
|
||
export default createAll |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import createAll from './createAll' | ||
import immutableStructure from './structure/immutable' | ||
|
||
export const { | ||
LOCATION_CHANGE, | ||
CALL_HISTORY_METHOD, | ||
push, | ||
replace, | ||
go, | ||
goBack, | ||
goForward, | ||
routerActions, | ||
ConnectedRouter, | ||
connectRouter, | ||
routerMiddleware, | ||
} = createAll(immutableStructure) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
export { LOCATION_CHANGE } from './actions' | ||
export connectRouter from './reducer' | ||
import createAll from './createAll' | ||
import plainStructure from './structure/plain' | ||
|
||
export { | ||
export const { | ||
LOCATION_CHANGE, | ||
CALL_HISTORY_METHOD, | ||
push, replace, go, goBack, goForward, | ||
routerActions | ||
} from './actions' | ||
|
||
export routerMiddleware from './middleware' | ||
export ConnectedRouter from './ConnectedRouter' | ||
push, | ||
replace, | ||
go, | ||
goBack, | ||
goForward, | ||
routerActions, | ||
ConnectedRouter, | ||
connectRouter, | ||
routerMiddleware, | ||
} = createAll(plainStructure) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,46 @@ | ||
import { LOCATION_CHANGE } from './actions' | ||
|
||
/** | ||
* This reducer will update the state with the most recent location history | ||
* has transitioned to. | ||
*/ | ||
const routerReducer = (state, { type, payload } = {}) => { | ||
if (type === LOCATION_CHANGE) { | ||
return { | ||
...state, | ||
...payload, | ||
const createConnectRouter = (structure) => { | ||
const { | ||
filterNotRouter, | ||
fromJS, | ||
getIn, | ||
merge, | ||
setIn, | ||
} = structure | ||
/** | ||
* This reducer will update the state with the most recent location history | ||
* has transitioned to. | ||
*/ | ||
const routerReducer = (state, { type, payload } = {}) => { | ||
if (type === LOCATION_CHANGE) { | ||
return merge(state, payload) | ||
} | ||
} | ||
|
||
return state | ||
} | ||
|
||
const connectRouter = (history) => { | ||
const initialRouterState = { | ||
location: history.location, | ||
action: history.action, | ||
return state | ||
} | ||
// Wrap a root reducer and return a new root reducer with router state | ||
return (rootReducer) => (state, action) => { | ||
let routerState = initialRouterState | ||
|
||
// Extract router state | ||
if (state) { | ||
const { router, ...rest} = state | ||
routerState = router || routerState | ||
state = rest | ||
} | ||
const reducerResults = rootReducer(state, action) | ||
const connectRouter = (history) => { | ||
const initialRouterState = fromJS({ | ||
location: history.location, | ||
action: history.action, | ||
}) | ||
// Wrap a root reducer and return a new root reducer with router state | ||
return (rootReducer) => (state, action) => { | ||
let routerState = initialRouterState | ||
|
||
// Extract router state | ||
if (state) { | ||
routerState = getIn(state, 'router') || routerState | ||
state = filterNotRouter(state) | ||
} | ||
const reducerResults = rootReducer(state, action) | ||
|
||
return { | ||
...reducerResults, | ||
router: routerReducer(routerState, action) | ||
return setIn(reducerResults, 'router', routerReducer(routerState, action)) | ||
} | ||
} | ||
|
||
return connectRouter | ||
} | ||
|
||
export default connectRouter | ||
export default createConnectRouter |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Iterable } from 'immutable' | ||
import toPath from 'lodash.topath' | ||
import plainGetIn from '../plain/getIn' | ||
|
||
const getIn = (state, field) => | ||
Iterable.isIterable(state) | ||
? state.getIn(toPath(field)) | ||
: plainGetIn(state, field) | ||
|
||
export default getIn |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Iterable, fromJS } from 'immutable' | ||
import getIn from './getIn' | ||
import setIn from './setIn' | ||
|
||
const structure = { | ||
filterNotRouter: (state) => state.filterNot((v, k) => k === 'router'), | ||
fromJS: jsValue => fromJS(jsValue, (key, value) => | ||
Iterable.isIndexed(value) ? value.toList() : value.toMap()), | ||
getIn, | ||
merge: (state, payload) => state.merge(payload), | ||
setIn, | ||
toJS: value => Iterable.isIterable(value) ? value.toJS() : value, | ||
} | ||
|
||
export default structure |
Oops, something went wrong.