Skip to content

Instantly share code, notes, and snippets.

@Joe1220
Created August 11, 2018 07:23
Show Gist options
  • Select an option

  • Save Joe1220/d0f4ed9d1e0eeff02a16bc06e7b731d3 to your computer and use it in GitHub Desktop.

Select an option

Save Joe1220/d0f4ed9d1e0eeff02a16bc06e7b731d3 to your computer and use it in GitHub Desktop.
// imports
import data from "MOCK_DATA.json";
// actions
const SET_PRODUCTS = "SET_PRODUCTS";
// action creators
function setProducts(items) {
return {
type: SET_PRODUCTS,
items
};
}
// API Actions
function getProducts() {
return (dispatch, getState) => {
fetch("MOCK_DATA.json", {
headers : {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
})
.then(response => data)
.then(json => dispatch(setProducts(json)))
.catch(err => console.log(err));
};
}
// Initial State
const initialState = {};
// Reducer
function reducer(state = initialState, action) {
switch (action.type) {
case SET_PRODUCTS:
return applySetProducts(state, action);
default:
return state;
}
}
// Reducer Functions
function applySetProducts(state, action) {
const { items } = action;
return {
...state,
items
};
}
// Exports
const actionCreators = {
getProducts
};
export { actionCreators };
// Export reducer by default
export default reducer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment