@@ -12,6 +12,7 @@ import * as analytics from "services/analytics";
1212import {
1313 useAppDispatch ,
1414 useSearchCoordinates ,
15+ useStakeholders ,
1516 useUserCoordinates ,
1617 useWidget ,
1718} from "../../appReducer" ;
@@ -24,13 +25,13 @@ export default function AddressDropDown({ autoFocus }) {
2425 const [ inputVal , setInputVal ] = useState (
2526 searchCoordinates ?. locationName || ""
2627 ) ;
27- const [ open , setOpen ] = useState ( true ) ;
28+ const [ open , setOpen ] = useState ( false ) ;
2829 const { mapboxResults, fetchMapboxResults, isLoading } = useMapboxGeocoder ( ) ;
2930 const dispatch = useAppDispatch ( ) ;
3031 const navigate = useNavigate ( ) ;
3132 const { flyTo } = useMapbox ( ) ;
3233 const [ highlightedOption , setHighlightedOption ] = useState ( null ) ;
33-
34+ const stakeholders = useStakeholders ( ) ;
3435 useEffect ( ( ) => {
3536 if ( userCoordinates ) {
3637 setInputVal ( "" ) ;
@@ -50,8 +51,27 @@ export default function AddressDropDown({ autoFocus }) {
5051 const result = mapboxResults . find (
5152 ( item ) => item . place_name === selectedResult
5253 ) ;
54+ const stakeholderResult = stakeholders . find (
55+ ( stakeholder ) => getNameAndAddress ( stakeholder ) === selectedResult
56+ ) ;
5357 setOpen ( false ) ;
5458 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+
5575 if ( result ) {
5676 const [ longitude , latitude ] = result . center ;
5777
@@ -77,7 +97,7 @@ export default function AddressDropDown({ autoFocus }) {
7797 } }
7898 variant = "outlined"
7999 { ...params }
80- label = "Search by address or zip code"
100+ label = "Search by name, address, or zip code"
81101 margin = "none"
82102 fullWidth
83103 name = "address"
@@ -117,11 +137,11 @@ export default function AddressDropDown({ autoFocus }) {
117137 } ;
118138
119139 const renderOption = ( props , option ) => {
120- const { key } = props ;
140+ const { id } = props ;
121141 return (
122142 < MenuItem
123143 { ...props }
124- key = { key }
144+ key = { id }
125145 component = "div"
126146 onClick = { ( ) => handleAutocompleteOnChange ( option ) }
127147 >
@@ -155,7 +175,18 @@ export default function AddressDropDown({ autoFocus }) {
155175 onChange = { ( _event , newValue ) => {
156176 setInputVal ( newValue ?? "" ) ;
157177 } }
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+ } }
159190 sx = { {
160191 width : 600 ,
161192 backgroundColor : "#F0F0F0" ,
@@ -178,9 +209,23 @@ export default function AddressDropDown({ autoFocus }) {
178209 } ,
179210 } ,
180211 } }
212+ ListboxProps = { {
213+ style : {
214+ maxHeight : "160px" ,
215+ } ,
216+ } }
181217 renderInput = { ( params ) => renderInput ( params ) }
182218 renderOption = { ( props , option ) => renderOption ( props , option ) }
183219 />
184220 </ >
185221 ) ;
186222}
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