Last active
November 11, 2018 21:05
-
-
Save productioncoder/f5b05eba3b0ddcf1fecd7ab34b53d6ac to your computer and use it in GitHub Desktop.
Youtube in React: perform channel request if needed
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
/* ... */ | |
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