Skip to content

Commit

Permalink
Merge pull request #39 from concord-consortium/186939189-get-interact…
Browse files Browse the repository at this point in the history
…ive-state

Save + get interactive state. (PT-186939189)
  • Loading branch information
lublagg authored Jan 30, 2024
2 parents 8486bfd + beb31f8 commit f04fab2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
34 changes: 33 additions & 1 deletion src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { IDataType } from "../types";
import { StationDSName, globalMaxDate, globalMinDate } from "../constants";
import { geoLocSearch } from "../utils/geonameSearch";
import { DataReturnWarning } from "./data-return-warning";
import { IState } from "../types";

Check failure on line 18 in src/components/App.tsx

View workflow job for this annotation

GitHub Actions / Build & Run Tests

'../types' import is duplicated

Check warning on line 18 in src/components/App.tsx

View workflow job for this annotation

GitHub Actions / Build & Run Tests

'/home/runner/work/noaa-codap-plugin/noaa-codap-plugin/src/types.ts' imported multiple times
import dayjs from "dayjs";

import "./App.scss";

Expand All @@ -34,7 +36,37 @@ export const App = () => {
const weatherStations = getWeatherStations();

useEffect(() => {
initializePlugin({pluginName: kPluginName, version: kVersion, dimensions: kInitialDimensions});

const init = async () => {
const newState = await initializePlugin({pluginName: kPluginName, version: kVersion, dimensions: kInitialDimensions}) as IState;
// 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;
if (startDateStr) {
draft.startDate = dayjs(startDateStr).toDate();
}
if (endDateStr) {
draft.endDate = dayjs(endDateStr).toDate();
}
});
}
};

init();

const stationSelectionHandler = async(req: any) =>{
if (req.values.operation === "selectCases") {
Expand Down
14 changes: 11 additions & 3 deletions src/hooks/use-state.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createContext, useContext } from "react";
import { createContext, useContext, useEffect } from "react";
import { Updater, useImmer } from "use-immer";
import { IState, DefaultState } from "../types";
import { codapInterface } from "@concord-consortium/codap-plugin-api";

export interface IStateContext {
state: IState;
Expand All @@ -10,10 +11,17 @@ export interface IStateContext {
export const useStateContextInAppContainerOnly = (): IStateContext => {
const [state, setState] = useImmer<IState>(DefaultState);

return { state, setState };
useEffect(() => {
codapInterface.updateInteractiveState(state);
}, [state]);


return {
state,
setState
};
};

// note: the "setState: () => undefined" is fine as it is overridden in the AppContainer.Provider tag
export const StateContext = createContext<IStateContext>({state: DefaultState, setState: () => undefined});

export const useStateContext = () => useContext(StateContext);

0 comments on commit f04fab2

Please sign in to comment.