From b8d0aefdb7d9bbffcf62f05d7fad11ad309d6d38 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timoth=C3=A9e=20Poussard?= <tpoussard@neogeo.fr>
Date: Fri, 3 Sep 2021 16:45:13 +0200
Subject: [PATCH] Add import features after feature_type created

---
 src/store/modules/feature_type.js             | 49 +++++++-----------
 .../feature_type/Feature_type_detail.vue      |  4 +-
 src/views/feature_type/Feature_type_edit.vue  | 51 ++++++++++++++-----
 src/views/project/Project_detail.vue          |  4 +-
 4 files changed, 63 insertions(+), 45 deletions(-)

diff --git a/src/store/modules/feature_type.js b/src/store/modules/feature_type.js
index 954199b3..510d9fc0 100644
--- a/src/store/modules/feature_type.js
+++ b/src/store/modules/feature_type.js
@@ -1,36 +1,18 @@
 import axios from "axios"
-//import store from '@/store';
-
-// export const RESET = 'RESET';
-
-
-/* const initialState = () => ({ // ? closure ?
-  form: null,
-  colorsStyleList: [],
-  customForms: [],
-  current_feature_type_slug: null,
-  feature_types: [],
-}); */
 
 const feature_type = {
   namespaced: true,
-  //state: initialState(),
   state: {
     form: null,
     colorsStyleList: [],
     customForms: [],
     current_feature_type_slug: null,
     feature_types: [],
+    filenameToImport: null,
     importFeatureTypeData: [],
   },
 
   mutations: {
-    /* [RESET]: (state) => {
-      const newState = initialState();
-      Object.keys(newState).forEach((key) => {
-        state[key] = newState[key];
-      });
-    }, */
     SET_FEATURE_TYPES(state, feature_types) {
       state.feature_types = feature_types;
     },
@@ -62,7 +44,10 @@ const feature_type = {
     },
     EMPTY_CUSTOM_FORMS(state) {
       state.customForms = [];
-    }
+    },
+    SET_FILE_NAME_TO_IMPORT(state, payload) {
+      state.filenameToImport = payload;
+    },
   },
   getters: {
     feature_type: state => state.feature_types.find(
@@ -70,10 +55,7 @@ const feature_type = {
     )
   },
   actions: {
-    /*  [RESET]: ({ commit }) => {
-       commit("RESET_STATE");
-     }, */
-    POST_FEATURE_TYPE({ state, rootGetters }) {
+    async POST_FEATURE_TYPE({ state, rootGetters }) {
       const data = {
         'title': state.form.title.value,
         'slug': rootGetters.project.slug,
@@ -93,24 +75,29 @@ const feature_type = {
         }),
         //'is_editable': true,
       }
-      console.log("data", data)
+      //console.log("data", data)
 
-      axios
+     return axios
         .post(`${process.env.VUE_APP_DJANGO_API_BASE}feature-types/`, data)
         .then((response) => {
-          console.log(response)
+          const feature_type_slug = response.data.slug;
+          console.log(feature_type_slug);
+          //console.log(response)
+          //console.log(response.data.slug)
+          return feature_type_slug;
         })
         .catch((error) => {
           console.error(error);
         });
     },
 
-    POST_FEATURES_FROM_GEOJSON({ dispatch }, payload) {
-      const { slug, feature_type_slug, filenameToImport } = payload
+    POST_FEATURES_FROM_GEOJSON({ state, dispatch }, payload) {
+      const { slug, feature_type_slug } = payload
+      console.log(slug, feature_type_slug)
 
-      if (filenameToImport.size > 0) {
+      if (state.filenameToImport.size > 0) {
         var formData = new FormData();
-        formData.append("json_file", filenameToImport);
+        formData.append("json_file", state.filenameToImport);
         let url =
           process.env.VUE_APP_URL_BASE +
           "projet/" + slug +
diff --git a/src/views/feature_type/Feature_type_detail.vue b/src/views/feature_type/Feature_type_detail.vue
index 311c5ee7..d95fdec7 100644
--- a/src/views/feature_type/Feature_type_detail.vue
+++ b/src/views/feature_type/Feature_type_detail.vue
@@ -230,7 +230,9 @@ export default {
     onFileChange(e) {
       var files = e.target.files || e.dataTransfer.files;
       if (!files.length) return;
-      this.filenameToImport = files[0];
+      this.filenameToImport = files[0]; // todo : remove this value from state as it stored
+      this.$store.commit("feature_type/SET_FILE_NAME_TO_IMPORT", this.filenameToImport)
+      //console.log(this.filenameToImport)
     },
 
     importGeoJson() {
diff --git a/src/views/feature_type/Feature_type_edit.vue b/src/views/feature_type/Feature_type_edit.vue
index f7a80d8e..5944387e 100644
--- a/src/views/feature_type/Feature_type_edit.vue
+++ b/src/views/feature_type/Feature_type_edit.vue
@@ -152,6 +152,8 @@ export default {
     FeatureTypeCustomForm,
   },
 
+  props: ["geojson", "filenameToImport"],
+
   data() {
     return {
       action: "create",
@@ -210,7 +212,6 @@ export default {
       ],
     };
   },
-  props: ["geojson"],
 
   computed: {
     ...mapGetters(["project"]),
@@ -256,8 +257,10 @@ export default {
       deep: true,
     },
   },
+
   methods: {
     ...mapMutations("feature_type", ["RESET"]),
+
     definePageType() {
       if (this.$router.history.current.name === "ajouter-type-signalement") {
         this.action = "create";
@@ -271,6 +274,7 @@ export default {
         this.action = "duplicate";
       }
     },
+
     addCustomForm(customForm) {
       this.dataKey += 1; // * increment counter for key in v-for
       let newCustomForm = {
@@ -281,6 +285,7 @@ export default {
       }
       this.$store.commit("feature_type/ADD_CUSTOM_FORM", newCustomForm); // * create an object with the counter in store
     },
+
     iniateColorsStyleFields() {
       const selected = this.colorsStyleList.find(
         (el) => el.label === this.selected_colors_style
@@ -296,6 +301,15 @@ export default {
       }
     },
 
+    updateStore() {
+      this.$store.commit("feature_type/UPDATE_FORM", {
+        color: this.form.color,
+        title: this.form.title,
+        geom_type: this.form.geom_type,
+        colors_style: this.form.colors_style,
+      });
+    },
+
     postFeatureType() {
       if (this.form.title.value) {
         this.form.title.errors = [];
@@ -309,18 +323,29 @@ export default {
           .scrollIntoView({ block: "end", inline: "nearest" });
       }
     },
-    postFeatureTypeNfeatures() {
+
+    async postFeatureTypeNfeatures() {
       // todo : post feature_type and create & post features
-      console.log("TODO : DO FUNCTION");
-      this.postFeatureType();
-    },
-    updateStore() {
-      this.$store.commit("feature_type/UPDATE_FORM", {
-        color: this.form.color,
-        title: this.form.title,
-        geom_type: this.form.geom_type,
-        colors_style: this.form.colors_style,
-      });
+      if (this.form.title.value) {
+        this.form.title.errors = [];
+        await this.$store
+          .dispatch("feature_type/POST_FEATURE_TYPE")
+          .then((feature_type_slug) => {
+            console.log(feature_type_slug);
+            this.$store.dispatch("feature_type/POST_FEATURES_FROM_GEOJSON", {
+              slug: this.$route.params.slug,
+              feature_type_slug: feature_type_slug,
+              //filenameToImport: this.filenameToImport,
+            });
+          });
+      } else if (
+        !this.form.title.errors.includes("Veuillez compléter ce champ.") // TODO : Gérer les autres champs
+      ) {
+        this.form.title.errors.push("Veuillez compléter ce champ.");
+        document
+          .getElementById("errorlist")
+          .scrollIntoView({ block: "end", inline: "nearest" });
+      }
     },
 
     // * Methodes for geojson import * //
@@ -330,6 +355,7 @@ export default {
         params: { geojson: this.jsonDict },
       });
     },
+
     translateLabel(value) {
       if (value == "LineString") {
         return "linestring";
@@ -403,6 +429,7 @@ export default {
 
     this.definePageType();
   },
+
   mounted() {
     if (this.action === "edit" || this.action === "duplicate") {
       for (const el in this.feature_type) {
diff --git a/src/views/project/Project_detail.vue b/src/views/project/Project_detail.vue
index 2c6f54c6..253b6ea2 100644
--- a/src/views/project/Project_detail.vue
+++ b/src/views/project/Project_detail.vue
@@ -505,7 +505,7 @@ export default {
     toNewFeatureType() {
       this.$router.push({
         name: "ajouter-type-signalement",
-        params: { geojson: this.geojsonImport },
+        params: { geojson: this.geojsonImport, filenameToImport: this.filenameToImport },
       });
     },
     onFileChange(e) {
@@ -520,6 +520,8 @@ export default {
           this.geojsonImport = JSON.parse(e.target.result);
         };
         fr.readAsText(this.filenameToImport);
+        //* stock filename to import features afterward
+        this.$store.commit("feature_type/SET_FILE_NAME_TO_IMPORT", this.filenameToImport)
       }
     },
     subsribeProject() {
-- 
GitLab