Skip to content
This repository has been archived by the owner on Sep 7, 2020. It is now read-only.

Extra middlewares and store enhancers for redux store #102

Merged
merged 1 commit into from
Jan 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ renamed accordingly.
``defaultLayout``
- Changed: ``redux`` as been upgraded to v4.x.
([#91](https://github.com/MoOx/statinamic/pull/91))
- Added: Allow to pass extra middlewares and store enhancers to redux store
([#102](https://github.com/MoOx/statinamic/pull/102))
- Added: Option to add static assets such as images, video
([#94](https://github.com/MoOx/statinamic/pull/94))
- Added: ``statinamic`` section in ``package.json`` can be used to define core
Expand Down
59 changes: 59 additions & 0 deletions docs/content/docs/advanced/redux.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,62 @@ if (module.hot) {

export default store
```
## Adding custom middlewares and store enhancers to Redux store

`statinamic/lib/redux/createStore` accepts two extra parameters that
allow you to pass custom middlewares and store enhancers

Here is an example of adding
[redux-logger](https://github.com/fcomb/redux-logger) and
[redux-search](https://github.com/treasure-data/redux-search)
to Redux store

```js
import { combineReducers } from "redux"
import createStore from "statinamic/lib/redux/createStore"
import * as statinamicReducers from "statinamic/lib/redux/modules"
import minifyCollection from "statinamic/lib/md-collection-loader/minify"
import { reducer as searchReducer, reduxSearch } from "redux-search"
import createLogger from "redux-logger"

import * as layouts from "layouts"

const extraMiddlewares = { createLogger() }
const extraStoreEnhancers = {
reduxSearch({
resourceIndexes: {
books: ['author', 'title']
},
resourceSelector: (resourceName, state) => {
return state.resources.get(resourceName)
}
})
}

const store = createStore(
combineReducers({
...statinamicReducers,
...{
search: searchReducer
}
}),

// initialState
{
...(typeof window !== "undefined") && window.__INITIAL_STATE__,

// static build optimization
...__PROD__ && {
collection:
minifyCollection(require("statinamic/lib/md-collection-loader/cache")),
},

layouts,
},
extraMiddlewares,
extraStoreEnhancers,

)

export default store
```
25 changes: 21 additions & 4 deletions src/redux/createStore/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { createStore, applyMiddleware, compose } from "redux"
import thunk from "redux-thunk"

export default function(reducer = {}, initialState = {}) {
export default function(
reducer = {},
initialState = {},
extraMiddlewares = {},
extraStoreEnhancers = {}
) {
function promiseMiddleware() {
return (next) => (action) => {
const { promise, types, ...rest } = action
Expand Down Expand Up @@ -35,13 +40,25 @@ export default function(reducer = {}, initialState = {}) {
}

finalCreateStore = compose(
applyMiddleware(promiseMiddleware, thunk),
applyMiddleware(
promiseMiddleware,
thunk,
...extraMiddlewares
),
devTools(),
persistState(getDebugSessionKey())
persistState(getDebugSessionKey()),
...extraStoreEnhancers
)(createStore)
}
else {
finalCreateStore = applyMiddleware(promiseMiddleware, thunk)(createStore)
finalCreateStore = compose(
applyMiddleware(
promiseMiddleware,
thunk,
...extraMiddlewares
),
...extraStoreEnhancers
)(createStore)
}

return finalCreateStore(reducer, initialState)
Expand Down