Created
December 1, 2015 19:02
-
-
Save idan/c38cac0d3e5fda41d01e 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
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