Skip to content

Instantly share code, notes, and snippets.

@idan
Created December 1, 2015 19:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save idan/c38cac0d3e5fda41d01e to your computer and use it in GitHub Desktop.
Save idan/c38cac0d3e5fda41d01e to your computer and use it in GitHub Desktop.
export function addClip (id) {
return (dispatch, getState) => {
// prevent adding duplicate clips
const state = getState()
if (state.Clips.has(id)) {
return
}
// create the entry with a asyncState of PENDING
dispatch({
type: ActionTypes.Clip.addClip,
payload: {
asyncState: AsyncState.PENDING,
fetchTimestamp: new Date(),
id: id
}
})
// go fetch the content
console.debug(`about to fetch id ${id}...`)
dataclipsAPI(`/${id}.json`)
.then((response) => response.json())
.catch((error) => {
console.error('Clip fetched failure!', error)
// Ruh roh, failure. Set asyncState = REJECTED and add an error field
dispatch({
type: ActionTypes.Clip.addClip,
payload: {
asyncState: AsyncState.REJECTED,
id,
error
}
})
})
.then((content) => {
// Success, update our state with the content of the clip
// and set asyncState to FULFILLED
dispatch({
type: ActionTypes.Clip.addClip,
payload: {
asyncState: AsyncState.FULFILLED,
id,
content
}
})
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment