From c32072c4f68c3719fa6b7e07786a1552fd9ace9f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timoth=C3=A9e?= <tpoussard@neogeo.fr>
Date: Fri, 3 Jun 2022 11:20:09 +0200
Subject: [PATCH] prevent undefined properties

---
 src/services/map-service.js | 103 +++++++++++++++++++-----------------
 1 file changed, 54 insertions(+), 49 deletions(-)

diff --git a/src/services/map-service.js b/src/services/map-service.js
index a55a86cf..be9485cd 100644
--- a/src/services/map-service.js
+++ b/src/services/map-service.js
@@ -379,75 +379,80 @@ const mapService = {
     const properties = feature.getProperties();
     let featureType;
     // GeoJSON
-    if(properties.feature_type){
+    if(properties && properties.feature_type){
       featureType = featureTypes
         .find((ft) => ft.slug === (properties.feature_type.slug || properties.feature_type));
     } else { //MVT
       featureType = featureTypes.find((x) => x.slug.split('-')[0] === '' + properties.feature_type_id);
     }
-    const color = this.retrieveFeatureColor(featureType, properties);
-    const colorValue =
+
+    if (featureType) {
+      const color = this.retrieveFeatureColor(featureType, properties);
+      const colorValue =
       color.value && color.value.length ?
         color.value : typeof color === 'string' && color.length ?
           color : '#000000';
 
-    const rgbaColor = asArray(colorValue);
-    //* set opacity from feature-type colors_style
-    rgbaColor[3] = this.retrieveFeatureOpacity(featureType, properties) || 0.5;//opacity
-    const hiddenStyle = new Style();
-
-    const defaultStyle = new Style(
-      {
-        image: new Circle({
-          fill: new Fill(
-            {
-              color: rgbaColor,
-            },
-          ),
+      const rgbaColor = asArray(colorValue);
+      //* set opacity from feature-type colors_style
+      rgbaColor[3] = this.retrieveFeatureOpacity(featureType, properties) || 0.5;//opacity
+
+      const defaultStyle = new Style(
+        {
+          image: new Circle({
+            fill: new Fill(
+              {
+                color: rgbaColor,
+              },
+            ),
+            stroke: new Stroke(
+              {
+                color: colorValue,
+                width: 2,
+              },
+            ),
+            radius: 5,
+          }),
           stroke: new Stroke(
             {
               color: colorValue,
               width: 2,
             },
           ),
-          radius: 5,
-        }),
-        stroke: new Stroke(
-          {
-            color: colorValue,
-            width: 2,
-          },
-        ),
-        fill: new Fill(
-          {
-            color: rgbaColor,
-          },
-        ),
-      },
-    );
-
-    // Filtre sur le feature type
-    if(formFilters){
-      if (formFilters.type && formFilters.type.selected) {
-        if (featureType.title !== formFilters.type.selected) {
-          return hiddenStyle;
+          fill: new Fill(
+            {
+              color: rgbaColor,
+            },
+          ),
+        },
+      );
+
+      const hiddenStyle = new Style(); // hide the feature to apply filters
+      // Filtre sur le feature type
+      if(formFilters){
+        if (formFilters.type && formFilters.type.selected) {
+          if (featureType.title !== formFilters.type.selected) {
+            return hiddenStyle;
+          }
         }
-      }
-      // Filtre sur le statut
-      if (formFilters.status && formFilters.status.selected.value) {
-        if (properties.status !== formFilters.status.selected.value) {
-          return hiddenStyle;
+        // Filtre sur le statut
+        if (formFilters.status && formFilters.status.selected.value) {
+          if (properties.status !== formFilters.status.selected.value) {
+            return hiddenStyle;
+          }
         }
-      }
-      // Filtre sur le titre
-      if (formFilters.title) {
-        if (!properties.title.toLowerCase().includes(formFilters.title.toLowerCase())) {
-          return hiddenStyle;
+        // Filtre sur le titre
+        if (formFilters.title) {
+          if (!properties.title.toLowerCase().includes(formFilters.title.toLowerCase())) {
+            return hiddenStyle;
+          }
         }
       }
+      return defaultStyle;
+    } else {
+      console.error('No corresponding featureType found.');
+      return;
     }
-
-    return defaultStyle;
   },
 
   addFeatures: function (features, filter, featureTypes, addToMap = true) {
-- 
GitLab