Skip to content

Instantly share code, notes, and snippets.

@productioncoder
Last active November 11, 2018 21:05
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/f5b05eba3b0ddcf1fecd7ab34b53d6ac to your computer and use it in GitHub Desktop.
Save productioncoder/f5b05eba3b0ddcf1fecd7ab34b53d6ac to your computer and use it in GitHub Desktop.
Youtube in React: perform channel request if needed
/* ... */
import {SEARCH_LIST_RESPONSE, VIDEO_LIST_RESPONSE} from '../api/youtube-api-response-types';
/* ... */
function* fetchVideoDetails(responses, shouldFetchChannelInfo) {
const searchListResponse = responses.find(response => response.result.kind === SEARCH_LIST_RESPONSE);
const relatedVideoIds = searchListResponse.result.items.map(relatedVideo => relatedVideo.id.videoId);
const requests = relatedVideoIds.map(relatedVideoId => {
return buildVideoDetailRequest.bind(null, relatedVideoId);
});
if (shouldFetchChannelInfo) {
// we have to extract the video's channel id from the video details response
// so we can load additional channel information.
// this is only needed, when a user directly accesses .../watch?v=1234
// because then we only know the video id
const videoDetailResponse = responses.find(response => response.result.kind === VIDEO_LIST_RESPONSE);
const videos = videoDetailResponse.result.items;
if (videos && videos.length) {
requests.push(buildChannelRequest.bind(null, videos[0].snippet.channelId));
}
}
/* ... */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment