Redux middleware
This commit is contained in:
parent
fbf7a5452c
commit
8c3335094a
2 changed files with 31 additions and 3 deletions
22
src/js/middleware/index.js
Normal file
22
src/js/middleware/index.js
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
import { ADD_ARTICLE } from "../constants/action-types"
|
||||||
|
|
||||||
|
const forbiddenWords = ["spam", "money"]
|
||||||
|
|
||||||
|
export function forbiddenWordsMiddleware({ dispatch }) {
|
||||||
|
return function(next) {
|
||||||
|
return function(action) {
|
||||||
|
// do your stuff
|
||||||
|
if (action.type === ADD_ARTICLE) {
|
||||||
|
|
||||||
|
const foundWord = forbiddenWords.filter(word =>
|
||||||
|
action.payload.title.includes(word)
|
||||||
|
)
|
||||||
|
|
||||||
|
if (foundWord.length) {
|
||||||
|
return dispatch({ type: "FOUND_BAD_WORD" })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return next(action)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,12 @@
|
||||||
import { createStore } from "redux"
|
import { createStore, applyMiddleware, compose } from 'redux'
|
||||||
import rootReducer from "../reducers/index"
|
import rootReducer from '../reducers/index'
|
||||||
|
import { forbiddenWordsMiddleware } from '../middleware'
|
||||||
|
|
||||||
const store = createStore(rootReducer)
|
const storeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose
|
||||||
|
|
||||||
|
const store = createStore(
|
||||||
|
rootReducer,
|
||||||
|
storeEnhancers(applyMiddleware(forbiddenWordsMiddleware))
|
||||||
|
)
|
||||||
|
|
||||||
export default store
|
export default store
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue