diff --git a/maps_dashboards/public/components/layer_control_panel/layer_control_panel.tsx b/maps_dashboards/public/components/layer_control_panel/layer_control_panel.tsx index c73963c0..da78afc9 100644 --- a/maps_dashboards/public/components/layer_control_panel/layer_control_panel.tsx +++ b/maps_dashboards/public/components/layer_control_panel/layer_control_panel.tsx @@ -17,6 +17,7 @@ import { EuiDraggable, EuiDroppable, euiDragDropReorder, + EuiConfirmModal, } from '@elastic/eui'; import { I18nProvider } from '@osd/i18n/react'; import { Map as Maplibre } from 'maplibre-gl'; @@ -68,6 +69,10 @@ const LayerControlPanel = memo(({ maplibreRef, setLayers, layers }: Props) => { const [addLayerId, setAddLayerId] = useState(''); const [isUpdatingLayerRender, setIsUpdatingLayerRender] = useState(false); const [isNewLayer, setIsNewLayer] = useState(false); + const [isDeleteLayerModalVisible, setIsDeleteLayerModalVisible] = useState(false); + const [selectedDeleteLayer, setSelectedDeleteLayer] = useState< + MapLayerSpecification | undefined + >(); useEffect(() => { if (!isUpdatingLayerRender && initialLayersLoaded) { @@ -207,6 +212,55 @@ const LayerControlPanel = memo(({ maplibreRef, setLayers, layers }: Props) => { const layersClone = [...layers]; return layersClone.reverse(); }; + const onLayerVisibilityChange = (layer: MapLayerSpecification) => { + if (layer.visibility === LAYER_VISIBILITY.VISIBLE) { + layer.visibility = LAYER_VISIBILITY.NONE; + setLayerVisibility(new Map(layerVisibility.set(layer.id, false))); + } else { + layer.visibility = LAYER_VISIBILITY.VISIBLE; + setLayerVisibility(new Map(layerVisibility.set(layer.id, true))); + } + layersFunctionMap[layer.type]?.hide(maplibreRef, layer); + }; + + const onDeleteLayerIconClick = (layer: MapLayerSpecification) => { + setSelectedDeleteLayer(layer); + setIsDeleteLayerModalVisible(true); + }; + + const onDeleteLayerConfirm = () => { + if (selectedDeleteLayer) { + layersFunctionMap[selectedDeleteLayer.type]?.remove(maplibreRef, selectedDeleteLayer); + removeLayer(selectedDeleteLayer.id); + setIsDeleteLayerModalVisible(false); + setSelectedDeleteLayer(undefined); + } + }; + + const onCancelDeleteLayer = () => { + setIsDeleteLayerModalVisible(false); + setSelectedDeleteLayer(undefined); + }; + + let deleteLayerModal; + if (isDeleteLayerModalVisible) { + deleteLayerModal = ( + +

+ Do you want to delete the layer {selectedDeleteLayer?.name}? This action + cannot be undone. +

+
+ ); + } if (isLayerControlVisible) { return ( @@ -281,20 +335,7 @@ const LayerControlPanel = memo(({ maplibreRef, setLayers, layers }: Props) => { : LAYER_PANEL_SHOW_LAYER_ICON } size="s" - onClick={() => { - if (layer.visibility === LAYER_VISIBILITY.VISIBLE) { - layer.visibility = LAYER_VISIBILITY.NONE; - setLayerVisibility( - new Map(layerVisibility.set(layer.id, false)) - ); - } else { - layer.visibility = LAYER_VISIBILITY.VISIBLE; - setLayerVisibility( - new Map(layerVisibility.set(layer.id, true)) - ); - } - layersFunctionMap[layer.type]?.hide(maplibreRef, layer); - }} + onClick={() => onLayerVisibilityChange(layer)} aria-label="Hide or show layer" color="text" /> @@ -306,10 +347,7 @@ const LayerControlPanel = memo(({ maplibreRef, setLayers, layers }: Props) => { { - layersFunctionMap[layer.type]?.remove(maplibreRef, layer); - removeLayer(layer.id); - }} + onClick={() => onDeleteLayerIconClick(layer)} aria-label="Delete layer" color="text" /> @@ -355,6 +393,7 @@ const LayerControlPanel = memo(({ maplibreRef, setLayers, layers }: Props) => { addLayer={addLayer} setIsNewLayer={setIsNewLayer} /> + {deleteLayerModal}