Guest User

Untitled

a guest
Mar 17th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. // todo/actions.js
  2. export const getTodos = (dispatch) => () => {
  3. dispatch({ type: 'GET_TODOS_REQUEST' });
  4. return fetch('/api/v1/todos')
  5. .then((todos) => dispatch({ type: 'GET_TODOS_SUCCESS', payload: todos })
  6. .catch((error) => dispatch({ type: 'GET_TODOS_FAILURE', payload: error, error: true });
  7. };
  8.  
  9. // todo/reducer.js
  10. const initialState = { todos: [] };
  11. export const todoReducer = (state = initialState, action) => {
  12. switch(action.type) {
  13. case 'GET_TODOS_REQUEST':
  14. return { ...state, isFetching: true };
  15. case 'GET_TODOS_SUCCESS':
  16. return { ...state, isFetching: false, todos: action.payload };
  17. case 'GET_TODOS_FAILURE':
  18. return { ...state, isFetching: false, errorMessage: action.payload.message };
  19. default:
  20. return state;
  21. }
  22. };
Add Comment
Please, Sign In to add comment