Skip to content

Instantly share code, notes, and snippets.

@productioncoder
Created October 17, 2018 19:47
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/7ef4ae12710cbfaa4a6ea112f2e13b64 to your computer and use it in GitHub Desktop.
Save productioncoder/7ef4ae12710cbfaa4a6ea112f2e13b64 to your computer and use it in GitHub Desktop.
Youtube reducing fetchMostPopularVideos request
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