Created
October 17, 2018 19:47
-
-
Save productioncoder/7ef4ae12710cbfaa4a6ea112f2e13b64 to your computer and use it in GitHub Desktop.
Youtube reducing fetchMostPopularVideos request
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 reduceFetchMostPopularVideos(response, prevState) { | |
const videoMap = response.items.reduce((accumulator, video) => { | |
accumulator[video.id] = video; | |
return accumulator; | |
}, {}); | |
let items = Object.keys(videoMap); | |
if (response.hasOwnProperty('prevPageToken') && prevState.mostPopular) { | |
items = [...prevState.mostPopular.items, ...items]; | |
} | |
const mostPopular = { | |
totalResults: response.pageInfo.totalResults, | |
nextPageToken: response.nextPageToken, | |
items, | |
}; | |
return { | |
...prevState, | |
mostPopular, | |
byId: {...prevState.byId, ...videoMap}, | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment