Skip to content
This repository has been archived by the owner on Jan 1, 2024. It is now read-only.

Commit

Permalink
feat(reducers): add metarouter reducer
Browse files Browse the repository at this point in the history
feat(reducers): add `metarouter` reducer allowing us to store current
route `meta` info in state. Probably, this reducer will be separated in
own package
  • Loading branch information
Metnew committed Nov 11, 2017
1 parent cd6ff27 commit eaeffed
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/common/reducers/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// @flow
import {combineReducers} from 'redux'
import {routerReducer} from 'react-router-redux'
import {getMetaRoutes} from 'routing'

import type {State as AuthState} from 'reducers/auth'
import type {State as LayoutState} from 'reducers/layout'
import type {State as EntitiesLinksState} from 'reducers/links'

import {layout} from './layout'
import {metaRouting} from './metarouter'
import {links} from './links'
import {auth} from './auth'

Expand All @@ -17,7 +19,8 @@ export default combineReducers({
entities: combineReducers({
links
}),
routing: routerReducer
routing: routerReducer,
metaRouting: metaRouting(getMetaRoutes())
})

export type GlobalState = {layout: LayoutState} & {me: {auth: AuthState}} & {
Expand Down
19 changes: 19 additions & 0 deletions src/common/reducers/metarouter/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {matchPath} from 'react-router'
import {LOCATION_CHANGE} from 'react-router-redux'

export const initialState = {}

export const metaRouting = allRoutes => (state = initialState, action) => {
switch (action.type) {
case LOCATION_CHANGE: {
const {pathname} = action.payload
const currentRoute =
allRoutes.filter(a => matchPath(pathname, a))[0] || {}
return {
currentRoute
}
}
default:
return state
}
}

0 comments on commit eaeffed

Please sign in to comment.