Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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