From 8c66fb02519059604c9622b39abdad2a65eb2f94 Mon Sep 17 00:00:00 2001 From: Sanjay Bhangar Date: Thu, 20 Dec 2018 18:11:53 +0530 Subject: [PATCH] fix search results page for topics --- src/actions/search.js | 24 ++++++++++++------------ src/components/TopicsList.js | 2 +- src/containers/Results.js | 16 +++++++++------- src/reducers/index.js | 2 +- 4 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/actions/search.js b/src/actions/search.js index 4389284..7d0c044 100644 --- a/src/actions/search.js +++ b/src/actions/search.js @@ -1,7 +1,8 @@ import {LOADED_ALL_RESULTS, LOADED_ALL_TOPICS, START_LOADING_ALL_RESULTS} from "./action_types"; -import {fetchAllTopics} from "../utils/api"; +import {fetchVideosByTopic} from "../utils/api"; import {APIError} from "./errors"; import {setItem} from "../utils/safe-storage"; + export function getSearchResults(categoryKey, searchValue) { // get the stuff from the api // dispatch loaded @@ -12,12 +13,12 @@ export function getSearchResults(categoryKey, searchValue) { return dispatch => { dispatch(loadingAllResults()) // this calls the API - fetchAllTopics() - .then(topics => { - dispatch(loadedTopics(topics)); + fetchVideosByTopic(searchValue) + .then(videos => { + dispatch(loadedResults(videos)); }).catch(error => { - dispatch(APIError(error)); - }) + dispatch(APIError(error)); + }) }; case 'date': console.log('date') @@ -29,15 +30,14 @@ export function getSearchResults(categoryKey, searchValue) { } function loadingAllResults() { - + return { + type: START_LOADING_ALL_RESULTS + } } -function loadedTopics(topics) { - // cache - setItem('allTopics', JSON.stringify(topics)); - setItem('topicsUpdatedAt', new Date()); +function loadedResults(videos) { return { type: LOADED_ALL_RESULTS, - payload: topics + payload: videos }; }; \ No newline at end of file diff --git a/src/components/TopicsList.js b/src/components/TopicsList.js index 6ef1fca..afa1345 100644 --- a/src/components/TopicsList.js +++ b/src/components/TopicsList.js @@ -40,7 +40,7 @@ class TopicsList extends React.Component { {this.state.filteredTopics.map(topic => { return (
- +

{topic.name} ({topic.items} videos)

diff --git a/src/containers/Results.js b/src/containers/Results.js index c296c84..a21ed02 100644 --- a/src/containers/Results.js +++ b/src/containers/Results.js @@ -11,16 +11,18 @@ class Results extends React.Component { // get params from url const categoryKey = this.props.match.params.categoryKey; const searchValue = this.props.match.params.searchValue; - console.log(categoryKey) - console.log(searchValue) + + //FIXME: first check if current key / value / results is already in state + this.props.getSearchResults(categoryKey, searchValue); } render() { + console.log('results', this.props.searchResults); return ( - {this.props.searchResults.map(video => + {this.props.searchResults && this.props.searchResults.map(video => )} @@ -29,10 +31,10 @@ class Results extends React.Component { } const mapStateToProps = state => ({ - searchResults: this.state.results.currentItems, - currentKey: this.state.results.currentKey, - currentValue: this.state.results.currentValue, - currentQuery: this.state.results.queryParams + searchResults: state.results.currentItems, + currentKey: state.results.currentKey, + currentValue: state.results.currentValue, + currentQuery: state.results.queryParams }); const mapDispatchToProps = dispatch => ({ diff --git a/src/reducers/index.js b/src/reducers/index.js index 0ede7a6..2cbec00 100644 --- a/src/reducers/index.js +++ b/src/reducers/index.js @@ -9,5 +9,5 @@ export default (history) => combineReducers({ router: connectRouter(history), topics: TopicsReducer, videos: VideosReducer, - searchResults: SearchReducer, + results: SearchReducer, });