Skip to content

Instantly share code, notes, and snippets.

@productioncoder productioncoder/watch.js
Last active Nov 11, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.