Created
August 11, 2018 07:23
-
-
Save Joe1220/d0f4ed9d1e0eeff02a16bc06e7b731d3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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