Skip to content

Instantly share code, notes, and snippets.

@productioncoder
Last active December 17, 2018 14:54
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/ab261964cdb0af5b1b6048966f18e747 to your computer and use it in GitHub Desktop.
Save productioncoder/ab261964cdb0af5b1b6048966f18e747 to your computer and use it in GitHub Desktop.
Youtube videos reducer: parsing most popular videos by id responses
function reduceFetchMostPopularVideosByCategory(responses, categories, prevState) {
let videoMap = {};
let byCategoryMap = {};
responses.forEach((response, index) => {
// ignore answer if there was an error
if (response.status === 400) return;
const categoryId = categories[index];
const {byId, byCategory} = groupVideosByIdAndCategory(response.result);
videoMap = {...videoMap, ...byId};
byCategoryMap[categoryId] = byCategory;
});
// compute new state
return {
...prevState,
byId: {...prevState.byId, ...videoMap},
byCategory: {...prevState.byCategory, ...byCategoryMap},
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment