1- import React , { useState } from 'react' ;
1+ import React , { useEffect , useState } from 'react' ;
22import TextField from '@material-ui/core/TextField' ;
33import Autocomplete from '@material-ui/lab/Autocomplete' ;
44import CloseIcon from '@material-ui/icons/Close' ;
@@ -11,66 +11,75 @@ import {getSearchSuggestions, getDataViaAPI} from "../../../actions";
1111import { makeStyles } from "@material-ui/core/styles" ;
1212import { LOAD_FILTER_PRODUCTS } from "../../../actions/types" ;
1313import { PRODUCT_BY_CATEGORY_DATA_API } from "../../../constants/api_routes" ;
14+ import { Loader } from "semantic-ui-react" ;
15+ import { StyledSearchBarDimmer } from "../../../styles/semanticUI/customStyles" ;
1416
15- export const useSearchBarStyles = makeStyles ( ( ) => ( {
17+ export const useSearchBarStyles = makeStyles ( ( theme ) => ( {
1618 paper : {
17- height : 300
19+ height : 250
1820 } ,
1921 listbox : {
20- maxHeight : 340
22+ maxHeight : 290 ,
23+ } ,
24+ option : {
25+ [ theme . breakpoints . down ( "xs" ) ] : {
26+ paddingLeft : 40
27+ } ,
2128 }
2229} ) ) ;
2330
2431function SearchBar ( props ) {
2532 const [ value , setValue ] = useState ( null ) ;
2633 const searchSuggestions = useSelector ( state => state . searchKeywordReducer )
34+ const filterProductsReducer = useSelector ( state => state . filterProductsReducer )
2735 const classes = useSearchBarStyles ( )
36+ const [ isLoading , setIsLoading ] = useState ( false )
37+ let selectedValue = null
38+
39+ useEffect ( ( ) => {
40+ log . info ( `[SearchBar] Component did mount` )
41+ setIsLoading ( false )
42+ } , [ filterProductsReducer ] )
2843
29- const getSelectedValue = ( ) => {
44+ const getSearchKeyword = ( ) => {
3045 return document . querySelector ( 'input[id="free-solo"]' ) . value
3146 }
3247
33- const searchKeyword = ( ) => {
34- const selectedValue = getSelectedValue ( )
35- if ( selectedValue && ! selectedValue . isEmpty ) {
48+ const searchKeyword = ( value ) => {
49+ if ( value && ! value . isEmpty ) {
3650 let queryLink = null
3751 for ( let index = 0 ; index < searchSuggestions . data . length ; ++ index ) {
38- if ( searchSuggestions . data [ index ] . keyword . length === selectedValue . length
39- && searchSuggestions . data [ index ] . keyword . localeCompare ( selectedValue ) === 0 ) {
52+ if ( searchSuggestions . data [ index ] . keyword . length === value . length
53+ && searchSuggestions . data [ index ] . keyword . localeCompare ( value ) === 0 ) {
4054
4155 // complete match
4256 queryLink = searchSuggestions . data [ index ] . link
4357 break ;
44- }
45- if ( searchSuggestions . data [ index ] . keyword . length > selectedValue . length ) {
46- // just stop finding if length exceeds.
47- if ( ! queryLink ) {
48- // then match whatever we have.
49- queryLink = searchSuggestions . data [ index ] . link
50- }
51- break ;
5258 } else {
53- // closest match
54- log . info ( `` )
5559 queryLink = searchSuggestions . data [ index ] . link
5660 }
5761 }
58- props . getDataViaAPI ( LOAD_FILTER_PRODUCTS ,
59- PRODUCT_BY_CATEGORY_DATA_API + queryLink )
62+
63+ log . info ( `queryLink = ${ queryLink } , value = ${ value } ` )
64+ if ( queryLink ) {
65+ setIsLoading ( true )
66+ props . getDataViaAPI ( LOAD_FILTER_PRODUCTS ,
67+ PRODUCT_BY_CATEGORY_DATA_API + queryLink )
68+ }
6069 }
6170 }
6271
63- const handleClose = ( event , reason ) => {
64- setValue ( '' )
65-
66- // search is selected
67- if ( reason === "select-option" ) {
68- searchKeyword ( )
72+ const handleClose = ( ) => {
73+ log . info ( `selectedValue------2 ===== ${ selectedValue } ` )
74+ let finalSelectedValue = selectedValue
75+ if ( ! selectedValue ) {
76+ finalSelectedValue = getSearchKeyword ( )
6977 }
78+ searchKeyword ( finalSelectedValue )
7079 }
7180
7281 const onSearchBtnClick = ( ) => {
73- searchKeyword ( )
82+ searchKeyword ( getSearchKeyword ( ) )
7483 props . handleClose ( )
7584 }
7685
@@ -88,17 +97,17 @@ function SearchBar(props) {
8897 InputProps = { {
8998 ...params . InputProps ,
9099 startAdornment : < ArrowBackIcon onClick = { props . handleClose } fontSize = "large" /> ,
91- endAdornment : < SearchIcon onClick = { onSearchBtnClick } fontSize = "large" />
92100 } }
93101 />
94102 )
95103 }
96104
97105 const handleInputChange = ( event , newValue ) => {
106+ selectedValue = newValue
98107 props . getSearchSuggestions ( newValue )
99108 }
100109
101- log . info ( " [Search Bar] Rendering search bar...." )
110+ log . info ( ` [Search Bar] Rendering search bar....` )
102111
103112 return (
104113 < Grid container alignItems = "center" >
@@ -144,10 +153,15 @@ function SearchBar(props) {
144153 fullWidth
145154 onClose = { handleClose }
146155 size = { props . size }
147- classes = { { paper : classes . paper , listbox : classes . listbox } }
156+ classes = { { paper : classes . paper , listbox : classes . listbox , option : classes . option } }
148157 renderInput = { ( params ) =>
149158 props . device ? renderMobileTextField ( params ) : renderDesktopTextField ( params ) }
150159 />
160+ { isLoading ?
161+ < StyledSearchBarDimmer active inverted >
162+ < Loader inverted > Loading</ Loader >
163+ </ StyledSearchBarDimmer > : null }
164+
151165 </ Grid >
152166 ) ;
153167}
0 commit comments