import React, { createContext, useCallback, useContext, useEffect, useState } from 'react'; import { LayerEnablementState, MapTheme } from './config/map-config'; interface DisplayPreferencesContextState { vista: LayerEnablementState; vistaSwitch: (e: React.FormEvent) => void; vistaSwitchOnClick: React.MouseEventHandler; flood: LayerEnablementState; floodSwitch: (e: React.FormEvent) => void; floodSwitchOnClick: React.MouseEventHandler; creative: LayerEnablementState; creativeSwitch: (e: React.FormEvent) => void; creativeSwitchOnClick: React.MouseEventHandler; housing: LayerEnablementState; housingSwitch: (e: React.FormEvent) => void; housingSwitchOnClick: React.MouseEventHandler; conservation: LayerEnablementState; conservationSwitch: (e: React.FormEvent) => void; conservationSwitchOnClick: React.MouseEventHandler; parcel: LayerEnablementState; parcelSwitch: (e: React.FormEvent) => void; parcelSwitchOnClick: React.MouseEventHandler; borough: LayerEnablementState; boroughSwitch: (e: React.FormEvent) => void; boroughSwitchOnClick: React.MouseEventHandler; historicData: LayerEnablementState; historicDataSwitch: (e: React.FormEvent) => void; historicDataSwitchOnClick: React.MouseEventHandler; darkLightTheme: MapTheme; darkLightThemeSwitch: (e: React.FormEvent) => void; darkLightThemeSwitchOnClick: React.MouseEventHandler; } const stub = (): never => { throw new Error('DisplayPreferencesProvider not set up'); }; export const DisplayPreferencesContext = createContext({ 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, historicData: undefined, historicDataSwitch: stub, historicDataSwitchOnClick: undefined, darkLightTheme: undefined, darkLightThemeSwitch: stub, darkLightThemeSwitchOnClick: undefined, }); const noop = () => {}; 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 [darkLightTheme, setDarkLightTheme] = useState('night'); 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); } 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); } return ( {children} ); }; export const useDisplayPreferences = (): DisplayPreferencesContextState => { return useContext(DisplayPreferencesContext); };