From 2679cdc365201428c0c983b79e371ebb679cbc1b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timoth=C3=A9e?= <tpoussard@neogeo.fr>
Date: Wed, 3 Aug 2022 18:01:45 +0200
Subject: [PATCH] remove feature_id in csv imported file

---
 .../Project/Detail/ProjectFeatureTypes.vue    | 43 +++++++++++--------
 1 file changed, 25 insertions(+), 18 deletions(-)

diff --git a/src/components/Project/Detail/ProjectFeatureTypes.vue b/src/components/Project/Detail/ProjectFeatureTypes.vue
index 7cb79685..69c17132 100644
--- a/src/components/Project/Detail/ProjectFeatureTypes.vue
+++ b/src/components/Project/Detail/ProjectFeatureTypes.vue
@@ -633,10 +633,10 @@ export default {
         try {
           fr.readAsText(this.csvFileToImport);
           fr.onloadend = () => {
-
+            let content = fr.result;
             // Find csv delimiter
-            const commaDelimited = fr.result.split('\n')[0].includes(',');
-            const semicolonDelimited = fr.result.split('\n')[0].includes(';');
+            const commaDelimited = content.split('\n')[0].includes(',');
+            const semicolonDelimited = content.split('\n')[0].includes(';');
             const delimiter = commaDelimited && !semicolonDelimited ? ',' : semicolonDelimited ?  ';' : false;
 
             if ((commaDelimited && semicolonDelimited) || !delimiter) {
@@ -644,32 +644,39 @@ export default {
               this.featureTypeImporting = false;
               return;
             }
-
             // Check if file contains 'lat' and 'long' fields
-            const headersLine =
-              fr.result
-                .split('\n')[0]
-                .split(delimiter)
-                .map(el => {
-                  return el.replace('\r', '');
-                })
-                .filter(el => {
-                  return el === 'lat' || el === 'lon';
-                });
-
+            const headers = content
+              .split('\n')[0]
+              .split(delimiter)
+              .map(el => {
+                return el.replace('\r', '');
+              });
+            const headersCoord = headers.filter(el => {
+              return el === 'lat' || el === 'lon';
+            });
             // Look for 2 decimal fields in first line of csv
             // corresponding to lon and lat
             const sampleLine =
-              fr.result
+              content
                 .split('\n')[1]
                 .split(delimiter)
                 .map(el => {
                   return !isNaN(el) && el.indexOf('.') !== -1;
                 })
                 .filter(Boolean);
-            if (sampleLine.length > 1 && headersLine.length === 2) {
+            if (sampleLine.length > 1 && headersCoord.length === 2) {
               this.csvError = null;
-              this.csvImport = csvToJson(fr.result, delimiter);
+              //* remove feature_id if found in headers
+              const featureIdIndex = headers.indexOf('feature_id');
+              if (featureIdIndex > -1) {
+                content = content.split('\n')
+                  .reduce((csvString, rowStr) => {
+                    let rowArray = rowStr.split(delimiter);
+                    rowArray.splice(featureIdIndex, 1); // remove feature_id in header and features
+                    return csvString += rowArray.join(delimiter) + '\n';
+                  }, '');
+              }
+              this.csvImport = csvToJson(content, delimiter);
               this.featureTypeImporting = false;
               //* stock filename to import features afterward
               this.SET_FILE_TO_IMPORT(this.csvFileToImport);
-- 
GitLab