From 9c07119e6d9e3e39ce9a7f65cb6759b8d1fb452b Mon Sep 17 00:00:00 2001 From: Mateusz Konieczny Date: Tue, 10 Jan 2023 21:40:16 +0100 Subject: [PATCH] add button to reset overlay state --- .../frontend/displayPreferences-context.tsx | 78 +++++++++++++++++-- app/src/frontend/map/map-button.css | 8 ++ app/src/frontend/map/map.tsx | 2 + app/src/frontend/map/reset-switcher.tsx | 20 +++++ 4 files changed, 100 insertions(+), 8 deletions(-) create mode 100644 app/src/frontend/map/reset-switcher.tsx diff --git a/app/src/frontend/displayPreferences-context.tsx b/app/src/frontend/displayPreferences-context.tsx index bde2d89d..67c0f69a 100644 --- a/app/src/frontend/displayPreferences-context.tsx +++ b/app/src/frontend/displayPreferences-context.tsx @@ -3,6 +3,9 @@ import React, { createContext, useCallback, useContext, useEffect, useState } fr import { LayerEnablementState, MapTheme } from './config/map-config'; interface DisplayPreferencesContextState { + resetLayers: (e: React.FormEvent) => void; + anyLayerModifiedState: () => boolean; + vista: LayerEnablementState; vistaSwitch: (e: React.FormEvent) => void; vistaSwitchOnClick: React.MouseEventHandler; @@ -45,6 +48,9 @@ const stub = (): never => { }; export const DisplayPreferencesContext = createContext({ + resetLayers: stub, + anyLayerModifiedState: stub, + vista: undefined, vistaSwitch: stub, vistaSwitchOnClick: undefined, @@ -85,16 +91,69 @@ export const DisplayPreferencesContext = createContext {}; export const DisplayPreferencesProvider: React.FC<{}> = ({children}) => { - const [vista, setVista] = useState('disabled'); - const [flood, setFlood] = useState('disabled'); - const [creative, setCreative] = useState('disabled'); - const [housing, setHousing] = useState('disabled'); - const [borough, setBorough] = useState('enabled'); - const [parcel, setParcel] = useState('disabled'); - const [conservation, setConservation] = useState('disabled'); - const [historicData, setHistoricData] = useState('disabled'); + const defaultVista = 'disabled' + const defaultFlood = 'disabled' + const defaultCreative = 'disabled' + const defaultHousing = 'disabled' + const defaultBorough = 'enabled' + const defaultParcel = 'disabled' + const defaultConservation = 'disabled' + const defaultHistoricData = 'disabled' + const [vista, setVista] = useState(defaultVista); + const [flood, setFlood] = useState(defaultFlood); + const [creative, setCreative] = useState(defaultCreative); + const [housing, setHousing] = useState(defaultHousing); + const [borough, setBorough] = useState(defaultBorough); + const [parcel, setParcel] = useState(defaultParcel); + const [conservation, setConservation] = useState(defaultConservation); + const [historicData, setHistoricData] = useState(defaultHistoricData); const [darkLightTheme, setDarkLightTheme] = useState('night'); + const resetLayers = useCallback( + (e) => { + e.preventDefault(); + setVista(defaultVista); + setFlood(defaultFlood); + setCreative(defaultCreative); + setHousing(defaultHousing); + setBorough(defaultBorough) + setParcel(defaultParcel); + setConservation(defaultConservation); + setHistoricData(defaultHistoricData); + //setDarkLightTheme('night'); // reset only layers + }, + [] + ) + + function anyLayerModifiedState() { + if(vista != defaultVista) { + return true; + } + if(flood != defaultFlood) { + return true; + } + if(creative != defaultCreative) { + return true; + } + if(housing != defaultHousing) { + return true; + } + if(borough != defaultBorough) { + return true; + } + if(parcel != defaultParcel) { + return true; + } + if(conservation != defaultConservation) { + return true; + } + if(historicData != defaultHistoricData) { + return true; + } + //darkLightTheme not handled here + return false; + } + const vistaSwitch = useCallback( (e) => { e.preventDefault(); @@ -233,6 +292,9 @@ export const DisplayPreferencesProvider: React.FC<{}> = ({children}) => { return ( = ({ mode !== 'basic' && <> + { diff --git a/app/src/frontend/map/reset-switcher.tsx b/app/src/frontend/map/reset-switcher.tsx new file mode 100644 index 00000000..d8c06845 --- /dev/null +++ b/app/src/frontend/map/reset-switcher.tsx @@ -0,0 +1,20 @@ +import React from 'react'; + +import './map-button.css'; +import { useDisplayPreferences } from '../displayPreferences-context'; + +export const ResetSwitcher: React.FC<{}> = () => { + const { resetLayers, darkLightTheme, anyLayerModifiedState } = useDisplayPreferences(); + if(anyLayerModifiedState()) { + return ( +
+ +
+ ); + } else { + return <> + } +}