add topic count in random topic component #7

Merged
sanj merged 2 commits from feature/add-topic-count into develop 2018-12-07 17:44:24 +00:00
5 changed files with 9 additions and 7 deletions

View File

@ -40,7 +40,7 @@ export function getRandomTopicVideos(allTopics, numVideos=4) {
return dispatch => { return dispatch => {
const randomTopic = getRandomTopic(allTopics); const randomTopic = getRandomTopic(allTopics);
dispatch(loadingRandomTopicVideos()); dispatch(loadingRandomTopicVideos());
fetchVideosByTopic(randomTopic, 0, 4) fetchVideosByTopic(randomTopic.name, 0, 4)
.then(videos => { .then(videos => {
dispatch(loadedRandomTopicVideos(randomTopic, videos)); dispatch(loadedRandomTopicVideos(randomTopic, videos));
}); });

View File

@ -21,7 +21,7 @@ class RandomTopic extends React.Component {
See All Topics See All Topics
</Link> </Link>
<Link to="/topics"> <Link to="/topics">
See All videos of {this.props.topicName} () See All videos of {this.props.topicName} ({this.props.topicCount})
</Link> </Link>
</Grid> </Grid>
</section> </section>
@ -33,7 +33,8 @@ class RandomTopic extends React.Component {
RandomTopic.propTypes = { RandomTopic.propTypes = {
topicName: PropTypes.string, topicName: PropTypes.string,
videos: PropTypes.array, videos: PropTypes.array,
loading: PropTypes.bool loading: PropTypes.bool,
topicCount: PropTypes.number
}; };
export default (RandomTopic); export default (RandomTopic);

View File

@ -18,7 +18,6 @@ class Home extends React.Component {
} }
componentWillReceiveProps(nextProps) { componentWillReceiveProps(nextProps) {
console.log('nextProps', nextProps);
// This is slightly awkward - we need to check that we have all topics, // This is slightly awkward - we need to check that we have all topics,
// But don't have a random topic yet (or at not already loading a random topic) // But don't have a random topic yet (or at not already loading a random topic)
if ((this.props.allTopics || nextProps.allTopics) && !this.props.loadingRandomTopic && !nextProps.loadingRandomTopic && !this.props.randomTopic) { if ((this.props.allTopics || nextProps.allTopics) && !this.props.loadingRandomTopic && !nextProps.loadingRandomTopic && !this.props.randomTopic) {
@ -31,6 +30,7 @@ class Home extends React.Component {
<div> <div>
<RandomTopic <RandomTopic
topicName={ this.props.randomTopic || ''} topicName={ this.props.randomTopic || ''}
topicCount={ this.props.randomTopicCount }
videos = { this.props.randomTopicVideos || [] } videos = { this.props.randomTopicVideos || [] }
loading = { this.props.loadingAllTopics || this.props.loadingRandomTopic } loading = { this.props.loadingAllTopics || this.props.loadingRandomTopic }
/> />
@ -45,6 +45,7 @@ const mapStateToProps = state => ({
allTopics: state.topics.allTopics, allTopics: state.topics.allTopics,
loadingAllTopics: state.topics.loadingAllTopics, loadingAllTopics: state.topics.loadingAllTopics,
randomTopic: state.topics.randomTopic, randomTopic: state.topics.randomTopic,
randomTopicCount: state.topics.randomTopicCount,
loadingRandomTopic: state.topics.loadingRandomTopic, loadingRandomTopic: state.topics.loadingRandomTopic,
randomTopicVideos: state.topics.randomTopicVideos randomTopicVideos: state.topics.randomTopicVideos
}); });

View File

@ -25,7 +25,8 @@ export default function(state={}, action) {
case LOADED_RANDOM_TOPIC: case LOADED_RANDOM_TOPIC:
return Object.assign({}, state, { return Object.assign({}, state, {
loadingRandomTopic: false, loadingRandomTopic: false,
randomTopic: action.payload.topic, randomTopic: action.payload.topic.name,
randomTopicCount: action.payload.topic.items,
randomTopicVideos: action.payload.videos randomTopicVideos: action.payload.videos
}); });

View File

@ -1,6 +1,5 @@
export default function getRandomTopic(allTopics, minVideos=4) { export default function getRandomTopic(allTopics, minVideos=4) {
const validTopics = allTopics.filter(topic => topic.items >= minVideos); const validTopics = allTopics.filter(topic => topic.items >= minVideos);
console.log('valid topics', validTopics); return validTopics[Math.floor(Math.random() * validTopics.length)];
return validTopics[Math.floor(Math.random() * validTopics.length)].name;
} }