Skip to content

2129 prevent hitting return while search is loading #2426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions client/src/components/FoodSeeker/AddressDropDown.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ export default function AddressDropDown({ autoFocus }) {
searchCoordinates?.locationName || ""
);
const [open, setOpen] = useState(true);
const { mapboxResults, fetchMapboxResults } = useMapboxGeocoder();
const { mapboxResults, fetchMapboxResults, isLoading } = useMapboxGeocoder();
const dispatch = useAppDispatch();
const navigate = useNavigate();
const { flyTo } = useMapbox();
const [highlightedOption, setHighlightedOption] = useState(null);

const handleInputChange = (delta) => {
if (!delta) return;
Expand Down Expand Up @@ -121,16 +122,21 @@ export default function AddressDropDown({ autoFocus }) {
};

const handleKeyDown = (event) => {
if (event.key === "Enter" && mapboxResults.length > 0) {
if (event.key === "Enter" && mapboxResults.length > 0 && !isLoading) {
event.preventDefault();
handleAutocompleteOnChange(mapboxResults[0].place_name);
const selected = highlightedOption ?? mapboxResults[0].place_name;
handleAutocompleteOnChange(selected);
}
};
return (
<>
<Autocomplete
disableCloseOnSelect
autoHighlight
onInputChange={(value) => handleInputChange(value)}
onHighlightChange={(_event, option) => {
setHighlightedOption(option);
}}
freeSolo
inputValue={inputVal}
open={open}
Expand Down
2 changes: 0 additions & 2 deletions client/src/components/FoodSeeker/Home.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,6 @@ const Home = () => {
})}
>
<Box
component="form"
onSubmit={() => navigate("/organizations")}
sx={(theme) => ({
width: "100%",
marginTop: theme.spacing(1),
Expand Down
41 changes: 23 additions & 18 deletions client/src/hooks/useMapboxGeocoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
MAPBOX_ACCESS_TOKEN,
DEFAULT_VIEWPORTS,
} from "helpers/Constants";
import { useReducer } from "react";
import { useCallback, useReducer } from "react";

const baseUrl = `https://api.mapbox.com/geocoding/v5/mapbox.places`;

Expand Down Expand Up @@ -46,25 +46,30 @@ export function useMapboxGeocoder() {
initialState
);

const fetchMapboxResults = debounce(
async (searchString) => {
const debouncedFetch = useCallback(
debounce(
async (searchString) => {
const bbox = DEFAULT_VIEWPORTS[TENANT_ID].bbox;
const mapboxUrl = `${baseUrl}/${searchString}.json?bbox=${bbox}&access_token=${MAPBOX_ACCESS_TOKEN}`;

const bbox = DEFAULT_VIEWPORTS[TENANT_ID].bbox;
const mapboxUrl = `${baseUrl}/${searchString}.json?bbox=${bbox}&access_token=${MAPBOX_ACCESS_TOKEN}`;

dispatch({ type: actionTypes.FETCH_REQUEST });
try {
const response = await axios.get(mapboxUrl);
dispatch({
type: actionTypes.FETCH_SUCCESS,
results: response.data.features,
});
} catch (error) {
dispatch({ type: actionTypes.FETCH_FAILURE, error });
}
},
{ wait: 300, after: false, before: true }
try {
const response = await axios.get(mapboxUrl);
dispatch({
type: actionTypes.FETCH_SUCCESS,
results: response.data.features,
});
} catch (error) {
dispatch({ type: actionTypes.FETCH_FAILURE, error });
}
},
{ wait: 300 }
),
[]
);
const fetchMapboxResults = useCallback((searchString) => {
dispatch({ type: actionTypes.FETCH_REQUEST });
debouncedFetch(searchString);
}, []);

return { error, isLoading, mapboxResults, fetchMapboxResults };
}
Loading