Skip to content

Commit c90ac09

Browse files
authored
1115 fix abbreviation issue on search bug introduced in pr 2484 (#2524)
1 parent c0a1586 commit c90ac09

1 file changed

Lines changed: 59 additions & 31 deletions

File tree

client/src/components/FoodSeeker/AddressDropDown.jsx

Lines changed: 59 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -48,45 +48,51 @@ export default function AddressDropDown({ autoFocus }) {
4848
};
4949

5050
const handleAutocompleteOnChange = (selectedResult) => {
51-
const result = mapboxResults.find(
52-
(item) => item.place_name === selectedResult
53-
);
54-
const stakeholderResult = stakeholders.find(
55-
(stakeholder) => getNameAndAddress(stakeholder) === selectedResult
56-
);
57-
setOpen(false);
58-
setInputVal(selectedResult);
59-
if (stakeholderResult) {
60-
const { longitude, latitude } = stakeholderResult;
51+
let result;
52+
if (selectedResult.type === "mapbox") {
53+
result = selectedResult.value.place_name;
54+
const [longitude, latitude] = selectedResult.value.center;
6155

6256
flyTo({
6357
latitude,
6458
longitude,
6559
});
60+
6661
dispatch({
6762
type: "SEARCH_COORDINATES_UPDATED",
68-
coordinates: { latitude, longitude, locationName: selectedResult },
63+
coordinates: {
64+
latitude,
65+
longitude,
66+
locationName: selectedResult.value.place_name,
67+
},
6968
});
69+
7070
navigate(isWidget ? "/widget" : "/organizations");
7171

7272
analytics.postEvent("changeOrigin", {});
73-
}
74-
75-
if (result) {
76-
const [longitude, latitude] = result.center;
73+
} else if (selectedResult.type === "stakeholder") {
74+
result = getNameAndAddress(selectedResult.value);
75+
const { longitude, latitude } = selectedResult.value;
7776

7877
flyTo({
7978
latitude,
8079
longitude,
8180
});
8281
dispatch({
8382
type: "SEARCH_COORDINATES_UPDATED",
84-
coordinates: { latitude, longitude, locationName: result.place_name },
83+
coordinates: {
84+
latitude,
85+
longitude,
86+
locationName: getNameAndAddress(selectedResult.value),
87+
},
8588
});
8689
navigate(isWidget ? "/widget" : "/organizations");
8790

8891
analytics.postEvent("changeOrigin", {});
89-
}
92+
} else return;
93+
94+
setOpen(false);
95+
setInputVal(result);
9096
};
9197

9298
const renderInput = (params) => {
@@ -145,18 +151,30 @@ export default function AddressDropDown({ autoFocus }) {
145151
component="div"
146152
onClick={() => handleAutocompleteOnChange(option)}
147153
>
148-
{option}
154+
{getOptionLabel(option)}
149155
</MenuItem>
150156
);
151157
};
152158

159+
const stakeholderOptions = stakeholders?.map((stakeholder) => ({
160+
type: "stakeholder",
161+
value: stakeholder,
162+
}));
163+
const mapboxOptions = mapboxResults.slice(0, 10).map((result) => ({
164+
type: "mapbox",
165+
value: result,
166+
}));
167+
153168
const handleKeyDown = (event) => {
154-
if (event.key === "Enter" && mapboxResults.length > 0 && !isLoading) {
169+
if (event.key === "Enter" && mapboxOptions.length > 0 && !isLoading) {
155170
event.preventDefault();
156-
const selected = highlightedOption ?? mapboxResults[0].place_name;
171+
const selected = highlightedOption ?? mapboxOptions[0];
157172
handleAutocompleteOnChange(selected);
158173
}
159174
};
175+
176+
const combinedOptions = [...stakeholderOptions, ...mapboxOptions];
177+
160178
return (
161179
<>
162180
<Autocomplete
@@ -173,19 +191,22 @@ export default function AddressDropDown({ autoFocus }) {
173191
onClose={() => setOpen(false)}
174192
onKeyDown={handleKeyDown}
175193
onChange={(_event, newValue) => {
176-
setInputVal(newValue ?? "");
194+
setInputVal(newValue ? getOptionLabel(newValue) : "");
177195
}}
178-
options={[
179-
...stakeholders?.map((stakeholder) => getNameAndAddress(stakeholder)),
180-
...mapboxResults.slice(0, 10).map((item) => item.place_name),
181-
]}
196+
options={combinedOptions}
197+
getOptionLabel={getOptionLabel}
182198
filterOptions={(options, { inputValue }) => {
183-
return options.filter((option) =>
184-
inputValue
185-
.toLowerCase()
186-
.split(" ")
187-
.every((word) => option.toLowerCase().includes(word))
188-
);
199+
return options.filter((option) => {
200+
if (option.type === "stakeholder") {
201+
return inputValue
202+
.toLowerCase()
203+
.split(" ")
204+
.every((word) =>
205+
getNameAndAddress(option.value).toLowerCase().includes(word)
206+
);
207+
}
208+
return true; // For mapbox results, we don't filter by inputValue
209+
});
189210
}}
190211
sx={{
191212
width: 600,
@@ -229,3 +250,10 @@ function getNameAndAddress(stakeholder) {
229250
(stakeholder.zip ? `, ${stakeholder.zip}` : "")
230251
);
231252
}
253+
254+
export function getOptionLabel(option) {
255+
if (option.type === "stakeholder") {
256+
return getNameAndAddress(option.value);
257+
}
258+
return option.value.place_name;
259+
}

0 commit comments

Comments
 (0)