Redux middleware

This commit is contained in:
Rodrigo Pedroso 2019-06-19 15:38:21 -04:00
commit 8c3335094a
2 changed files with 31 additions and 3 deletions

View 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)
}
}
}

View file

@ -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