Skip to content

Commit

Permalink
Merge pull request #45 from concord-consortium/186866310-station-dist…
Browse files Browse the repository at this point in the history
…ance-fix

186866310 station distance fix
  • Loading branch information
lublagg authored Feb 1, 2024
2 parents 9269c0f + 87b4721 commit fdd6e44
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
9 changes: 6 additions & 3 deletions src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,15 @@ export const App = () => {
if (myCase) {
const station = myCase.values;
const {latitude, longitude} = station;
const locationName = await geoLocSearch(latitude, longitude);
const locationInfo = await geoLocSearch(latitude, longitude);
const locale = `${locationInfo.split(",")[0]}, ${locationInfo.split(",")[1]}`;
const distance = Number(locationInfo.split(",")[2]);
setState((draft) => {
draft.weatherStation = station;
draft.location = {name: locationName, latitude, longitude};
draft.weatherStationDistance = 0;
draft.location = {name: locale, latitude, longitude};
draft.weatherStationDistance = distance;
draft.zoomMap = false;
draft.didUserSelectStationFromMap = true;
});
}
}
Expand Down
11 changes: 8 additions & 3 deletions src/components/location-picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import "./location-picker.scss";

export const LocationPicker = () => {
const {state, setState} = useStateContext();
const {units, location, weatherStation, weatherStationDistance, startDate, endDate} = state;
const {units, location, weatherStation, weatherStationDistance, startDate, endDate, didUserSelectStationFromMap} = state;
const [showMapButton, setShowMapButton] = useState(false);
const [isEditing, setIsEditing] = useState(false);
const [locationPossibilities, setLocationPossibilities] = useState<IPlace[]>([]);
Expand Down Expand Up @@ -71,7 +71,7 @@ export const LocationPicker = () => {
useEffect(() => {
const _startDate = startDate ? startDate : new Date( -5364662060); // 1/1/1750
const _endDate = endDate ? endDate : new Date(Date.now());
if (location) {
if (location && !didUserSelectStationFromMap) {
findNearestActiveStations(location.latitude, location.longitude, _startDate, _endDate)
.then((stationList: IStation[]) => {
if (stationList) {
Expand Down Expand Up @@ -110,7 +110,7 @@ export const LocationPicker = () => {
});
}
// eslint-disable-next-line react-hooks/exhaustive-deps
},[endDate, isEditing, location, startDate]);
},[endDate, isEditing, location, startDate, didUserSelectStationFromMap]);

useEffect(() => {
if (showStationSelectionList) {
Expand All @@ -137,6 +137,7 @@ export const LocationPicker = () => {
const placeNameSelected = (place: IPlace | undefined) => {
setState(draft => {
draft.location = place;
draft.didUserSelectStationFromMap = false;
});
setShowSelectionList(false);
setIsEditing(false);
Expand All @@ -149,6 +150,7 @@ export const LocationPicker = () => {
const stationSelected = (station: IWeatherStation | undefined) => {
setState(draft => {
draft.weatherStation = station;
draft.didUserSelectStationFromMap = false;
});
setShowStationSelectionList(false);
setStationHoveredIndex(null);
Expand Down Expand Up @@ -218,6 +220,7 @@ export const LocationPicker = () => {
setState(draft=>{
draft.location = locationPossibilities[selectedLocIdx];
draft.zoomMap = true;
draft.didUserSelectStationFromMap = false;
});
}
}
Expand All @@ -240,6 +243,7 @@ export const LocationPicker = () => {
const selectedStation = stationPossibilities[selectedLocIdx].station;
stationSelected(selectedStation);
setState(draft => {
draft.didUserSelectStationFromMap = false;
draft.weatherStation = selectedStation;
draft.weatherStationDistance = stationPossibilities[selectedLocIdx].distance;
});
Expand Down Expand Up @@ -273,6 +277,7 @@ export const LocationPicker = () => {
geoLocSearch(lat, long).then((currPosName) => {
setState(draft => {
draft.location = {name: currPosName, latitude: lat, longitude: long};
draft.didUserSelectStationFromMap = false;
});
setShowMapButton(true);
setIsEditing(false);
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export interface IState {
didUserSelectDate: boolean;
isMapOpen: boolean;
zoomMap: boolean;
didUserSelectStationFromMap?: boolean;
}

export const unitMap: UnitMap = {
Expand Down
6 changes: 3 additions & 3 deletions src/utils/geonameSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const kDefaultMaxRows = 4;
const kGeonamesUser = "codap";


async function geoNameSearch(searchString: string, maxRows?: number): Promise<IPlace[] | undefined> {
export const geoNameSearch = async (searchString: string, maxRows?: number): Promise<IPlace[] | undefined> =>{
const userClause = `username=${kGeonamesUser}`;
const countryClause = "country=US";
const maxRowsClause = `maxRows=${maxRows || kDefaultMaxRows}`;
Expand All @@ -34,7 +34,7 @@ async function geoNameSearch(searchString: string, maxRows?: number): Promise<IP
});
}
}
}
};

export const autoComplete = async(inputEl: HTMLInputElement) => {
let thisQuery = inputEl.value;
Expand All @@ -57,7 +57,7 @@ export const geoLocSearch = async (lat: number, long: number) => {
const response = await fetch(url);
if (response.ok) {
const data = await response.json();
return `${data?.geonames?.[0]?.name}, ${data?.geonames?.[0]?.adminCode1}` || "Unknown Location";
return `${data?.geonames?.[0]?.name}, ${data?.geonames?.[0]?.adminCode1}, ${data?.geonames?.[0]?.distance}` || "Unknown Location";
} else {
return Promise.reject(response.statusText);
}
Expand Down

0 comments on commit fdd6e44

Please sign in to comment.