Skip to content

Instantly share code, notes, and snippets.

@productioncoder
Last active November 10, 2018 10:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save productioncoder/675d690c3b9408099daef8dd8ca59fd3 to your computer and use it in GitHub Desktop.
Save productioncoder/675d690c3b9408099daef8dd8ca59fd3 to your computer and use it in GitHub Desktop.
Youtube in React: handling VIDEO_DETAILS_SUCCESS action
function reduceVideoDetails(responses, prevState) {
const videoResponses = responses.filter(response => response.result.kind === VIDEO_LIST_RESPONSE);
const parsedVideos = videoResponses.reduce((videoMap, response) => {
// we're explicitly asking for a video with a particular id
// so the response set must either contain 0 items (if a video with the id does not exist)
// or at most one item (i.e. the channel we've been asking for)
const video = response.result.items ? response.result.items[0] : null;
if (!video) {
return videoMap;
}
videoMap[video.id] = video;
return videoMap;
}, {});
return {
...prevState,
byId: {...prevState.byId, ...parsedVideos},
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment