From b49bde719bddeb4a9e5237b2780109baa9e03928 Mon Sep 17 00:00:00 2001
From: Sam McAlilly <38969506+smcalilly@users.noreply.github.com>
Date: Mon, 6 Nov 2023 09:24:15 -0600
Subject: [PATCH] check for existence of key before using it (#281)
* check for existance of key before using it
* use cleaner one liner
* even more one liner
---
.../js/components/maps/SelectAssetsMap.js | 89 +++++++++----------
1 file changed, 43 insertions(+), 46 deletions(-)
diff --git a/asset_dashboard/static/js/components/maps/SelectAssetsMap.js b/asset_dashboard/static/js/components/maps/SelectAssetsMap.js
index da97e5c..7354299 100644
--- a/asset_dashboard/static/js/components/maps/SelectAssetsMap.js
+++ b/asset_dashboard/static/js/components/maps/SelectAssetsMap.js
@@ -70,10 +70,7 @@ function SelectAssetsMap(props) {
}
function saveGeometries() {
- let existingIDs = []
- existingGeoms.features.forEach(geom => {
- existingIDs.push(geom.properties.asset_id)
- })
+ const existingIDs = existingGeoms?.features ? existingGeoms.features.map(geom => geom.properties.asset_id) : []
let data = geomsToSave['features']
.map(feature => {
@@ -96,7 +93,7 @@ function SelectAssetsMap(props) {
setAjaxMessage({text: 'All selected assets already exist', tag: 'danger'})
return
}
-
+
setIsSavingAssets(true)
fetch('/local-assets/', {
@@ -159,11 +156,11 @@ function SelectAssetsMap(props) {
}).then((response) => response.json())
.then((data) => {
setIsLoading(false)
-
+
if (data.features.length == 0) {
setAjaxMessage({text: 'No assets found with search query.', tag: 'info'})
}
-
+
setSearchGeoms(data)
})
.catch(error => {
@@ -187,17 +184,17 @@ function SelectAssetsMap(props) {
1. By clicking on a row in the search table. This loads up the singleFeature to save.
2. By clicking on a single geom in the map. This also loads up the singleFeature to save.
3. By selecting multipleFeatures via the GeometrySelector component, which loads multipleFeatures to save.
- If a user happens to select multipleFeatures and a singleFeature in one combination of interactions,
+ If a user happens to select multipleFeatures and a singleFeature in one combination of interactions,
then we need to save those pieces of states together. We also need to be able to save if they've been
- selected in separate interactions, like if a user only selects a single geometry features and clicks to
+ selected in separate interactions, like if a user only selects a single geometry features and clicks to
save, or if they use the draw tool to select multiple features.
-
- In other words, we need to be able to handle when a user selects multipleFeatures and a singleFeature
+
+ In other words, we need to be able to handle when a user selects multipleFeatures and a singleFeature
in the same interaction, without first pressing the "save" button when they switch between two types
- of interaction.
-
- To handle these variations, useEffect listens to changes to the singleFeature and multipleFeature
- variables. When either of those variables change, we reset the geomsToSave variable. The data
+ of interaction.
+
+ To handle these variations, useEffect listens to changes to the singleFeature and multipleFeature
+ variables. When either of those variables change, we reset the geomsToSave variable. The data
in geomsToSave is what gets POSTed to the API.
*/
useEffect(() => {
@@ -217,13 +214,13 @@ function SelectAssetsMap(props) {
const onEachSearchFeature = useCallback(
(feature, layer) => {
bindPopup(feature, layer)
-
+
layer.on({
'click': onSearchAssetClick,
'popupclose': () => {
setSingleFeature(null)
-
- // Reset the fillColor because the layer changed color
+
+ // Reset the fillColor because the layer changed color
// whenever it was clicked on.
if (layer.setStyle) {
layer.setStyle({
@@ -241,15 +238,15 @@ function SelectAssetsMap(props) {
bindPopup(feature, layer)
}, [existingGeoms]
)
-
+
return (
<>
- {ajaxMessage
- ?