changeset 1724:79a18eb1672b

import: POC import saveable
author Thomas Junk <thomas.junk@intevation.de>
date Wed, 09 Jan 2019 10:18:35 +0100
parents 50548a6df009
children b24a54c684bd
files client/src/components/importschedule/Importschedule.vue client/src/components/importschedule/Importscheduledetail.vue client/src/lib/errors.js client/src/store/imports.js
diffstat 4 files changed, 153 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/client/src/components/importschedule/Importschedule.vue	Tue Jan 08 20:11:12 2019 +0100
+++ b/client/src/components/importschedule/Importschedule.vue	Wed Jan 09 10:18:35 2019 +0100
@@ -114,7 +114,7 @@
   },
   methods: {
     getSchedules() {
-      this.$store.dispatch("imports/getSchedules").catch(error => {
+      this.$store.dispatch("imports/loadSchedules").catch(error => {
         const { status, data } = error.response;
         displayError({
           title: this.gettext("Backend Error"),
--- a/client/src/components/importschedule/Importscheduledetail.vue	Tue Jan 08 20:11:12 2019 +0100
+++ b/client/src/components/importschedule/Importscheduledetail.vue	Wed Jan 09 10:18:35 2019 +0100
@@ -81,6 +81,13 @@
                 </div>
               </div>
             </div>
+            <div v-if="!url" class="d-flex flex-row">
+              <small
+                ><translate class="text-danger"
+                  >Please enter a URL</translate
+                ></small
+              >
+            </div>
           </div>
           <div class="flex-column mt-3 w-100 mr-2">
             <div class="flex-row text-left">
@@ -164,6 +171,7 @@
               <div class="d-flex flex-row">
                 <h4 class="mt-auto mb-auto mr-2">{{ $options.EVERY }}</h4>
                 <select
+                  :disabled="!scheduled"
                   style="width: 130px;"
                   v-model="cronMode"
                   class="form-control"
@@ -179,6 +187,7 @@
                 <div v-if="cronMode == 'hour'" class="ml-1 d-flex flex-row">
                   <h4 class="mt-auto mb-auto">{{ $options.ON }}</h4>
                   <input
+                    :disabled="!scheduled"
                     v-model="minutes"
                     class="cronfield ml-1 mr-1 form-control"
                     type="number"
@@ -188,11 +197,13 @@
                 <div v-if="cronMode == 'day'" class="ml-1 d-flex flex-row">
                   <h4 class="mt-auto mb-auto">{{ $options.AT }}</h4>
                   <input
+                    :disabled="!scheduled"
                     v-model="hour"
                     class="cronfield ml-1 mr-1 form-control"
                     type="number"
                   />
                   <input
+                    :disabled="!scheduled"
                     v-model="minutes"
                     class="cronfield ml-1 mr-1 form-control"
                     type="number"
@@ -201,7 +212,11 @@
                 </div>
                 <div v-if="cronMode == 'week'" class="ml-1 d-flex flex-row">
                   <h4 class="ml-1 mr-1 mt-auto mb-auto">{{ $options.ON }}</h4>
-                  <select v-model="day" class="form-control">
+                  <select
+                    :disabled="!scheduled"
+                    v-model="day"
+                    class="form-control"
+                  >
                     <option
                       v-for="(option, key) in $options.DAYSOFWEEK"
                       :key="key"
@@ -211,11 +226,13 @@
                   </select>
                   <h4 class="ml-1 mt-auto mb-auto">{{ $options.AT }}</h4>
                   <input
+                    :disabled="!scheduled"
                     v-model="hour"
                     class="cronfield ml-1 mr-1 form-control"
                     type="number"
                   />
                   <input
+                    :disabled="!scheduled"
                     v-model="minutes"
                     class="cronfield ml-1 mr-1 form-control"
                     type="number"
@@ -224,17 +241,20 @@
                 <div v-if="cronMode == 'month'" class="ml-1 d-flex flex-row">
                   <h4 class="ml-1 mt-auto mb-auto">{{ $options.ON }}</h4>
                   <input
+                    :disabled="!scheduled"
                     v-model="dayOfMonth"
                     class="cronfield ml-1 mr-1 form-control"
                     type="number"
                   />
                   <h4 class="mt-auto mb-auto">{{ $options.AT }}</h4>
                   <input
+                    :disabled="!scheduled"
                     v-model="hour"
                     class="cronfield ml-1 mr-2 form-control"
                     type="number"
                   />
                   <input
+                    :disabled="!scheduled"
                     v-model="minutes"
                     class="cronfield ml-1 mr-2 form-control"
                     type="number"
@@ -244,12 +264,17 @@
                 <div v-if="cronMode == 'year'" class="ml-1 d-flex flex-row">
                   <h4 class="ml-1 mt-auto mb-auto">{{ $options.ON }}</h4>
                   <input
+                    :disabled="!scheduled"
                     v-model="dayOfMonth"
                     class="cronfield ml-1 mr-1 form-control"
                     type="number"
                   />
                   <h4 class="mt-auto mb-auto">{{ $options.OF }}</h4>
-                  <select v-model="month" class="ml-1 mr-1 form-control">
+                  <select
+                    :disabled="!scheduled"
+                    v-model="month"
+                    class="ml-1 mr-1 form-control"
+                  >
                     <option
                       v-for="(option, key) in $options.MONTHS"
                       :value="key"
@@ -259,11 +284,13 @@
                   </select>
                   <h4 class="mt-auto mb-auto">{{ $options.ON }}</h4>
                   <input
+                    :disabled="!scheduled"
                     v-model="hour"
                     class="cronfield ml-1 mr-1 form-control"
                     type="number"
                   />
                   <input
+                    :disabled="!scheduled"
                     v-model="minutes"
                     class="cronfield ml-1 mr-1 form-control"
                     type="number"
@@ -274,7 +301,12 @@
                 <h5 class="mt-auto mb-auto mr-2">
                   <translate>Cronstring</translate>
                 </h5>
-                <input class="form-control" :value="cronString" type="text" />
+                <input
+                  :disabled="!scheduled"
+                  class="form-control"
+                  :value="cronString"
+                  type="text"
+                />
               </div>
             </div>
           </div>
@@ -301,7 +333,20 @@
 </template>
 
 <script>
-import { IMPORTTYPES } from "@/store/imports.js";
+/* This is Free Software under GNU Affero General Public License v >= 3.0
+ * without warranty, see README.md and license for details.
+ *
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ * License-Filename: LICENSES/AGPL-3.0.txt
+ *
+ * Copyright (C) 2018 by via donau
+ *   – Österreichische Wasserstraßen-Gesellschaft mbH
+ * Software engineering by Intevation GmbH
+ *
+ * Author(s):
+ * Thomas Junk <thomas.junk@intevation.de>
+ */
+import { IMPORTTYPES, IMPORTTYPEKIND } from "@/store/imports.js";
 import { mapState } from "vuex";
 import { displayInfo, displayError } from "@/lib/errors.js";
 import app from "@/main.js";
@@ -310,6 +355,7 @@
   name: "importscheduledetail",
   data() {
     return {
+      id: null,
       importType: null,
       schedule: null,
       import_: null,
@@ -417,7 +463,11 @@
     }
   },
   methods: {
+    validateBottleneckfields() {
+      return !!this.url;
+    },
     initialize() {
+      this.id = this.currentSchedule.id;
       this.importType = this.currentSchedule.importType;
       this.schedule = this.currentSchedule.schedule;
       this.import_ = this.currentSchedule.import_;
@@ -449,6 +499,7 @@
     },
     triggerManualImport() {
       if (!this.triggerActive) return;
+      if (!this._imports) return;
       let data = {};
       if (this.import_ === this.$options.IMPORTTYPES.BOTTLENECK) {
         if (!this.url) return;
@@ -468,7 +519,7 @@
         .catch(error => {
           const { status, data } = error.response;
           displayError({
-            title: this.gettext("Backend Error"),
+            title: this.$gettext("Backend Error"),
             message: `${status}: ${data.message || data}`
           });
         })
@@ -477,10 +528,45 @@
         });
     },
     save() {
-      displayInfo({
-        title: this.$gettext("Import"),
-        message: this.$gettext("under construction")
-      });
+      let data = {};
+      if (!this.import_) return;
+      switch (this.import_) {
+        case this.$options.IMPORTTYPES.BOTTLENECK:
+          if (!this.validateBottleneckfields()) return;
+          data["url"] = this.url;
+          data["kind"] = IMPORTTYPEKIND[this.import_];
+          data["send_email"] = this.eMailNotification + "";
+          data["attributes"] = {
+            insecure: this.insecure + ""
+          };
+          break;
+        default:
+          break;
+      }
+      this.$store
+        .dispatch("imports/saveCurrentSchedule", data)
+        .then(response => {
+          const { id } = response.data;
+          displayInfo({
+            title: this.$gettext("Import"),
+            message: this.$gettext("Saved import: #") + id
+          });
+          this.closeDetailview();
+          this.$store.dispatch("imports/loadSchedules").catch(error => {
+            const { status, data } = error.response;
+            displayError({
+              title: this.gettext("Backend Error"),
+              message: `${status}: ${data.message || data}`
+            });
+          });
+        })
+        .catch(error => {
+          const { status, data } = error.response;
+          displayError({
+            title: this.$gettext("Backend Error"),
+            message: `${status}: ${data.message || data}`
+          });
+        });
     },
     closeDetailview() {
       this.$store.commit("imports/clearCurrentSchedule");
--- a/client/src/lib/errors.js	Tue Jan 08 20:11:12 2019 +0100
+++ b/client/src/lib/errors.js	Wed Jan 09 10:18:35 2019 +0100
@@ -26,8 +26,7 @@
 const displayError = ({ title, message, options }) => {
   if (!options) options = {};
   const mergedOptions = { ...displayOptions, ...options };
-  app.$snotify.info(message, title, mergedOptions);
-  app.$snotify.error(message, title, displayOptions);
+  app.$snotify.error(message, title, mergedOptions);
 };
 
 const displayInfo = ({ title, message, options }) => {
--- a/client/src/store/imports.js	Tue Jan 08 20:11:12 2019 +0100
+++ b/client/src/store/imports.js	Wed Jan 09 10:18:35 2019 +0100
@@ -31,8 +31,16 @@
   MONTHLY: "monthly"
 };
 
+const IMPORTTYPEKIND = {
+  bottleneck: "bn",
+  fairwayawailability: "fa",
+  gaugemeasurement: "ga",
+  waterwayaxis: "wx"
+};
+
 const initializeCurrentSchedule = () => {
   return {
+    id: null,
     importType: null,
     schedule: null,
     import_: null,
@@ -111,7 +119,53 @@
     }
   },
   actions: {
-    getSchedules({ commit }) {
+    deleteSchedule({ commit }, id) {
+      return new Promise((resolve, reject) => {
+        HTTP.delete("imports/config/" + id, {
+          headers: {
+            "X-Gemma-Auth": localStorage.getItem("token")
+          }
+        })
+          .then(response => {
+            resolve(response);
+          })
+          .catch(error => {
+            reject(error);
+          });
+      });
+    },
+    saveCurrentSchedule({ commit }, data) {
+      return new Promise((resolve, reject) => {
+        const create = true;
+        if (create) {
+          HTTP.post("imports/config", data, {
+            headers: {
+              "X-Gemma-Auth": localStorage.getItem("token")
+            }
+          })
+            .then(response => {
+              resolve(response);
+            })
+            .catch(error => {
+              reject(error);
+            });
+        } else {
+          const { id } = data;
+          HTTP.patch("imports/config/" + id, data, {
+            headers: {
+              "X-Gemma-Auth": localStorage.getItem("token")
+            }
+          })
+            .then(response => {
+              resolve(response);
+            })
+            .catch(error => {
+              reject(error);
+            });
+        }
+      });
+    },
+    loadSchedules({ commit }) {
       return new Promise((resolve, reject) => {
         HTTP.get("/imports/config", {
           headers: { "X-Gemma-Auth": localStorage.getItem("token") }
@@ -171,4 +225,4 @@
   }
 };
 
-export { imports, STATES, SCHEDULES, IMPORTTYPES };
+export { imports, STATES, SCHEDULES, IMPORTTYPES, IMPORTTYPEKIND };