From 1020232cb1dba35b1d94f756dfe65d2cb61b6be9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timoth=C3=A9e=20Poussard?= <tpoussard@neogeo.fr>
Date: Tue, 28 Sep 2021 14:33:38 +0200
Subject: [PATCH] make choices objects or dropdown & add put linked_feature

---
 src/components/feature/FeatureLinkedForm.vue | 20 +++++++++++++-----
 src/store/modules/feature.js                 | 22 ++++++++++++++++++--
 src/views/feature/Feature_detail.vue         |  2 +-
 3 files changed, 36 insertions(+), 8 deletions(-)

diff --git a/src/components/feature/FeatureLinkedForm.vue b/src/components/feature/FeatureLinkedForm.vue
index 36365296..6284d182 100644
--- a/src/components/feature/FeatureLinkedForm.vue
+++ b/src/components/feature/FeatureLinkedForm.vue
@@ -62,11 +62,19 @@ export default {
           errors: null,
           id_for_label: "relation_type",
           field: {
-            choices: ["Doublon", "Remplace", "Est remplacé par", "Dépend de"],
+            choices: [
+              { name: "Doublon", value: "doublon" },
+              { name: "Remplace", value: "remplace" },
+              { name: "Est remplacé par", value: "est_remplace_par" },
+              { name: "Dépend de", value: "depend_de" },
+            ],
           },
           html_name: "relation_type",
           label: "Type de liaison",
-          value: "Doublon",
+          value: {
+            name: "Doublon",
+            value: "doublon",
+          },
         },
         feature_to: {
           errors: null,
@@ -106,7 +114,7 @@ export default {
     selected_relation_type: {
       // getter
       get() {
-        return this.form.relation_type.value;
+        return this.form.relation_type.value.name;
       },
       // setter
       set(newValue) {
@@ -150,8 +158,10 @@ export default {
     updateStore() {
       this.$store.commit("feature/UPDATE_LINKED_FORM", {
         dataKey: this.linkedForm.dataKey,
-        relation_type: this.form.relation_type.value,
-        feature_to: this.form.feature_to.value.value,
+        relation_type: this.form.relation_type.value.value,
+        feature_to: {
+          feature_id: this.form.feature_to.value.value,
+        },
       });
     },
 
diff --git a/src/store/modules/feature.js b/src/store/modules/feature.js
index d57841b8..01d81266 100644
--- a/src/store/modules/feature.js
+++ b/src/store/modules/feature.js
@@ -106,6 +106,8 @@ const feature = {
           .put(`${rootState.configuration.VUE_APP_DJANGO_API_BASE}features/${state.form.feature_id}/`, geojson)
           .then((response) => {
             if (response.status === 200 && response.data) {
+              dispatch("SEND_ATTACHMENTS", response.data.id)
+              dispatch("PUT_LINKED_FEATURES", response.data.id)
               router.push({
                 name: "details-signalement",
                 params: {
@@ -119,12 +121,13 @@ const feature = {
           .catch((error) => {
             throw error;
           });
-      } else {
-        axios
+        } else {
+          axios
           .post(`${rootState.configuration.VUE_APP_DJANGO_API_BASE}features/`, geojson)
           .then((response) => {
             if (response.status === 201 && response.data) {
               dispatch("SEND_ATTACHMENTS", response.data.id)
+              dispatch("PUT_LINKED_FEATURES", response.data.id)
               router.push({
                 name: "details-signalement",
                 params: {
@@ -143,6 +146,7 @@ const feature = {
 
     SEND_ATTACHMENTS({ state, rootState }, featureId) {
       for (let attacht of state.attachmentFormset) {
+        if (!attacht.fileToImport) return; //* if no new file imported abort, until beeing able to do PUT
         let formdata = new FormData();
         formdata.append("file", attacht.fileToImport, attacht.fileToImport.name);
         const data = {
@@ -164,6 +168,20 @@ const feature = {
       }
     },
 
+    PUT_LINKED_FEATURES({ state, rootState }, featureId) {
+      axios
+        .put(`${rootState.configuration.VUE_APP_DJANGO_API_BASE}features/${featureId}/feature-links/`, state.linkedFormset)
+        .then((response) => {
+          if (response.status === 200 && response.data) {
+            console.log(response, response.data)
+            return "La relation a bien été ajouté"
+          }
+        })
+        .catch((error) => {
+          throw error;
+        });
+    },
+
     /*     GET_FEATURE_LINK({ commit }, featureId) {
           featureAPI
             .getlinked_features(featureId)
diff --git a/src/views/feature/Feature_detail.vue b/src/views/feature/Feature_detail.vue
index e24f5574..ddafad1d 100644
--- a/src/views/feature/Feature_detail.vue
+++ b/src/views/feature/Feature_detail.vue
@@ -155,7 +155,7 @@
                   }"
                   >{{ link.feature_to.title }}</router-link
                 >
-                ({{ link.feature_to.creator }} -
+                ({{ link.feature_to.display_creator }} -
                 {{ link.feature_to.created_on }})
               </td>
             </tr>
-- 
GitLab