Created
July 8, 2016 10:06
-
-
Save Haraldson/f772dd545d4a68cd26a851f78fb408c9 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
import api from 'utilities/api' | |
// Actions | |
const fetchingEvents = () => { | |
return { | |
type: 'events:fetching' | |
} | |
} | |
const fetchEventsFailure = (error) => { | |
return { | |
type: 'events:fetch:failure', | |
error: error.msg | |
} | |
} | |
const fetchEventsSuccess = (data, timestamp) => { | |
return { | |
type: 'events:fetch:success', | |
data, | |
timestamp | |
} | |
} | |
export function fetchAndHandleEvents() { | |
return function(dispatch) | |
{ | |
dispatch(fetchingEvents()) | |
return api.getEvents() | |
.then((data) => dispatch(fetchEventsSuccess(data, Date.now()))) | |
.catch((error) => dispatch(fetchEventsFailure(error))) | |
} | |
} | |
const initialState = { | |
data: [], | |
isFetching: false, | |
lastUpdated: 0, | |
error: '' | |
} | |
// Reducer | |
export default (state = initialState, action) => { | |
switch(action.type) { | |
case 'events:fetching': | |
return { | |
...state, | |
isFetching: true, | |
error: '' | |
} | |
case 'events:fetch:failure': | |
return { | |
...state, | |
isFetching: false, | |
error: action.error | |
} | |
case 'events:fetch:success': | |
return { | |
...state, | |
data: action.data, | |
lastUpdated: action.timestamp, | |
isFetching: false, | |
error: '' | |
} | |
default: | |
return state | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment