From 5d4d885577ecf47bf70b599029ddefee36aef720 Mon Sep 17 00:00:00 2001
From: leandro <leandroalmada86@gmail.com>
Date: Wed, 25 Aug 2021 15:40:57 +0200
Subject: [PATCH] update store feature type

---
 .../feature_type/FeatureTypeCustomForm.vue    |  6 +-
 src/store/modules/feature_type.js             | 94 ++++++++++---------
 src/views/feature_type/Feature_type_edit.vue  |  7 +-
 3 files changed, 61 insertions(+), 46 deletions(-)

diff --git a/src/components/feature_type/FeatureTypeCustomForm.vue b/src/components/feature_type/FeatureTypeCustomForm.vue
index 7d7ef4c4..aa7d5f2a 100644
--- a/src/components/feature_type/FeatureTypeCustomForm.vue
+++ b/src/components/feature_type/FeatureTypeCustomForm.vue
@@ -217,10 +217,10 @@ export default {
       this.$store.commit("feature_type/UPDATE_COLOR_STYLE");
     },
   },
-  beforeDestroy(){
-    this.$store.commit("feature_type/EMPTY_CUSTOM_FORM");
+  // beforeDestroy(){
+  //   this.$store.commit("feature_type/EMPTY_CUSTOM_FORM");
     
-  },
+  // },
   mounted() {
     for (let el in this.customForm) {
       if (el && this.form[el]) this.form[el].value = this.customForm[el].value;
diff --git a/src/store/modules/feature_type.js b/src/store/modules/feature_type.js
index 68916bf7..df91b49e 100644
--- a/src/store/modules/feature_type.js
+++ b/src/store/modules/feature_type.js
@@ -1,54 +1,62 @@
 import axios from "axios"
 import store from '@/store';
 
+export const RESET = 'RESET';
 
-const feature_type = {
-  namespaced: true,
-  state: {
-    form: {
-      colors_style: {
-        value: null,
-        options: [],
-        fields: [],
-      },
-      color: {
-        id_for_label: "couleur",
-        label: "Couleur",
-        field: {
-          max_length: 128, // ! Vérifier la valeur dans django
-        },
-        html_name: "couleur",
-        value: "#000000",
+
+const initialState = () => ({
+  form: {
+    colors_style: {
+      value: null,
+      options: [],
+      fields: [],
+    },
+    color: {
+      id_for_label: "couleur",
+      label: "Couleur",
+      field: {
+        max_length: 128, // ! Vérifier la valeur dans django
       },
-      title: {
-        errors: [],
-        id_for_label: "title",
-        label: "Titre",
-        field: {
-          max_length: 128, // ! Vérifier la valeur dans django
-        },
-        html_name: "title",
-        value: null,
+      html_name: "couleur",
+      value: "#000000",
+    },
+    title: {
+      errors: [],
+      id_for_label: "title",
+      label: "Titre",
+      field: {
+        max_length: 128, // ! Vérifier la valeur dans django
       },
-      geom_type: {
-        id_for_label: "geom_type",
-        label: "Type de géométrie",
-        field: {
-          choices: ["Ligne", "Point", "Polygone"],
-          max_length: 128, // ! Vérifier la valeur dans django
-        },
-        html_name: "geom_type",
-        value: "Point",
+      html_name: "title",
+      value: null,
+    },
+    geom_type: {
+      id_for_label: "geom_type",
+      label: "Type de géométrie",
+      field: {
+        choices: ["Ligne", "Point", "Polygone"],
+        max_length: 128, // ! Vérifier la valeur dans django
       },
+      html_name: "geom_type",
+      value: "Point",
     },
-    colorsStyleList: [],
-    customForms: [],
-    current_feature_type_slug: null,
-    feature_types: [],
   },
+  colorsStyleList: [],
+  customForms: [],
+  current_feature_type_slug: null,
+  feature_types: [],
+});
+
+const feature_type = {
+  namespaced: true,
+  state: initialState(),
+  
   mutations: {
-    EMPTY_CUSTOM_FORM(state) {
-      state.customForms = [];
+    [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;
@@ -83,7 +91,9 @@ const feature_type = {
     )
   },
   actions: {
-
+    [RESET]: ({ commit }) => {
+      commit("RESET_STATE");
+    },
     POST_FEATURE_TYPE({ state }) {
       const data = { form: state.form, formset: state.customForms }
       console.log("data", data)
diff --git a/src/views/feature_type/Feature_type_edit.vue b/src/views/feature_type/Feature_type_edit.vue
index c63a7e2f..da8320ba 100644
--- a/src/views/feature_type/Feature_type_edit.vue
+++ b/src/views/feature_type/Feature_type_edit.vue
@@ -122,7 +122,7 @@
 
 <script>
 import frag from "vue-frag";
-import { mapGetters, mapState } from "vuex";
+import { mapGetters, mapState, mapMutations } from "vuex";
 import Dropdown from "@/components/Dropdown.vue";
 import FeatureTypeCustomForm from "@/components/feature_type/FeatureTypeCustomForm.vue";
 
@@ -183,6 +183,7 @@ export default {
     },
   },
   methods: {
+    ...mapMutations('feature_type', ['RESET']),
     definePageType() {
       if (this.$router.history.current.name === "ajouter-type-signalement") {
         this.action = "create";
@@ -264,6 +265,10 @@ export default {
       }
     }
   },
+  beforeDestroy(){
+    this.RESET();
+    
+  },
   /* checkform() {
     let form_idx = $('#id_form-TOTAL_FORMS').val();
     for (var i=0; i <= form_idx;i++ ){
-- 
GitLab