Skip to content

Instantly share code, notes, and snippets.

@Haraldson
Created July 8, 2016 10:06
Show Gist options
  • Save Haraldson/f772dd545d4a68cd26a851f78fb408c9 to your computer and use it in GitHub Desktop.
Save Haraldson/f772dd545d4a68cd26a851f78fb408c9 to your computer and use it in GitHub Desktop.
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