From be803ffbd85d8fdf84054cdf4b580394b52431b6 Mon Sep 17 00:00:00 2001 From: eireland Date: Wed, 24 Jan 2024 16:26:52 -0800 Subject: [PATCH] Adds filtering for active stations --- src/components/location-picker.tsx | 9 +++---- src/utils/getWeatherStations.ts | 38 ++++++++++++++++++++++-------- 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/src/components/location-picker.tsx b/src/components/location-picker.tsx index 05d0d7b..a233658 100644 --- a/src/components/location-picker.tsx +++ b/src/components/location-picker.tsx @@ -13,7 +13,7 @@ import "./location-picker.scss"; export const LocationPicker = () => { const {state, setState} = useStateContext(); - const {units, location, weatherStation, weatherStationDistance} = state; + const {units, location, weatherStation, weatherStationDistance, startDate, endDate} = state; const [showMapButton, setShowMapButton] = useState(false); const [isEditing, setIsEditing] = useState(false); const [locationPossibilities, setLocationPossibilities] = useState([]); @@ -56,7 +56,6 @@ export const LocationPicker = () => { useEffect(() => { if (locationInputEl.current?.value === "") { setShowSelectionList(false); - // setSelectedLocation(undefined); } }, [locationInputEl.current?.value]); @@ -67,8 +66,10 @@ export const LocationPicker = () => { }, [isEditing]); useEffect(() => { + const _startDate = startDate ? startDate : new Date( -5364662060); // 1/1/1750 + const _endDate = endDate ? endDate : new Date(Date.now()); if (location) { - findNearestActiveStations(location.latitude, location.longitude, 80926000, "present") + findNearestActiveStations(location.latitude, location.longitude, _startDate, _endDate) .then((stationList: IStation[]) => { if (stationList) { setStationPossibilities(stationList); @@ -81,7 +82,7 @@ export const LocationPicker = () => { }); } // eslint-disable-next-line react-hooks/exhaustive-deps - },[location]); + },[endDate, isEditing, location, startDate]); useEffect(() => { if (showStationSelectionList) { diff --git a/src/utils/getWeatherStations.ts b/src/utils/getWeatherStations.ts index c0d1a1c..20f90bd 100644 --- a/src/utils/getWeatherStations.ts +++ b/src/utils/getWeatherStations.ts @@ -10,7 +10,6 @@ import weatherStations from "../assets/data/weather-stations.json"; */ export const adjustStationDataset = (dataset: IWeatherStation[]) => { const datasetArr = Array.from(dataset); - let maxDate: dayjs.Dayjs | null = null; if (dataset) { @@ -32,19 +31,19 @@ export const adjustStationDataset = (dataset: IWeatherStation[]) => { }); } } + return dataset; }; -export const findNearestActiveStations = async(targetLat: number, targetLong: number, fromDate: number | string, - toDate: number | string) => { - // TODO: filter out weather stations that are active - // let nearestStation: IWeatherStation | null = null; - // let minDistance = Number.MAX_VALUE; - let nearestStations: IStation[] = []; +export const findNearestActiveStations = async(targetLat: number, targetLong: number, fromDate: Date, + toDate: Date) => { + const adjustedStationDataset = adjustStationDataset(weatherStations as IWeatherStation[]); + const fromSecs = fromDate.getTime() / 1000; + const toSecs = toDate.getTime() / 1000; + const nearestStations: IStation[] = []; + console.log(adjustedStationDataset); - for (const station of weatherStations as IWeatherStation[]) { - const distance = calculateDistance(targetLat, targetLong, station.latitude, station.longitude); + const insertStation = (station: IWeatherStation, distance: number) => { const newStation = {station, distance}; - // Insert the new station into the sorted array at the correct position const index = nearestStations.findIndex(s => s.distance > distance); if (index === -1) { @@ -52,6 +51,25 @@ export const findNearestActiveStations = async(targetLat: number, targetLong: nu } else { nearestStations.splice(index, 0, newStation); } + }; + + for (const station of adjustedStationDataset) { + let shouldInsert = false; + if (station.maxdate === "present") { // If the station is still active (maxdate === "present") then we can use it + shouldInsert = true; + } else { // If the station is not active, we need to check if it has data in the date range + const stationMinSecs = new Date(station.mindate).getTime() / 1000; + const stationMaxSecs = new Date(station.maxdate).getTime() / 1000; + if (stationMinSecs <= toSecs && stationMaxSecs >= fromSecs) { + shouldInsert = true; + } + } + + if (shouldInsert) { + const distance = calculateDistance(targetLat, targetLong, station.latitude, station.longitude); + const newStation = {station, distance}; + insertStation(newStation.station, newStation.distance); + } } return nearestStations.slice(0, 5);