Skip to content

Commit

Permalink
Fixes distance calculation when station is selected from the CODAP map
Browse files Browse the repository at this point in the history
  • Loading branch information
eireland committed Jan 31, 2024
1 parent 8ef01fa commit 87b4721
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
12 changes: 6 additions & 6 deletions src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import { AttributesSelector } from "./attribute-selector";
import { AttributeFilter } from "./attribute-filter";
import { InfoModal } from "./info-modal";
import { useStateContext } from "../hooks/use-state";
import { adjustStationDataset, calculateDistance, getWeatherStations } from "../utils/getWeatherStations";
import { adjustStationDataset, getWeatherStations } from "../utils/getWeatherStations";
import { addNotificationHandler, createStationsDataset, guaranteeGlobal } from "../utils/codapHelpers";
import { useCODAPApi } from "../hooks/use-codap-api";
import { composeURL, formatData } from "../utils/noaaApiHelper";
import { StationDSName, globalMaxDate, globalMinDate } from "../constants";
import { geoLocSearch, geoNameSearch } from "../utils/geonameSearch";
import { geoLocSearch } from "../utils/geonameSearch";
import { DataReturnWarning } from "./data-return-warning";
import { IState } from "../types";
import InfoIcon from "../assets/images/icon-info.svg";
Expand Down Expand Up @@ -82,12 +82,12 @@ export const App = () => {
if (myCase) {
const station = myCase.values;
const {latitude, longitude} = station;
const locationName = await geoLocSearch(latitude, longitude);
const locale = await geoNameSearch(locationName, 1);
const distance = locale && calculateDistance(latitude, longitude, locale[0].latitude, locale[0].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.location = {name: locale, latitude, longitude};
draft.weatherStationDistance = distance;
draft.zoomMap = false;
draft.didUserSelectStationFromMap = true;
Expand Down
3 changes: 1 addition & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export interface IState {
didUserSelectDate: boolean;
isMapOpen: boolean;
zoomMap: boolean;
didUserSelectStationFromMap: boolean;
didUserSelectStationFromMap?: boolean;
}

export const unitMap: UnitMap = {
Expand Down Expand Up @@ -165,7 +165,6 @@ export const DefaultState: IState = {
didUserSelectDate: false,
isMapOpen: false,
zoomMap: false,
didUserSelectStationFromMap: false,
};

interface IDataTypeUnits {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/geonameSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 87b4721

Please sign in to comment.