Skip to content

Commit

Permalink
Merge pull request #67 from concord-consortium/187000993-station-sele…
Browse files Browse the repository at this point in the history
…ction-bug-fix

187000993 station selection bug fix
  • Loading branch information
eireland authored Feb 9, 2024
2 parents 6f96c77 + 37fe9c4 commit aab06b7
Showing 1 changed file with 14 additions and 31 deletions.
45 changes: 14 additions & 31 deletions src/components/location-picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ export const LocationPicker = ({setActiveStations, setStatus}: IProps) => {
if (stationList) {
setActiveStations(stationList);
setStationPossibilities(stationList.slice(0, 5));
(isEditing && stationList.length > 0) && setShowSelectionList(true);
}
if (stationList.length > 0 && !didUserSelectStationFromMap) {
setState((draft) => {
Expand All @@ -123,7 +122,7 @@ export const LocationPicker = ({setActiveStations, setStatus}: IProps) => {
});
}
// eslint-disable-next-line react-hooks/exhaustive-deps
},[endDate, isEditing, location, startDate, didUserSelectStationFromMap]);
},[endDate, location, startDate]);

useEffect(() => {
if (showStationSelectionList) {
Expand Down Expand Up @@ -291,52 +290,36 @@ export const LocationPicker = ({setActiveStations, setStatus}: IProps) => {
};

// Station selection functions
const stationSelected = (station: IWeatherStation | undefined, distance: number) => {
setState(draft => {
draft.weatherStation = station;
draft.weatherStationDistance = distance;
draft.didUserSelectStationFromMap = false;
draft.zoomMap = false;
});
setShowStationSelectionList(false);
setStationHoveredIndex(null);
setArrowedIndex(-1);
};

const handleStationSelection = (ev: React.MouseEvent<HTMLLIElement>) => {
const target = ev.currentTarget;
if (target.dataset.ix !== undefined) {
const selectedLocIdx = parseInt(target.dataset.ix, 10);
if (selectedLocIdx >= 0) {
const selectedStation = stationPossibilities[selectedLocIdx].station;
stationSelected(selectedStation);
setState(draft => {
draft.didUserSelectStationFromMap = false;
draft.weatherStation = selectedStation;
draft.weatherStationDistance = stationPossibilities[selectedLocIdx].distance;
});
stationSelected(selectedStation, stationPossibilities[selectedLocIdx].distance);
}
setShowStationSelectionList(false);
setState(draft => {
draft.zoomMap = false;
});
}
};

const handleStationSelectionKeyDown = (e: React.KeyboardEvent<HTMLLIElement>, index: number) => {
if (e.key === "Enter") {
const selectedStation = stationPossibilities[index].station;
stationSelected(selectedStation);
setState(draft => {
draft.weatherStation = selectedStation;
draft.weatherStationDistance = stationPossibilities[index].distance;
});
setShowStationSelectionList(false);
setState(draft => {
draft.zoomMap = false;
});
stationSelected(selectedStation, stationPossibilities[index].distance);
}
};

const stationSelected = (station: IWeatherStation | undefined) => {
setState(draft => {
draft.weatherStation = station;
draft.didUserSelectStationFromMap = false;
});
setShowStationSelectionList(false);
setStationHoveredIndex(null);
setArrowedIndex(-1);
};

const handleStationHover = (index: number | null) => {
setStationHoveredIndex(index);
};
Expand Down

0 comments on commit aab06b7

Please sign in to comment.