Added a basic store, fleshing out for our use, including actions relevant to migationsTracker

This commit is contained in:
pepper committed 2021-01-19 19:09:09 -05:00
1 parent 271942ae7f
commit a3c380ffd1
5 files changed
+130 -33

No files matched your search

+3
View File
@@ -0,0 +1,3 @@
export const ADD_ARTICLE = "ADD_ARTICLE";
export const DATA_LOADED = "DATA_LOADED";
export const API_ERRORED = "API_ERRORED";
+21
View File
@@ -0,0 +1,21 @@
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);
};
};
}
+23
View File
@@ -0,0 +1,23 @@
import { ADD_ARTICLE, DATA_LOADED } from "../constants/action-types";
const initialState = {
articles: [],
remoteArticles: [],
};
function rootReducer(state = initialState, action) {
if (action.type === ADD_ARTICLE) {
return Object.assign({}, state, {
articles: state.articles.concat(action.payload),
});
}
if (action.type === DATA_LOADED) {
return Object.assign({}, state, {
remoteArticles: state.remoteArticles.concat(action.payload),
});
}
return state;
}
export default rootReducer;
+13
View File
@@ -0,0 +1,13 @@
import { createStore, applyMiddleware, compose } from "redux";
import rootReducer from "../reducers";
import { forbiddenWordsMiddleware } from "../middleware";
import thunk from "redux-thunk";
const storeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const store = createStore(
rootReducer,
storeEnhancers(applyMiddleware(forbiddenWordsMiddleware, thunk))
);
export default store;