From f9f7dcba2de4c1ff5ed71881047d35a89d00f5c6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timoth=C3=A9e=20Poussard?= <tpoussard@neogeo.fr>
Date: Fri, 10 Sep 2021 10:49:48 +0200
Subject: [PATCH] add post project

---
 src/components/Dropdown.vue                   |  2 +-
 .../feature_type/FeatureTypeCustomForm.vue    |  2 +-
 src/store/index.js                            | 19 +++---
 src/store/modules/feature_type.js             | 15 +++--
 src/views/Index.vue                           |  2 +-
 src/views/project/Project_edit.vue            | 59 +++++++++++++------
 6 files changed, 62 insertions(+), 37 deletions(-)

diff --git a/src/components/Dropdown.vue b/src/components/Dropdown.vue
index 24b352ab..0ca8e093 100644
--- a/src/components/Dropdown.vue
+++ b/src/components/Dropdown.vue
@@ -47,7 +47,7 @@ export default {
   props: ["options", "selected", "disabled", "search", "placeholder"],
 
   computed: {
-    processedOptions: function () {
+    processedOptions: function () { //* si un objet {name, value}
       let options = this.options.map((el) =>
         el.constructor == Object ? el.name : el
       );
diff --git a/src/components/feature_type/FeatureTypeCustomForm.vue b/src/components/feature_type/FeatureTypeCustomForm.vue
index d66205e1..46c4d363 100644
--- a/src/components/feature_type/FeatureTypeCustomForm.vue
+++ b/src/components/feature_type/FeatureTypeCustomForm.vue
@@ -242,7 +242,7 @@ export default {
       };
       this.$store.commit("feature_type/UPDATE_CUSTOM_FORM", data);
     },
-    updateOptions() {
+    updateOptions() { // TODO : supprimer les espaces pour chaque option au début et à la fin QUE à la validation
       this.form.options.value[0] = this.form.options.value[0].replace(
         /\s*,\s*/gi,
         ","
diff --git a/src/store/index.js b/src/store/index.js
index 1fdb4094..b1e05892 100644
--- a/src/store/index.js
+++ b/src/store/index.js
@@ -45,6 +45,9 @@ export default new Vuex.Store({
     SET_PROJECTS(state, projects) {
       state.projects = projects;
     },
+    ADD_PROJECT(state, project) {
+      state.projects = [project, ...state.projects];
+    },
     SET_PROJECT_SLUG(state, slug) {
       state.project_slug = slug;
     },
@@ -178,23 +181,23 @@ export default new Vuex.Store({
         });
     },
 
-    /* GET_PROJECT_FEATURES({ commit }, project_slug) {
+    GET_PROJECT_FEATURE_TYPES({ commit }, project_slug) {
       axios
-        .get(`${DJANGO_API_BASE}projects/${project_slug}/feature`)
-        .then((response) => commit("feature/SET_FEATURES", response.data.features))
+        .get(`${DJANGO_API_BASE}projects/${project_slug}/feature-types`)
+        .then((response) => commit("feature_type/SET_FEATURE_TYPES", response.data.feature_types))
         .catch((error) => {
           throw error;
         });
-    }, */
+    },
 
-    GET_PROJECT_FEATURE_TYPES({ commit }, project_slug) {
+    /* GET_PROJECT_FEATURES({ commit }, project_slug) {
       axios
-        .get(`${DJANGO_API_BASE}projects/${project_slug}/feature-types`)
-        .then((response) => commit("feature_type/SET_FEATURE_TYPES", response.data.feature_types))
+        .get(`${DJANGO_API_BASE}projects/${project_slug}/feature`)
+        .then((response) => commit("feature/SET_FEATURES", response.data.features))
         .catch((error) => {
           throw error;
         });
-    },
+    }, */
     /* GET_PROJECT({ commit }, project_slug) {
       axios
         .get(`${DJANGO_API_BASE}projects/${project_slug}/project`)
diff --git a/src/store/modules/feature_type.js b/src/store/modules/feature_type.js
index cbf9dee3..ad9caf4f 100644
--- a/src/store/modules/feature_type.js
+++ b/src/store/modules/feature_type.js
@@ -85,8 +85,8 @@ const feature_type = {
               return { feature_type_slug, status };
             }
           })
-          .catch((/* error */) => {
-            //console.error(error);
+          .catch((error) => {
+            throw(error);
           });
       } else if (requestType === "put") {
         return axios
@@ -98,8 +98,8 @@ const feature_type = {
               return { feature_type_slug, status };
             }
           })
-          .catch((/* error */) => {
-            //console.error(error);
+          .catch((error) => {
+            throw(error);
           });
       }
     },
@@ -123,13 +123,12 @@ const feature_type = {
           .then((response) => {
             if (response && response.status === 200) {
               dispatch("GET_IMPORTS", feature_type_slug);
-              // TODO : RELOAD DERNIER SIGNALEMENTS
+              // TODO : reload dernier signalements
             }
             return response
           })
-          .catch((/* err */) => {
-            // TODO : HANDLER ERROR
-            //console.log(err);
+          .catch((error) => {
+            throw(error);
           });
       }
     },
diff --git a/src/views/Index.vue b/src/views/Index.vue
index 585762e8..ef43ddc8 100644
--- a/src/views/Index.vue
+++ b/src/views/Index.vue
@@ -38,7 +38,7 @@
           <!-- // ? récupérer l'image sur serveur front (et non back) ? -->
           <img
             :src="
-              project.thumbnail.includes('default')
+              !project.thumbnail
                 ? require('@/assets/img/default.png')
                 : DJANGO_BASE_URL + project.thumbnail
             "
diff --git a/src/views/project/Project_edit.vue b/src/views/project/Project_edit.vue
index f40bf7d9..1ed31528 100644
--- a/src/views/project/Project_edit.vue
+++ b/src/views/project/Project_edit.vue
@@ -106,8 +106,8 @@
             >Visibilité des signalements publiés</label
           >
           <Dropdown
-            :options="access_level_pub_feature_choices"
-            :selected="form.access_level_pub_feature"
+            :options="level_permissions_choices"
+            :selected="form.access_level_pub_feature.name"
             :selection.sync="form.access_level_pub_feature"
           />
         </div>
@@ -116,8 +116,8 @@
             Visibilité des signalements archivés
           </label>
           <Dropdown
-            :options="access_level_arch_feature_choices"
-            :selected="form.access_level_arch_feature"
+            :options="level_permissions_choices"
+            :selected="form.access_level_arch_feature.name"
             :selection.sync="form.access_level_arch_feature"
           />
         </div>
@@ -159,6 +159,7 @@
 </template>
 
 <script>
+const axios = require("axios");
 import Dropdown from "@/components/Dropdown.vue";
 import { mapGetters } from "vuex";
 
@@ -172,15 +173,10 @@ export default {
   data() {
     return {
       action: "create",
-      access_level_pub_feature_choices: [
-        "Utilisateur anonyme",
-        "Utilisateur connecté",
-        "Contributeur",
-      ],
-      access_level_arch_feature_choices: [
-        "Utilisateur anonyme",
-        "Utilisateur connecté",
-        "Contributeur",
+      level_permissions_choices: [
+        {name: "Utilisateur anonyme", value: "anonymous"},
+        {name: "Utilisateur connecté", value: "logged_user"},
+        {name: "Contributeur", value: "contributor"},
       ],
       form: {
         title: "",
@@ -192,8 +188,8 @@ export default {
         thumbnail: require("@/assets/img/default.png"), // todo : utiliser l'image par défaut
         thumbnail_name: "", // todo: delete after getting image in jpg or png instead of data64 (require post to django)
         creator: null,
-        access_level_pub_feature: "",
-        access_level_arch_feature: "",
+        access_level_pub_feature: {name: "", value: ""},
+        access_level_arch_feature: {name: "", value: ""},
         archive_feature: 0,
         delete_feature: 0,
         nb_features: 0,
@@ -255,9 +251,36 @@ export default {
       this.$router.push(routerHistory[routerHistory.length - 1] || "/");
     },
 
-    postForm() {
-      /* const data = JSON.stringify(this.project);
-      console.log("POST this data : ", data); */
+    async postForm() {
+      //const data = JSON.stringify(this.project);
+      //* create project
+      // todo: check form
+      const projectData = {
+        title: this.form.title,
+        description: this.form.description,
+        access_level_arch_feature: this.form.access_level_arch_feature.value,
+        access_level_pub_feature: this.form.access_level_pub_feature.value,
+        archive_feature: this.form.archive_feature,
+        delete_feature: this.form.delete_feature,
+        is_project_type: this.form.is_project_type,
+        moderation: this.form.moderation,
+      };
+
+      await axios
+        .post(`${process.env.VUE_APP_DJANGO_API_BASE}projects/`, projectData)
+        .then((response) => {
+          console.log(response);
+          if (response && response.status === 201) {
+            this.$store.commit("ADD_PROJECT", response.data)
+            this.$router.push("/")
+            // todo : send thumbnail after
+            //postProjectThumbnail()
+          }
+        })
+        .catch((error) => {
+          throw error;
+        });
+      console.log("POST this data : ", projectData);
     },
   },
   created() {
-- 
GitLab