2022-12-06 03:03:06 -05:00
|
|
|
import React, { createContext, useCallback, useContext, useEffect, useState } from 'react';
|
|
|
|
|
2022-12-06 21:44:59 -05:00
|
|
|
import { LayerEnablementState, MapTheme } from './config/map-config';
|
2022-12-06 03:03:06 -05:00
|
|
|
|
|
|
|
interface DisplayPreferencesContextState {
|
2023-01-10 15:40:16 -05:00
|
|
|
resetLayers: (e: React.FormEvent<HTMLFormElement>) => void;
|
|
|
|
anyLayerModifiedState: () => boolean;
|
|
|
|
|
2022-12-06 03:03:06 -05:00
|
|
|
vista: LayerEnablementState;
|
|
|
|
vistaSwitch: (e: React.FormEvent<HTMLFormElement>) => void;
|
|
|
|
vistaSwitchOnClick: React.MouseEventHandler<HTMLButtonElement>;
|
|
|
|
|
|
|
|
flood: LayerEnablementState;
|
|
|
|
floodSwitch: (e: React.FormEvent<HTMLFormElement>) => void;
|
|
|
|
floodSwitchOnClick: React.MouseEventHandler<HTMLButtonElement>;
|
|
|
|
|
|
|
|
creative: LayerEnablementState;
|
|
|
|
creativeSwitch: (e: React.FormEvent<HTMLFormElement>) => void;
|
|
|
|
creativeSwitchOnClick: React.MouseEventHandler<HTMLButtonElement>;
|
|
|
|
|
|
|
|
housing: LayerEnablementState;
|
|
|
|
housingSwitch: (e: React.FormEvent<HTMLFormElement>) => void;
|
|
|
|
housingSwitchOnClick: React.MouseEventHandler<HTMLButtonElement>;
|
2022-12-06 21:44:59 -05:00
|
|
|
|
|
|
|
conservation: LayerEnablementState;
|
|
|
|
conservationSwitch: (e: React.FormEvent<HTMLFormElement>) => void;
|
|
|
|
conservationSwitchOnClick: React.MouseEventHandler<HTMLButtonElement>;
|
|
|
|
|
|
|
|
parcel: LayerEnablementState;
|
|
|
|
parcelSwitch: (e: React.FormEvent<HTMLFormElement>) => void;
|
|
|
|
parcelSwitchOnClick: React.MouseEventHandler<HTMLButtonElement>;
|
|
|
|
|
|
|
|
borough: LayerEnablementState;
|
|
|
|
boroughSwitch: (e: React.FormEvent<HTMLFormElement>) => void;
|
|
|
|
boroughSwitchOnClick: React.MouseEventHandler<HTMLButtonElement>;
|
|
|
|
|
|
|
|
historicData: LayerEnablementState;
|
|
|
|
historicDataSwitch: (e: React.FormEvent<HTMLFormElement>) => void;
|
|
|
|
historicDataSwitchOnClick: React.MouseEventHandler<HTMLButtonElement>;
|
|
|
|
|
|
|
|
darkLightTheme: MapTheme;
|
|
|
|
darkLightThemeSwitch: (e: React.FormEvent<HTMLFormElement>) => void;
|
|
|
|
darkLightThemeSwitchOnClick: React.MouseEventHandler<HTMLButtonElement>;
|
2022-12-06 03:03:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const stub = (): never => {
|
|
|
|
throw new Error('DisplayPreferencesProvider not set up');
|
|
|
|
};
|
|
|
|
|
|
|
|
export const DisplayPreferencesContext = createContext<DisplayPreferencesContextState>({
|
2023-01-10 15:40:16 -05:00
|
|
|
resetLayers: stub,
|
|
|
|
anyLayerModifiedState: stub,
|
|
|
|
|
2022-12-06 03:03:06 -05:00
|
|
|
vista: undefined,
|
|
|
|
vistaSwitch: stub,
|
|
|
|
vistaSwitchOnClick: undefined,
|
|
|
|
|
|
|
|
flood: undefined,
|
|
|
|
floodSwitch: stub,
|
|
|
|
floodSwitchOnClick: undefined,
|
|
|
|
|
|
|
|
creative: undefined,
|
|
|
|
creativeSwitch: stub,
|
|
|
|
creativeSwitchOnClick: undefined,
|
|
|
|
|
|
|
|
housing: undefined,
|
|
|
|
housingSwitch: stub,
|
|
|
|
housingSwitchOnClick: undefined,
|
|
|
|
|
|
|
|
conservation: undefined,
|
|
|
|
conservationSwitch: stub,
|
|
|
|
conservationSwitchOnClick: undefined,
|
|
|
|
|
|
|
|
parcel: undefined,
|
|
|
|
parcelSwitch: stub,
|
|
|
|
parcelSwitchOnClick: undefined,
|
|
|
|
|
|
|
|
borough: undefined,
|
|
|
|
boroughSwitch: stub,
|
|
|
|
boroughSwitchOnClick: undefined,
|
|
|
|
|
2022-12-06 21:44:59 -05:00
|
|
|
historicData: undefined,
|
|
|
|
historicDataSwitch: stub,
|
|
|
|
historicDataSwitchOnClick: undefined,
|
|
|
|
|
|
|
|
darkLightTheme: undefined,
|
|
|
|
darkLightThemeSwitch: stub,
|
|
|
|
darkLightThemeSwitchOnClick: undefined,
|
2022-12-06 03:03:06 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
const noop = () => {};
|
|
|
|
|
|
|
|
export const DisplayPreferencesProvider: React.FC<{}> = ({children}) => {
|
2023-01-10 15:40:16 -05:00
|
|
|
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<LayerEnablementState>(defaultVista);
|
|
|
|
const [flood, setFlood] = useState<LayerEnablementState>(defaultFlood);
|
|
|
|
const [creative, setCreative] = useState<LayerEnablementState>(defaultCreative);
|
|
|
|
const [housing, setHousing] = useState<LayerEnablementState>(defaultHousing);
|
|
|
|
const [borough, setBorough] = useState<LayerEnablementState>(defaultBorough);
|
|
|
|
const [parcel, setParcel] = useState<LayerEnablementState>(defaultParcel);
|
|
|
|
const [conservation, setConservation] = useState<LayerEnablementState>(defaultConservation);
|
|
|
|
const [historicData, setHistoricData] = useState<LayerEnablementState>(defaultHistoricData);
|
2022-12-06 21:44:59 -05:00
|
|
|
const [darkLightTheme, setDarkLightTheme] = useState<MapTheme>('night');
|
2022-12-06 03:03:06 -05:00
|
|
|
|
2023-01-10 15:40:16 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-12-06 03:03:06 -05:00
|
|
|
const vistaSwitch = useCallback(
|
|
|
|
(e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
const newVista = (vista === 'enabled')? 'disabled' : 'enabled';
|
|
|
|
setVista(newVista);
|
|
|
|
},
|
|
|
|
[vista],
|
|
|
|
)
|
|
|
|
|
|
|
|
const vistaSwitchOnClick = (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
const newVista = (vista === 'enabled')? 'disabled' : 'enabled';
|
|
|
|
setVista(newVista);
|
|
|
|
}
|
|
|
|
|
|
|
|
const floodSwitch = useCallback(
|
|
|
|
(e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
const newFlood = (flood === 'enabled')? 'disabled' : 'enabled';
|
|
|
|
setFlood(newFlood);
|
|
|
|
},
|
|
|
|
[flood],
|
|
|
|
)
|
|
|
|
|
|
|
|
const floodSwitchOnClick = (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
const newFlood = (flood === 'enabled')? 'disabled' : 'enabled';
|
|
|
|
setFlood(newFlood);
|
|
|
|
}
|
|
|
|
|
|
|
|
const housingSwitch = useCallback(
|
|
|
|
(e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
const newHousing = (housing === 'enabled')? 'disabled' : 'enabled';
|
|
|
|
setHousing(newHousing);
|
|
|
|
},
|
|
|
|
[housing],
|
|
|
|
)
|
|
|
|
|
|
|
|
const housingSwitchOnClick = (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
const newHousing = (housing === 'enabled')? 'disabled' : 'enabled';
|
|
|
|
setHousing(newHousing);
|
|
|
|
}
|
|
|
|
|
|
|
|
const creativeSwitch = useCallback(
|
|
|
|
(e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
const newCreative = (creative === 'enabled')? 'disabled' : 'enabled';
|
|
|
|
setCreative(newCreative);
|
|
|
|
},
|
|
|
|
[creative],
|
|
|
|
)
|
|
|
|
|
|
|
|
const creativeSwitchOnClick = (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
const newCreative = (creative === 'enabled')? 'disabled' : 'enabled';
|
|
|
|
setCreative(newCreative);
|
|
|
|
}
|
|
|
|
|
2022-12-06 21:44:59 -05:00
|
|
|
const boroughSwitch = useCallback(
|
|
|
|
(e) => {
|
|
|
|
flipBorough(e)
|
|
|
|
},
|
|
|
|
[borough],
|
|
|
|
)
|
|
|
|
const boroughSwitchOnClick = (e) => {
|
|
|
|
flipBorough(e)
|
|
|
|
}
|
|
|
|
function flipBorough(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
const newBorough = (borough === 'enabled')? 'disabled' : 'enabled';
|
|
|
|
setBorough(newBorough);
|
|
|
|
}
|
|
|
|
|
|
|
|
const parcelSwitch = useCallback(
|
|
|
|
(e) => {
|
|
|
|
flipParcel(e)
|
|
|
|
},
|
|
|
|
[parcel],
|
|
|
|
)
|
|
|
|
const parcelSwitchOnClick = (e) => {
|
|
|
|
flipParcel(e)
|
|
|
|
}
|
|
|
|
function flipParcel(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
const newParcel = (parcel === 'enabled')? 'disabled' : 'enabled';
|
|
|
|
setParcel(newParcel);
|
|
|
|
}
|
|
|
|
|
|
|
|
const conservationSwitch = useCallback(
|
|
|
|
(e) => {
|
|
|
|
flipConservation(e)
|
|
|
|
},
|
|
|
|
[conservation],
|
|
|
|
)
|
|
|
|
const conservationSwitchOnClick = (e) => {
|
|
|
|
flipConservation(e)
|
|
|
|
}
|
|
|
|
function flipConservation(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
const newConservation = (conservation === 'enabled')? 'disabled' : 'enabled';
|
|
|
|
setConservation(newConservation);
|
|
|
|
}
|
|
|
|
|
|
|
|
const historicDataSwitch = useCallback(
|
|
|
|
(e) => {
|
|
|
|
flipHistoricData(e)
|
|
|
|
},
|
|
|
|
[historicData],
|
|
|
|
)
|
|
|
|
const historicDataSwitchOnClick = (e) => {
|
|
|
|
flipHistoricData(e)
|
|
|
|
}
|
|
|
|
function flipHistoricData(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
const newHistoric = (historicData === 'enabled')? 'disabled' : 'enabled';
|
|
|
|
setHistoricData(newHistoric);
|
|
|
|
}
|
|
|
|
|
|
|
|
const darkLightThemeSwitch = useCallback(
|
|
|
|
(e) => {
|
|
|
|
flipDarkLightTheme(e)
|
|
|
|
},
|
|
|
|
[darkLightTheme],
|
|
|
|
)
|
|
|
|
const darkLightThemeSwitchOnClick = (e) => {
|
|
|
|
flipDarkLightTheme(e)
|
|
|
|
}
|
|
|
|
function flipDarkLightTheme(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
const newDarkLightTheme = (darkLightTheme === 'light')? 'night' : 'light';
|
|
|
|
setDarkLightTheme(newDarkLightTheme);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-06 03:03:06 -05:00
|
|
|
return (
|
|
|
|
<DisplayPreferencesContext.Provider value={{
|
2023-01-10 15:40:16 -05:00
|
|
|
resetLayers,
|
|
|
|
anyLayerModifiedState,
|
|
|
|
|
2022-12-06 03:03:06 -05:00
|
|
|
vista,
|
|
|
|
vistaSwitch,
|
|
|
|
vistaSwitchOnClick,
|
|
|
|
flood,
|
|
|
|
floodSwitch,
|
|
|
|
floodSwitchOnClick,
|
|
|
|
creative,
|
|
|
|
creativeSwitch,
|
|
|
|
creativeSwitchOnClick,
|
|
|
|
housing,
|
|
|
|
housingSwitch,
|
2022-12-06 21:44:59 -05:00
|
|
|
housingSwitchOnClick,
|
|
|
|
conservation,
|
|
|
|
conservationSwitch,
|
|
|
|
conservationSwitchOnClick,
|
|
|
|
parcel,
|
|
|
|
parcelSwitch,
|
|
|
|
parcelSwitchOnClick,
|
|
|
|
|
|
|
|
borough,
|
|
|
|
boroughSwitch,
|
|
|
|
boroughSwitchOnClick,
|
|
|
|
|
|
|
|
historicData,
|
|
|
|
historicDataSwitch,
|
|
|
|
historicDataSwitchOnClick,
|
|
|
|
|
|
|
|
darkLightTheme,
|
|
|
|
darkLightThemeSwitch,
|
|
|
|
darkLightThemeSwitchOnClick
|
2022-12-06 03:03:06 -05:00
|
|
|
}}>
|
|
|
|
{children}
|
|
|
|
</DisplayPreferencesContext.Provider>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useDisplayPreferences = (): DisplayPreferencesContextState => {
|
|
|
|
return useContext(DisplayPreferencesContext);
|
|
|
|
};
|