From fa03f5d18d362d66565709ca271d89ffb4231a62 Mon Sep 17 00:00:00 2001 From: John Date: Wed, 14 Feb 2024 00:50:34 +0000 Subject: [PATCH] - #178 Continue use of react-query for server state management - Update all of preview components to use react-query hooks. - Clean up old code, work into new state model. - Add multiple guards for missing data or loading states. --- public/config.yaml | 9 +- src/components/daterange/sub/DataTrack.js | 3 +- src/components/main/App/App.js | 17 ++-- src/components/main/App/use-initialize-app.js | 55 ----------- src/components/main/Body/Body.js | 11 ++- src/components/main/Body/station-data-tab.js | 37 +++++++ .../preview/StationPreview/GraphsBlock.js | 99 ++++++++++++++----- .../preview/StationPreview/HeaderBlock.js | 31 +++++- .../preview/StationPreview/NavBlock.js | 30 ++++-- .../preview/StationPreview/PreviewGraph.js | 30 ++++-- .../preview/StationPreview/RangeBlock.js | 87 ++++++++-------- .../preview/StationPreview/StationPreview.js | 55 ++++------- src/index.js | 13 ++- .../use-config-defaults.js | 55 +++++++++++ .../use-station-variables-defaults.js | 56 +++++++++++ src/state/query-hooks/use-config.js | 37 +++---- .../use-station-variable-observations.js | 18 +++- .../query-hooks/use-station-variables.js | 7 ++ src/state/query-hooks/use-station.js | 21 +++- src/state/query-hooks/use-stations.js | 8 +- src/state/slice-preview.js | 87 ++++++++++++++++ src/state/state-store.js | 4 + 22 files changed, 546 insertions(+), 224 deletions(-) mode change 100644 => 100755 public/config.yaml delete mode 100644 src/components/main/App/use-initialize-app.js create mode 100644 src/components/main/Body/station-data-tab.js create mode 100644 src/state/client-server-hooks/use-config-defaults.js create mode 100644 src/state/client-server-hooks/use-station-variables-defaults.js create mode 100644 src/state/slice-preview.js diff --git a/public/config.yaml b/public/config.yaml old mode 100644 new mode 100755 index 14cf2494..440b9cf0 --- a/public/config.yaml +++ b/public/config.yaml @@ -4,11 +4,11 @@ baseMap: BC # sdsUrl values will be replaced by dev or prod URLs when they become ready. # For now, we have demo instances inside the firewall. # Uses monsoon database -sdsUrl: http://docker-dev02.pcic.uvic.ca:30512 +#sdsUrl: http://docker-dev02.pcic.uvic.ca:30512 # Uses new database #sdsUrl: http://docker-dev02.pcic.uvic.ca:30562 # Local instance -#sdsUrl: http://localhost:5000 +sdsUrl: http://localhost:5000 # Currently deployed metadata backends do not respond to provinces QP. # When they do, we can use stationsQpProvinces and lose stationFilters @@ -34,3 +34,8 @@ pdpDataUrl: http://docker-dev02.pcic.uvic.ca:30514 #stationStride: undefined #showReloadStationsButton: false #timingEnabled: false + +# Preview Options +plotColor: "#1f77b4" +dataRequestDurations: [1, 3, 6, 12] # months +dataRequestDurationsDefault: 6 # months diff --git a/src/components/daterange/sub/DataTrack.js b/src/components/daterange/sub/DataTrack.js index 7039d0df..f4f4af2c 100644 --- a/src/components/daterange/sub/DataTrack.js +++ b/src/components/daterange/sub/DataTrack.js @@ -21,12 +21,11 @@ const baseHeight = 50; const getTrackStyle = ({ source, target, count, index, color }) => { const height = baseHeight / count / 2; const topPosition = -22 + (baseHeight / count) * index + height / 2; - console.log("### getTrackStyle", source, target, count, index, color); const basicStyle = { top: `${topPosition}px`, height: `${height}px`, left: `${source.percent}%`, - width: `calc(${target.percent - source.percent}% - 1px)`, + width: `calc(${target.percent - source.percent}% + 1px)`, backgroundColor: color, }; diff --git a/src/components/main/App/App.js b/src/components/main/App/App.js index ddd843af..9e4400d8 100644 --- a/src/components/main/App/App.js +++ b/src/components/main/App/App.js @@ -1,19 +1,16 @@ -import React, { useEffect } from "react"; +import React from "react"; import { Container } from "react-bootstrap"; import Disclaimer from "../../info/Disclaimer"; import Header from "../Header/Header"; -import useInitializeApp from "./use-initialize-app"; import { Outlet } from "react-router-dom"; import "./App.css"; -import { useConfig } from "../../../state/query-hooks/use-config"; - -export default function App() { - const { isLoading, isError } = useConfig(); - useInitializeApp(); +import { useConfigDefaults } from "../../../state/client-server-hooks/use-config-defaults"; +export const App = () => { + const { isLoading, isError } = useConfigDefaults(); if (isError) { - return
{configErrorMessage}
; + return
An Error occoured while loading the app configuration.
; } if (isLoading) { @@ -27,4 +24,6 @@ export default function App() { ); -} +}; + +export default App; diff --git a/src/components/main/App/use-initialize-app.js b/src/components/main/App/use-initialize-app.js deleted file mode 100644 index d7750a15..00000000 --- a/src/components/main/App/use-initialize-app.js +++ /dev/null @@ -1,55 +0,0 @@ -// This hook performs a number of one-time app initializations. -import { useEffect } from "react"; -import L from "leaflet"; -import { setLethargicMapScrolling } from "../../../utils/leaflet-extensions"; -import { setTimingEnabled } from "../../../utils/timing"; -import { useConfig } from "../../../state/query-hooks/use-config"; - -function initializeApp(config) { - console.log("### initializeApp", config); - if (config === null) { - return; - } - - // Set browser title - document.title = config.appTitle; - - // Set up (polygon) drawing tool in Leaflet. - L.drawLocal.edit.toolbar.buttons = { - edit: "Edit shapes", - editDisabled: "No shapes to edit", - remove: "Remove shapes", - removeDisabled: "No shapes to remove", - }; - L.drawLocal.edit.handlers.remove.tooltip = "Click shape to remove"; - L.drawLocal.edit.toolbar.actions.clearAll = { - title: "Remove all shapes", - text: "Remove all", - }; - - // Initialize Lethargy, which fixes scrolling problems with Mac computers. - if (config.lethargy.enabled) { - setLethargicMapScrolling( - config.lethargy.stability, - config.lethargy.sensitivity, - config.lethargy.tolerance, - ); - } - - // Export timing values to non-component code - setTimingEnabled(config.timingEnabled); -} - -/** - * This hook loads the config and initializes the app. Primarily setting up Map, Scrolling behaviour and debug timing. - * - * @returns {void} - */ -export default function useInitializeApp() { - const { data } = useConfig(); - useEffect(() => { - if (data) { - initializeApp(data); - } - }, [data]); -} diff --git a/src/components/main/Body/Body.js b/src/components/main/Body/Body.js index 48b1eec3..2db1d935 100644 --- a/src/components/main/Body/Body.js +++ b/src/components/main/Body/Body.js @@ -29,6 +29,7 @@ import { useConfig } from "../../../state/query-hooks/use-config"; import { useStations } from "../../../state/query-hooks/use-stations"; import { useVariables } from "../../../state/query-hooks/use-variables"; import { useFrequencies } from "../../../state/query-hooks/use-frequencies"; +import { useNetworks } from "../../../state/query-hooks/use-networks"; import css from "../common.module.css"; @@ -51,8 +52,12 @@ function Body() { isLoading: isFrequenciesLoading, isError: isFrequenciesError, } = useFrequencies(); + const { + data: networks, + isLoading: networksLoading, + isError: networksError, + } = useNetworks(); - // actions should be fixed functions on the store, so they shouldn't really change const actions = useStore( useShallow((state) => ({ setStationsLimit: state.setStationsLimit, @@ -117,7 +122,7 @@ function Body() {