diff --git a/src/components/map/default-layers.js b/src/components/map/default-layers.js index 8ff22fc2..d214119e 100644 --- a/src/components/map/default-layers.js +++ b/src/components/map/default-layers.js @@ -1,4 +1,4 @@ -import React, { Fragment, useEffect, useState } from 'react'; +import React, { useEffect, useState } from 'react'; import { WMSTileLayer, GeoJSON } from 'react-leaflet'; import { CircleMarker } from 'leaflet'; import { useLayers } from '@context'; @@ -87,7 +87,12 @@ export const DefaultLayers = () => { data.workbench.forEach(function (layer_id) { const layer = getCatalogEntry(data.catalog, layer_id); if (layer) - layer_list.push(layer); + layer_list.push({ + ...layer, + state: { + visible: true, + } + }); // TODO: do we really need to do this here??! // if this is an obs layer, need to retrieve @@ -130,15 +135,12 @@ export const DefaultLayers = () => { getDefaultLayers().then(); }, []); - //console.log("defaultModelLayers: " + JSON.stringify(defaultModelLayers, null, 2)) - - return ( - <> - {defaultModelLayers.map((layer, index) => { + return defaultModelLayers + .filter(layer => layer.state.visible) + .map((layer, index) => { const pieces = layer.id.split('-'); const type = pieces[pieces.length-1]; - //console.log("type: " + JSON.stringify(type, null, 2)) - if( type === "obs" && obsData !== "") { + if (type === "obs" && obsData !== "") { //console.log("obsData: " + JSON.stringify(obsData, null, 2)); return ( { onEachFeature = {onEachObsFeature} /> ); - } - else { + } else { return ( { /> ); } - })}; - - ); + }); }; \ No newline at end of file diff --git a/src/components/trays/layers/list.js b/src/components/trays/layers/list.js index d8de2482..8c66538a 100644 --- a/src/components/trays/layers/list.js +++ b/src/components/trays/layers/list.js @@ -18,15 +18,12 @@ import { import { useLayers } from '@context'; export const LayersList = () => { - const { defaultModelLayers } = useLayers(); + const { defaultModelLayers, toggleLayerVisibility } = useLayers(); const layers = [...defaultModelLayers]; - console.log(layers); + console.table(layers[0]); const [expandedIds, setExpandedIds] = useState(new Set()); - // of course, this is dummy state. - // real state will be maintained in some higher-up context. - const [visibleIds, setVisibleIds] = useState(new Set()); const handleToggleExpansion = id => () => { const _expandedIds = new Set([...expandedIds]); @@ -39,23 +36,12 @@ export const LayersList = () => { setExpandedIds(_expandedIds); }; - const handleToggleVisibilitySwitch = id => () => { - const _visibleIds = new Set([...visibleIds]); - if (_visibleIds.has(id)) { - _visibleIds.delete(id); - setVisibleIds(_visibleIds); - return; - } - _visibleIds.add(id); - setVisibleIds(_visibleIds); - }; - return ( { layers.map(layer => { const isExpanded = expandedIds.has(layer.id); - const isVisible = visibleIds.has(layer.id); + const isVisible = layer.state.visible; return ( { sx={{ overflow: 'hidden', textOverflow: 'ellipsis', - whiteSpace: 'no-wrap', + whiteSpace: 'nowrap', }} > { layer.layers } @@ -116,8 +102,8 @@ export const LayersList = () => { toggleLayerVisibility(layer.id) } /> diff --git a/src/context/map-context.js b/src/context/map-context.js index da1f29a9..e185f0e7 100644 --- a/src/context/map-context.js +++ b/src/context/map-context.js @@ -5,9 +5,25 @@ export const LayersContext = createContext({}); export const useLayers = () => useContext(LayersContext); export const LayersProvider = ({ children }) => { - const [defaultModelLayers, setDefaultModelLayers] = useState([]); - const [filteredModelLayers, setFilteredModelLayers] = useState([]); - const [map, setMap] = useState(null); + const [defaultModelLayers, setDefaultModelLayers] = useState([]); + const [filteredModelLayers, setFilteredModelLayers] = useState([]); + const [map, setMap] = useState(null); + + const toggleLayerVisibility = id => { + const newLayers = [...defaultModelLayers]; + const index = newLayers.findIndex(l => l.id === id); + if (index === -1) { + new Error('Could not locate layer', id); + return; + } + const alteredLayer = newLayers[index]; + alteredLayer.state.visible = !alteredLayer.state.visible; + setDefaultModelLayers([ + ...newLayers.slice(0, index), + { ...alteredLayer }, + ...newLayers.slice(index + 1), + ]); + }; return ( { defaultModelLayers, setDefaultModelLayers, filteredModelLayers, - setFilteredModelLayers + setFilteredModelLayers, + toggleLayerVisibility, }} > {children}