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 - ? + /> : null }
@@ -258,12 +255,12 @@ function SelectAssetsMap(props) {
- setSearchText(e.target.value)} value={searchText} - className='form-control' - aria-label='Search for assets' + className='form-control' + aria-label='Search for assets' placeholder='Search for assets' />
@@ -274,7 +271,7 @@ function SelectAssetsMap(props) { > -
{isLoading ? 'Loading...' : null} - {searchGeoms && + {searchGeoms &&

{props.phase_name}

- {geomsToSave + {geomsToSave ? -
- +
- {searchGeoms && + {searchGeoms && <> - - @@ -345,9 +342,9 @@ function SelectAssetsMap(props) { {singleFeature && } } - {existingGeoms && + {existingGeoms && @@ -357,12 +354,12 @@ function SelectAssetsMap(props) {
- +

Phase Assets

{ - existingGeoms + existingGeoms ? Phase has no assets.

}
- +
@@ -378,9 +375,9 @@ function SelectAssetsMap(props) {

{props.phase_name}

-