From 69c2181afa233994f52f86f3c1729fc0c3f8a38b Mon Sep 17 00:00:00 2001 From: Sanjay Bhangar Date: Fri, 7 Dec 2018 19:37:57 +0200 Subject: [PATCH 1/2] add random topic items count to state --- src/containers/Home.js | 3 ++- src/reducers/topics.js | 3 ++- src/utils/get-random-topic.js | 3 +-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/containers/Home.js b/src/containers/Home.js index b426128..fa87710 100644 --- a/src/containers/Home.js +++ b/src/containers/Home.js @@ -18,7 +18,6 @@ class Home extends React.Component { } componentWillReceiveProps(nextProps) { - console.log('nextProps', nextProps); // 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) if ((this.props.allTopics || nextProps.allTopics) && !this.props.loadingRandomTopic && !nextProps.loadingRandomTopic && !this.props.randomTopic) { @@ -31,6 +30,7 @@ class Home extends React.Component {
@@ -45,6 +45,7 @@ const mapStateToProps = state => ({ allTopics: state.topics.allTopics, loadingAllTopics: state.topics.loadingAllTopics, randomTopic: state.topics.randomTopic, + randomTopicCount: static.topics.randomTopicCount, loadingRandomTopic: state.topics.loadingRandomTopic, randomTopicVideos: state.topics.randomTopicVideos }); diff --git a/src/reducers/topics.js b/src/reducers/topics.js index 2beda0a..5866f75 100644 --- a/src/reducers/topics.js +++ b/src/reducers/topics.js @@ -25,7 +25,8 @@ export default function(state={}, action) { case LOADED_RANDOM_TOPIC: return Object.assign({}, state, { loadingRandomTopic: false, - randomTopic: action.payload.topic, + randomTopic: action.payload.topic.name, + randomTopicCount: action.payload.topic.items, randomTopicVideos: action.payload.videos }); diff --git a/src/utils/get-random-topic.js b/src/utils/get-random-topic.js index fb9796a..32c11ed 100644 --- a/src/utils/get-random-topic.js +++ b/src/utils/get-random-topic.js @@ -1,6 +1,5 @@ export default function getRandomTopic(allTopics, minVideos=4) { const validTopics = allTopics.filter(topic => topic.items >= minVideos); - console.log('valid topics', validTopics); - return validTopics[Math.floor(Math.random() * validTopics.length)].name; + return validTopics[Math.floor(Math.random() * validTopics.length)]; } \ No newline at end of file -- 2.17.1 From 2d755be6b81de5f20d2733d7b9e8ad48f9726ca8 Mon Sep 17 00:00:00 2001 From: Sanjay Bhangar Date: Fri, 7 Dec 2018 19:43:25 +0200 Subject: [PATCH 2/2] add count for current topic in random topic component --- src/actions/topics.js | 2 +- src/components/RandomTopic.js | 5 +++-- src/containers/Home.js | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/actions/topics.js b/src/actions/topics.js index 32edf52..50e53c2 100644 --- a/src/actions/topics.js +++ b/src/actions/topics.js @@ -40,7 +40,7 @@ export function getRandomTopicVideos(allTopics, numVideos=4) { return dispatch => { const randomTopic = getRandomTopic(allTopics); dispatch(loadingRandomTopicVideos()); - fetchVideosByTopic(randomTopic, 0, 4) + fetchVideosByTopic(randomTopic.name, 0, 4) .then(videos => { dispatch(loadedRandomTopicVideos(randomTopic, videos)); }); diff --git a/src/components/RandomTopic.js b/src/components/RandomTopic.js index b9c6bd2..b70439a 100644 --- a/src/components/RandomTopic.js +++ b/src/components/RandomTopic.js @@ -21,7 +21,7 @@ class RandomTopic extends React.Component { See All Topics - See All videos of {this.props.topicName} () + See All videos of {this.props.topicName} ({this.props.topicCount}) @@ -33,7 +33,8 @@ class RandomTopic extends React.Component { RandomTopic.propTypes = { topicName: PropTypes.string, videos: PropTypes.array, - loading: PropTypes.bool + loading: PropTypes.bool, + topicCount: PropTypes.number }; export default (RandomTopic); \ No newline at end of file diff --git a/src/containers/Home.js b/src/containers/Home.js index fa87710..55d38b9 100644 --- a/src/containers/Home.js +++ b/src/containers/Home.js @@ -45,7 +45,7 @@ const mapStateToProps = state => ({ allTopics: state.topics.allTopics, loadingAllTopics: state.topics.loadingAllTopics, randomTopic: state.topics.randomTopic, - randomTopicCount: static.topics.randomTopicCount, + randomTopicCount: state.topics.randomTopicCount, loadingRandomTopic: state.topics.loadingRandomTopic, randomTopicVideos: state.topics.randomTopicVideos }); -- 2.17.1