Skip to content

Instantly share code, notes, and snippets.

@idan idan/clipActions.js
Created Dec 1, 2015

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
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.