Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // todo/actions.js
- export const getTodos = (dispatch) => () => {
- dispatch({ type: 'GET_TODOS_REQUEST' });
- return fetch('/api/v1/todos')
- .then((todos) => dispatch({ type: 'GET_TODOS_SUCCESS', payload: todos })
- .catch((error) => dispatch({ type: 'GET_TODOS_FAILURE', payload: error, error: true });
- };
- // todo/reducer.js
- const initialState = { todos: [] };
- export const todoReducer = (state = initialState, action) => {
- switch(action.type) {
- case 'GET_TODOS_REQUEST':
- return { ...state, isFetching: true };
- case 'GET_TODOS_SUCCESS':
- return { ...state, isFetching: false, todos: action.payload };
- case 'GET_TODOS_FAILURE':
- return { ...state, isFetching: false, errorMessage: action.payload.message };
- default:
- return state;
- }
- };
Add Comment
Please, Sign In to add comment