1
- import React , { useRef , useState } from 'react' ;
1
+ import React , { useState } from 'react' ;
2
2
import TextField from '@material-ui/core/TextField' ;
3
3
import Autocomplete from '@material-ui/lab/Autocomplete' ;
4
4
import CloseIcon from '@material-ui/icons/Close' ;
@@ -7,8 +7,10 @@ import {Grid} from "@material-ui/core";
7
7
import SearchIcon from '@material-ui/icons/Search' ;
8
8
import log from 'loglevel' ;
9
9
import { connect , useSelector } from "react-redux" ;
10
- import { getSearchSuggestions } from "../../../actions" ;
10
+ import { getSearchSuggestions , getDataViaAPI } from "../../../actions" ;
11
11
import { makeStyles } from "@material-ui/core/styles" ;
12
+ import { LOAD_FILTER_PRODUCTS } from "../../../actions/types" ;
13
+ import { PRODUCT_BY_CATEGORY_DATA_API } from "../../../constants/api_routes" ;
12
14
13
15
export const useSearchBarStyles = makeStyles ( ( ) => ( {
14
16
paper : {
@@ -23,17 +25,25 @@ function SearchBar(props) {
23
25
const [ value , setValue ] = useState ( null ) ;
24
26
const searchSuggestions = useSelector ( state => state . searchKeywordReducer )
25
27
const classes = useSearchBarStyles ( )
26
- const textFieldRef = useRef ( null )
28
+ let selectedValue = null
27
29
28
30
const handleClose = ( event , reason ) => {
29
31
if ( props . handleClose ) {
30
32
props . handleClose ( ) ;
31
33
}
32
34
33
35
// search is selected
34
- if ( reason === "select-option" ) {
36
+ if ( reason === "select-option" && selectedValue != null ) {
35
37
log . info ( "Search is selected.... value = "
36
- + event . target . getAttributeNames ( ) )
38
+ + selectedValue )
39
+ for ( let index = 0 ; index < searchSuggestions . data . length ; ++ index ) {
40
+ if ( searchSuggestions . data [ index ] . keyword . length === selectedValue . length
41
+ && searchSuggestions . data [ index ] . keyword . localeCompare ( selectedValue ) === 0 ) {
42
+ props . getDataViaAPI ( LOAD_FILTER_PRODUCTS ,
43
+ PRODUCT_BY_CATEGORY_DATA_API + searchSuggestions . data [ index ] . link )
44
+ return
45
+ }
46
+ }
37
47
}
38
48
}
39
49
@@ -58,6 +68,7 @@ function SearchBar(props) {
58
68
}
59
69
60
70
const handleInputChange = ( event , newValue ) => {
71
+ selectedValue = newValue
61
72
props . getSearchSuggestions ( newValue )
62
73
}
63
74
@@ -87,8 +98,6 @@ function SearchBar(props) {
87
98
selectOnFocus
88
99
clearOnBlur
89
100
handleHomeEndKeys
90
- ref = { textFieldRef }
91
- open
92
101
closeIcon = { < CloseIcon /> }
93
102
id = "free-solo"
94
103
options = { searchSuggestions . data }
@@ -117,4 +126,4 @@ function SearchBar(props) {
117
126
) ;
118
127
}
119
128
120
- export default connect ( null , { getSearchSuggestions} ) ( SearchBar ) ;
129
+ export default connect ( null , { getSearchSuggestions, getDataViaAPI } ) ( SearchBar ) ;
0 commit comments