@@ -12,6 +12,7 @@ import * as analytics from "services/analytics";
12
12
import {
13
13
useAppDispatch ,
14
14
useSearchCoordinates ,
15
+ useStakeholders ,
15
16
useUserCoordinates ,
16
17
useWidget ,
17
18
} from "../../appReducer" ;
@@ -24,13 +25,13 @@ export default function AddressDropDown({ autoFocus }) {
24
25
const [ inputVal , setInputVal ] = useState (
25
26
searchCoordinates ?. locationName || ""
26
27
) ;
27
- const [ open , setOpen ] = useState ( true ) ;
28
+ const [ open , setOpen ] = useState ( false ) ;
28
29
const { mapboxResults, fetchMapboxResults, isLoading } = useMapboxGeocoder ( ) ;
29
30
const dispatch = useAppDispatch ( ) ;
30
31
const navigate = useNavigate ( ) ;
31
32
const { flyTo } = useMapbox ( ) ;
32
33
const [ highlightedOption , setHighlightedOption ] = useState ( null ) ;
33
-
34
+ const stakeholders = useStakeholders ( ) ;
34
35
useEffect ( ( ) => {
35
36
if ( userCoordinates ) {
36
37
setInputVal ( "" ) ;
@@ -50,8 +51,27 @@ export default function AddressDropDown({ autoFocus }) {
50
51
const result = mapboxResults . find (
51
52
( item ) => item . place_name === selectedResult
52
53
) ;
54
+ const stakeholderResult = stakeholders . find (
55
+ ( stakeholder ) => getNameAndAddress ( stakeholder ) === selectedResult
56
+ ) ;
53
57
setOpen ( false ) ;
54
58
setInputVal ( selectedResult ) ;
59
+ if ( stakeholderResult ) {
60
+ const { longitude, latitude } = stakeholderResult ;
61
+
62
+ flyTo ( {
63
+ latitude,
64
+ longitude,
65
+ } ) ;
66
+ dispatch ( {
67
+ type : "SEARCH_COORDINATES_UPDATED" ,
68
+ coordinates : { latitude, longitude, locationName : selectedResult } ,
69
+ } ) ;
70
+ navigate ( isWidget ? "/widget" : "/organizations" ) ;
71
+
72
+ analytics . postEvent ( "changeOrigin" , { } ) ;
73
+ }
74
+
55
75
if ( result ) {
56
76
const [ longitude , latitude ] = result . center ;
57
77
@@ -77,7 +97,7 @@ export default function AddressDropDown({ autoFocus }) {
77
97
} }
78
98
variant = "outlined"
79
99
{ ...params }
80
- label = "Search by address or zip code"
100
+ label = "Search by name, address, or zip code"
81
101
margin = "none"
82
102
fullWidth
83
103
name = "address"
@@ -117,11 +137,11 @@ export default function AddressDropDown({ autoFocus }) {
117
137
} ;
118
138
119
139
const renderOption = ( props , option ) => {
120
- const { key } = props ;
140
+ const { id } = props ;
121
141
return (
122
142
< MenuItem
123
143
{ ...props }
124
- key = { key }
144
+ key = { id }
125
145
component = "div"
126
146
onClick = { ( ) => handleAutocompleteOnChange ( option ) }
127
147
>
@@ -155,7 +175,18 @@ export default function AddressDropDown({ autoFocus }) {
155
175
onChange = { ( _event , newValue ) => {
156
176
setInputVal ( newValue ?? "" ) ;
157
177
} }
158
- options = { mapboxResults . slice ( 0 , 10 ) . map ( ( item ) => item . place_name ) }
178
+ options = { [
179
+ ...stakeholders ?. map ( ( stakeholder ) => getNameAndAddress ( stakeholder ) ) ,
180
+ ...mapboxResults . slice ( 0 , 10 ) . map ( ( item ) => item . place_name ) ,
181
+ ] }
182
+ filterOptions = { ( options , { inputValue } ) => {
183
+ return options . filter ( ( option ) =>
184
+ inputValue
185
+ . toLowerCase ( )
186
+ . split ( " " )
187
+ . every ( ( word ) => option . toLowerCase ( ) . includes ( word ) )
188
+ ) ;
189
+ } }
159
190
sx = { {
160
191
width : 600 ,
161
192
backgroundColor : "#F0F0F0" ,
@@ -178,9 +209,23 @@ export default function AddressDropDown({ autoFocus }) {
178
209
} ,
179
210
} ,
180
211
} }
212
+ ListboxProps = { {
213
+ style : {
214
+ maxHeight : "160px" ,
215
+ } ,
216
+ } }
181
217
renderInput = { ( params ) => renderInput ( params ) }
182
218
renderOption = { ( props , option ) => renderOption ( props , option ) }
183
219
/>
184
220
</ >
185
221
) ;
186
222
}
223
+
224
+ function getNameAndAddress ( stakeholder ) {
225
+ return (
226
+ ( stakeholder . name ?? "" ) +
227
+ ( stakeholder . address1 ? `, ${ stakeholder . address1 } ` : "" ) +
228
+ ( stakeholder . city ? `, ${ stakeholder . city } ` : "" ) +
229
+ ( stakeholder . zip ? `, ${ stakeholder . zip } ` : "" )
230
+ ) ;
231
+ }
0 commit comments