Last active
December 17, 2018 14:54
-
-
Save productioncoder/ab261964cdb0af5b1b6048966f18e747 to your computer and use it in GitHub Desktop.
Youtube videos reducer: parsing most popular videos by id responses
This file contains 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
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