-
Notifications
You must be signed in to change notification settings - Fork 981
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #28303 - connect react router to redux
- Loading branch information
Showing
10 changed files
with
101 additions
and
31 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
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import { createBrowserHistory } from 'history'; | ||
import forceSingleton from '../react_app/common/forceSingleton'; | ||
|
||
export default createBrowserHistory(); | ||
const history = forceSingleton('history', () => createBrowserHistory()); | ||
export default history; |
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
26 changes: 24 additions & 2 deletions
26
webpack/assets/javascripts/react_app/redux/middlewares/index.js
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,2 +1,24 @@ | ||
export { APIMiddleware } from '../API'; | ||
export { IntervalMiddleware } from './IntervalMiddleware'; | ||
import createLogger from 'redux-logger'; | ||
import thunk from 'redux-thunk'; | ||
import { routerMiddleware } from 'connected-react-router'; | ||
import { APIMiddleware } from '../API'; | ||
import { IntervalMiddleware } from './IntervalMiddleware'; | ||
import history from '../../history'; | ||
|
||
const logReduxToConsole = () => { | ||
const isProduction = process.env.NODE_ENV === 'production'; | ||
const isLogger = process.env.REDUX_LOGGER; | ||
|
||
if (!isProduction && !global.__testing__) { | ||
if (isLogger === undefined || isLogger === true) return true; | ||
} | ||
return isProduction && isLogger; | ||
}; | ||
|
||
export const middlewares = [ | ||
thunk, | ||
IntervalMiddleware, | ||
APIMiddleware, | ||
routerMiddleware(history), | ||
...(logReduxToConsole() ? [createLogger()] : []), | ||
]; |
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
6 changes: 6 additions & 0 deletions
6
webpack/assets/javascripts/react_app/routes/RouterSelector.js
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,6 @@ | ||
export const selectRouter = state => state.router; | ||
export const selectRouterLocation = state => selectRouter(state).location; | ||
export const selectRouterPath = state => selectRouterLocation(state).pathname; | ||
export const selectRouterSearch = state => selectRouterLocation(state).search; | ||
export const selectRouterHash = state => selectRouterLocation(state).hash; | ||
export const selectLastHistoryAction = state => selectRouter(state).action; |
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,42 @@ | ||
import { Meta } from '@theforeman/stories'; | ||
|
||
<Meta | ||
title="Introduction|Connected React Router" | ||
parameters={{ | ||
storyWeight: 140, | ||
}} | ||
/> | ||
|
||
# Connected React Router | ||
Foreman uses react-router for client routing in react pages, however foreman contains lots of erb content which includes react components. | ||
in order to control react router better and to have one source of truth, | ||
foreman uses `connected-react-router` which enable controling react router via redux. | ||
|
||
### Push url | ||
This pushed a new url (including querystring) to react router and creates a transition | ||
|
||
```js | ||
import { pushUrl } from '../foreman_navigation'; | ||
import { foremanUrl } from '../foreman_tools'; | ||
|
||
<Button onClick={() => pushUrl(foremanUrl('/hosts'))}> | ||
Click Me! | ||
</Button> | ||
``` | ||
|
||
### React Router Selectors | ||
You can use react router selectors for retriving data | ||
|
||
selectRouterLocation - the current location. | ||
electRouterPath - the current path | ||
selectRouterSearch - the current search | ||
selectRouterHash - the current hash | ||
selectRouter - the entire router object which includes every entry above | ||
|
||
```js | ||
import { useSelector } from 'react-redux'; | ||
import { selectRouterLocation } from '../routes/RouterSelector'; | ||
|
||
|
||
const location = useSelector(selectRouterLocation); | ||
``` |