Skip to content

Commit

Permalink
Merge pull request #71 from concord-consortium/187212874-handle-legac…
Browse files Browse the repository at this point in the history
…y-doc

187212874 handle legacy doc
  • Loading branch information
eireland authored Apr 10, 2024
2 parents c0baa5b + 0edba4e commit dbca317
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 28 deletions.
81 changes: 61 additions & 20 deletions src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import { AttributeFilter } from "./attribute-filter";
import { InfoModal } from "./info-modal";
import { useStateContext } from "../hooks/use-state";
import { adjustStationDataset, getWeatherStations } from "../utils/getWeatherStations";
import { addNotificationHandler, createStationsDataset, guaranteeGlobal } from "../utils/codapHelpers";
import { addNotificationHandler, createStationsDataset, guaranteeGlobal, hasMap } from "../utils/codapHelpers";
import { useCODAPApi } from "../hooks/use-codap-api";
import { composeURL, formatData } from "../utils/noaaApiHelper";
import { DSName, StationDSName, globalMaxDate, globalMinDate } from "../constants";
import { geoLocSearch, geoNameSearch } from "../utils/geonameSearch";
import { DataReturnWarning } from "./data-return-warning";
import { IState, IStation, IStatus } from "../types";
import { IState, IStation, IStatus, ILegacyState } from "../types";
import InfoIcon from "../assets/images/icon-info.svg";
import ProgressIcon from "../assets/images/icon-progress-indicator.svg";
import DoneIcon from "../assets/images/icon-done.svg";
Expand All @@ -41,32 +41,73 @@ export const App = () => {

useEffect(() => {
const init = async () => {
const newState = await initializePlugin({pluginName: kPluginName, version: kVersion, dimensions: kInitialDimensions}) as IState;
const newState = await initializePlugin({pluginName: kPluginName, version: kVersion, dimensions: kInitialDimensions});
const isMapOpen = await hasMap();
// plugins in new documents return an empty object for the interactive state
// so ignore the new state and keep the default starting state in that case
if (Object.keys(newState || {}).length > 0) {
setState((draft) => {
draft.location = newState.location;
draft.selectedFrequency = newState.selectedFrequency;
draft.units = newState.units;
draft.timezone = newState.timezone;
draft.weatherStation = newState.weatherStation;
draft.weatherStationDistance = newState.weatherStationDistance;
draft.zoomMap = newState.zoomMap;
draft.frequencies = newState.frequencies;
draft.didUserSelectDate = newState.didUserSelectDate;
draft.isMapOpen = newState.isMapOpen;

const startDateStr = newState.startDate;
const endDateStr = newState.endDate;
// Check if newState is essentially empty
if (Object.keys(newState || {}).length === 0) {
// eslint-disable-next-line no-console
console.log("New state is empty, keeping default state.");
return;
}
// legacy plugin uses sampleFrequency as key vs new plugin uses selectedFrequency
const isLegacy = "sampleFrequency" in newState;
let locale = "", localeLat=0, localeLong=0, distance=0;
if (isLegacy) {
const legacyState = newState as ILegacyState;
const legacyStation = legacyState.selectedStation;
const locationInfo = await geoLocSearch(legacyStation.latitude, legacyStation.longitude);
locale = `${locationInfo.split(",")[0]}, ${locationInfo.split(",")[1]}`;
distance = Number(locationInfo.split(",")[2]);
const localeLatLong = await geoNameSearch(locale);
localeLat = localeLatLong?.[0].latitude || legacyStation.latitude;
localeLong = localeLatLong?.[0].longitude || legacyStation.longitude;
}
setState((draft: IState) => {
if (isLegacy) {
draft.isMapOpen = isMapOpen;
const legacyState = newState as ILegacyState;
draft.selectedFrequency = legacyState.sampleFrequency;
draft.units = legacyState.unitSystem;
draft.didUserSelectDate = !!legacyState.userSelectedDate;
draft.weatherStation = legacyState.selectedStation;
draft.weatherStationDistance = distance;
draft.location = {name: locale, latitude: localeLat, longitude: localeLong};
const startDateStr = legacyState.startDate;
const endDateStr = legacyState.endDate;
if (draft.timezone) {
draft.timezone.gmtOffset = (legacyState.stationTimezoneOffset).toString();
draft.timezone.name = legacyState.stationTimezoneName;
}
if (startDateStr) {
draft.startDate = dayjs(startDateStr).toDate();
}
if (endDateStr) {
draft.endDate = dayjs(endDateStr).toDate();
}
});
}
} else {
const _newState = newState as IState;
draft.location = _newState.location;
draft.selectedFrequency = _newState.selectedFrequency;
draft.units = _newState.units;
draft.timezone = _newState.timezone;
draft.weatherStation = _newState.weatherStation;
draft.weatherStationDistance = _newState.weatherStationDistance;
draft.zoomMap = _newState.zoomMap;
draft.frequencies =_newState.frequencies;
draft.didUserSelectDate = _newState.didUserSelectDate;
draft.isMapOpen = _newState.isMapOpen;
const startDateStr = _newState.startDate;
const endDateStr = _newState.endDate;
if (startDateStr) {
draft.startDate = dayjs(startDateStr).toDate();
}
if (endDateStr) {
draft.endDate = dayjs(endDateStr).toDate();
}
}
});
};
init();
const adjustedStationDataset = adjustStationDataset(); //change max data to "present"
Expand Down
5 changes: 3 additions & 2 deletions src/components/attribute-filter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {
dailyMonthlyAttrMap,
hourlyAttrMap,
IAboveMeanFilter,
IBelowMeanFilter
IBelowMeanFilter,
IFilter
} from "../types";
import EditIcon from "../assets/images/icon-edit.svg";

Expand Down Expand Up @@ -221,7 +222,7 @@ const FilterModal = ({attr, position, targetFilterBottom, setShowFilterModal, se
setState(draft => {
const existingFilter = frequencies[selectedFrequency].filters.find(f=>f.attribute === attr.name);
const attrFilterIndex = existingFilter && frequencies[selectedFrequency].filters.indexOf(existingFilter);
const existingFilterDraft = draft.frequencies[selectedFrequency].filters.find(f=>f.attribute === attr.name);
const existingFilterDraft = draft.frequencies[selectedFrequency].filters.find((f: IFilter)=>f.attribute === attr.name);
if (existingFilter) {
switch (operator) {
case "between":
Expand Down
4 changes: 2 additions & 2 deletions src/components/attribute-selector.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useEffect, useState } from "react";
import classnames from "classnames";
import { useStateContext } from "../hooks/use-state";
import { dailyMonthlyAttrMap, hourlyAttrMap } from "../types";
import { AttrType, dailyMonthlyAttrMap, hourlyAttrMap } from "../types";
import { dataTypeStore } from "../utils/noaaDataTypes";

import "./attribute-selector.scss";
Expand Down Expand Up @@ -78,7 +78,7 @@ export const AttributesSelector = () => {
const selectedAttr = attributeList.find(a => a.name === selectedAttrName);
const filters = selectedAttrsAndFiltersForFrequency.filters;
setState(draft => {
const draftAttrNames = draft.frequencies[selectedFrequency].attrs.map(a => {return a.name;});
const draftAttrNames = draft.frequencies[selectedFrequency].attrs.map((a:AttrType) => {return a.name;});
if (allSelected) {
setAllSelected(false);
draft.frequencies[selectedFrequency] = {attrs: [], filters};
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/use-codap-api.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useEffect, useState } from "react";
import { useStateContext } from "./use-state";
import { Attribute, Collection, DataContext, ICODAPItem, IDataType, IItem } from "../types";
import { Attribute, Collection, DataContext, DefaultState, ICODAPItem, IDataType, IItem } from "../types";
import { IResult, codapInterface, createItems, getAllItems, getDataContext } from "@concord-consortium/codap-plugin-api";
import { DSCollection1, DSCollection2, DSName, kStationsDatasetName } from "../constants";
import { createMap, selectStations } from "../utils/codapHelpers";
Expand All @@ -10,7 +10,7 @@ export const useCODAPApi = () => {
const {state} = useStateContext();
const [ selectedDataTypes, setSelectedDataTypes ] = useState<IDataType[]>([]);
const { frequencies, selectedFrequency, weatherStation, units, isMapOpen, zoomMap } = state;
const { attrs } = frequencies[selectedFrequency];
const { attrs } = frequencies ? frequencies[selectedFrequency] : DefaultState.frequencies.daily;

useEffect(() => {
if (weatherStation && isMapOpen) {
Expand Down
19 changes: 17 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export interface IWeatherStation {
maxdate: string; // "1973-01-01" || "present",
isdID: string; // "72613014755,72613099999",
ghcndID: string; // "USW00014755"
ranges: IWeatherStationRange[];
ranges?: IWeatherStationRange[];
}

interface IWeatherStationRange {
Expand All @@ -95,7 +95,7 @@ interface IWeatherStationRange {
longitude: number;
name: string;
elevation?: string | number;
ids: IWeatherStationID[];
ids?: IWeatherStationID[];
}

interface IWeatherStationID {
Expand All @@ -107,6 +107,21 @@ export interface ITimeZone {
gmtOffset: string;
name: string;
}
export interface ILegacyState {
database: string;
dateGranularity: string | null;
endDate: string;
isFetchable: boolean;
sampleFrequency: IFrequency;
selectedDataTypes: IDataType[];
length: number;
selectedStation: IWeatherStation;
startDate: string;
stationTimezoneName: string;
stationTimezoneOffset: number;
unitSystem: IUnits;
userSelectedDate: string | null;
}

export interface IState {
location?: IPlace;
Expand Down

0 comments on commit dbca317

Please sign in to comment.