Skip to content

Commit

Permalink
Merge branch '187001114-store-location-from-map' into 187000993-stati…
Browse files Browse the repository at this point in the history
…on-selection-bug-fix
  • Loading branch information
eireland committed Feb 8, 2024
2 parents 6727040 + acf6a96 commit edbcc8b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
33 changes: 22 additions & 11 deletions src/components/location-picker.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useEffect, useRef, useState } from "react";
import classnames from "classnames";
import { autoComplete, geoLocSearch } from "../utils/geonameSearch";
import { autoComplete, geoLocSearch, geoNameSearch } from "../utils/geonameSearch";
import { geonamesUser, kOffsetMap, timezoneServiceURL } from "../constants";
import { useStateContext } from "../hooks/use-state";
import { IPlace, IStation, IWeatherStation, IStatus } from "../types";
Expand Down Expand Up @@ -57,7 +57,7 @@ export const LocationPicker = ({setActiveStations, setStatus}: IProps) => {
setShowSelectionList(false);
setShowMapButton(false);
setState((draft) => {
draft.location = undefined;
draft.location = locationInputEl.current?.value === "" ? undefined : location;
draft.zoomMap = false;
});
}
Expand All @@ -71,7 +71,7 @@ export const LocationPicker = ({setActiveStations, setStatus}: IProps) => {
document.removeEventListener("mousedown", handleClickOutside);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
}, [location]);

useEffect(() => {
if (locationInputEl.current?.value === "") {
Expand Down Expand Up @@ -156,9 +156,10 @@ export const LocationPicker = ({setActiveStations, setStatus}: IProps) => {
}
};

const getLocationList = () => {
if (locationInputEl.current) {
autoComplete(locationInputEl.current)
const getLocationList = (newLoc?: string) => {
const locationToUse = newLoc || locationInputEl.current?.value;
if (locationToUse) {
autoComplete(locationToUse)
.then ((placeList: IPlace[] | undefined) => {
if (placeList) {
setLocationPossibilities(placeList);
Expand All @@ -168,10 +169,12 @@ export const LocationPicker = ({setActiveStations, setStatus}: IProps) => {
};

useEffect(() => {
if (isEditing) {
getLocationList();
if (isEditing || location) { //if location changes from map selection
const newLocation = location ? location.name : "";
setCandidateLocation(newLocation);
getLocationList(newLocation);
}
}, [isEditing]);
}, [isEditing, location]);

const placeNameSelected = (place: IPlace | undefined) => {
setState(draft => {
Expand All @@ -182,13 +185,20 @@ export const LocationPicker = ({setActiveStations, setStatus}: IProps) => {
setCandidateLocation(place?.name || "");
setShowSelectionList(false);
setIsEditing(false);
setShowMapButton(true);
setShowMapButton(place?.name !== undefined);
setLocationPossibilities([]);
setHoveredIndex(null);
setArrowedIndex(-1);
};

const handleInputKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
const handleInputKeyDown = async(e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.key === "Enter") {
if (e.currentTarget.value) {
const locale = await geoNameSearch(e.currentTarget.value);
placeNameSelected(locale?.[0]);
}
setIsEditing(false);
} else
if (e.key === "ArrowDown" && locationPossibilities.length > 0) {
setHoveredIndex(0);
setArrowedIndex(0);
Expand Down Expand Up @@ -234,6 +244,7 @@ export const LocationPicker = ({setActiveStations, setStatus}: IProps) => {
if (target.value !== "") {
getLocationList();
setCandidateLocation(target.value);
setState(draft => {draft.location = location;});
} else {
setIsEditing(false);
setCandidateLocation(location?.name || "");
Expand Down
3 changes: 1 addition & 2 deletions src/utils/geonameSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ export const geoNameSearch = async (searchString: string, maxRows?: number): Pro
}
};

export const autoComplete = async(inputEl: HTMLInputElement) => {
let thisQuery = inputEl.value;
export const autoComplete = async(thisQuery: string) => {
try {
let placeList = await geoNameSearch(thisQuery);
placeList = placeList || [];
Expand Down

0 comments on commit edbcc8b

Please sign in to comment.