Last active
December 8, 2017 11:28
-
-
Save vzaidman/e15c3646140a4203c68d27b66e9deb02 to your computer and use it in GitHub Desktop.
The Vanilla Redux Counterpart
This file contains 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
export const FETCH_TODOS_LOADING = 'FETCH_TODOS@LOADING' | |
export const FETCH_TODOS_SUCCESS = 'FETCH_TODOS@SUCCESS' | |
export const FETCH_TODOS_FAILURE = 'FETCH_TODOS@FAILURE' | |
This file contains 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
import { FETCH_TODOS_LOADING, FETCH_TODOS_SUCCESS, FETCH_TODOS_FAILURE } from './consts' | |
export const fetchTodos = filter => { | |
return (dispatch, getState) => { | |
dispatch({type: FETCH_TODOS_LOADING}) | |
fetchTodosFromServer(filter) | |
.then(data => | |
dispatch({type: FETCH_TODOS_SUCCESS, payload: data})) | |
.catch(error => | |
dispatch({type: FETCH_TODOS_FAILURE, payload: error})) | |
} | |
} | |
This file contains 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
import { FETCH_TODOS_LOADING, FETCH_TODOS_SUCCESS, FETCH_TODOS_FAILURE } from './consts' | |
const initialState = { | |
error: undefined, | |
loading: false, | |
data: undefined, | |
} | |
export const todos = (state = initialState, action) => { | |
switch (action.type){ | |
case FETCH_TODOS_LOADING: | |
return { loading: true } | |
case FETCH_TODOS_SUCCESS: | |
return { data: action.payload, loading: false } | |
case FETCH_TODOS_FAILURE: | |
return { error: action.payload, loading: false } | |
default: | |
return state | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment