Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save + get interactive state. (PT-186939189) #39

Merged
merged 2 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { composeURL, formatData } from "../utils/noaaApiHelper";
import { StationDSName } from "../constants";
import { geoLocSearch } from "../utils/geonameSearch";
import { DataReturnWarning } from "./data-return-warning";
import { IState } from "../types";
import dayjs from "dayjs";

import "./App.scss";

Expand All @@ -33,7 +35,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) =>{
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);
Loading