From 95bc850b9db986b9f462f19d7801218027307d58 Mon Sep 17 00:00:00 2001 From: ankit-thanekar007 Date: Wed, 3 Mar 2021 12:34:33 -0800 Subject: TMA-663-Search Integration --- src/components/search/ExploreSection.tsx | 2 +- src/components/search/SearchResultCell.tsx | 12 ++-- src/components/search/SearchResultList.tsx | 32 +++++---- src/screens/search/SearchScreen.tsx | 107 ++++++++++++----------------- 4 files changed, 70 insertions(+), 83 deletions(-) (limited to 'src') diff --git a/src/components/search/ExploreSection.tsx b/src/components/search/ExploreSection.tsx index 025c8c3c..e888370e 100644 --- a/src/components/search/ExploreSection.tsx +++ b/src/components/search/ExploreSection.tsx @@ -14,7 +14,7 @@ interface ExploreSectionProps { users: ProfilePreviewType[]; } const ExploreSection: React.FC = ({title, users}) => { - return users.length !== 0 ? ( + return users && users.length !== 0 ? ( {title} = (showBorder: Boolean) => { }; const SearchResultList: React.FC = ({results}) => { + const [showSection, setShowSection] = useState(true); return ( - item + index} - renderItem={({item}) => } - renderSectionHeader={({section: {title}}) => - sectionHeader(title !== 'categories') - } - /> + + item + index} + renderItem={({item}) => } + renderSectionHeader={({section: {title, data}}) => { + if (title === 'categories' && data.length === 0) { + setShowSection(false); + } + return sectionHeader(title !== 'categories' && showSection); + }} + /> + + ); }; const styles = StyleSheet.create({ - container: {flex: 1, marginTop: SCREEN_HEIGHT * 0.02}, + container: {marginTop: SCREEN_HEIGHT * 0.02}, sectionHeaderStyle: { width: '100%', height: 0.5, diff --git a/src/screens/search/SearchScreen.tsx b/src/screens/search/SearchScreen.tsx index 39b0425d..a66a2cbc 100644 --- a/src/screens/search/SearchScreen.tsx +++ b/src/screens/search/SearchScreen.tsx @@ -16,21 +16,15 @@ import { SearchBackground, SearchBar, SearchHeader, - SearchResults, - SearchResultsBackground, SearchResultList, + SearchResultsBackground, TabsGradient, } from '../../components'; -import { - SEARCH_ENDPOINT, - SEARCH_V2_ENDPOINT, - TAGG_LIGHT_BLUE, -} from '../../constants'; +import {SEARCH_V2_ENDPOINT, TAGG_LIGHT_BLUE} from '../../constants'; import {loadRecentlySearched, resetScreenType} from '../../store/actions'; import {RootState} from '../../store/rootReducer'; import {ProfilePreviewType, ScreenType, UserType} from '../../types'; import {SCREEN_HEIGHT, SCREEN_WIDTH, StatusBarHeight} from '../../utils'; -import MockResults from './mock'; const NO_USER: UserType = { userId: '', username: '', @@ -69,62 +63,45 @@ const SearchScreen: React.FC = () => { setResults([]); return; } - // const loadResults = async (q: string) => { - // try { - // const token = await AsyncStorage.getItem('token'); - // const response = await fetch(`${SEARCH_V2_ENDPOINT}?query=${q}`, { - // method: 'GET', - // headers: { - // Authorization: 'Token ' + token, - // }, - // }); - // const {status} = response; - // if (status === 200) { - // const searchResults = await response.json(); - // const sanitizedResult = [ - // { - // title: 'categories', - // data: searchResults.categories, - // }, - // { - // title: 'users', - // data: searchResults.users, - // }, - // ]; - // setResults(sanitizedResult); - // } else { - // const searchResults = MockResults(); - // const sanitizedResult = [ - // { - // title: 'categories', - // data: searchResults.categories, - // }, - // { - // title: 'users', - // data: searchResults.users, - // }, - // ]; - // setResults(sanitizedResult); - // } - // setResults([]); - // } catch (error) { - // console.log(error); - // setResults([]); - // } - // }; - const searchResults = MockResults(); - const sanitizedResult = [ - { - title: 'categories', - data: searchResults.categories, - }, - { - title: 'users', - data: searchResults.users, - }, - ]; - setResults(sanitizedResult); - // loadResults(query); + const loadResults = async (q: string) => { + try { + const token = await AsyncStorage.getItem('token'); + const response = await fetch(`${SEARCH_V2_ENDPOINT}?query=${q}`, { + method: 'GET', + headers: { + Authorization: 'Token ' + token, + }, + }); + const {status} = response; + if (status === 200) { + const searchResults = await response.json(); + const sanitizedResult = [ + { + title: 'categories', + data: searchResults.categories, + }, + { + title: 'users', + data: searchResults.users, + }, + ]; + + if ( + searchResults.categories.length !== 0 || + searchResults.users.length !== 0 + ) { + if (query.length > 3) { + setResults(sanitizedResult); + return; + } + } + } + } catch (error) { + console.log(error); + } + setResults([]); + }; + loadResults(query); }, [query]); /** @@ -200,6 +177,7 @@ const SearchScreen: React.FC = () => { {...{top, searching}} /> + {results && results.length === 0 ? ( { )} + ); -- cgit v1.2.3-70-g09d2