From 833995e739a65cb744205770bc05aa2d3f7b268b Mon Sep 17 00:00:00 2001
From: Dmitry <b4tm4n@mail.ru>
Date: Thu, 3 Jun 2021 17:34:01 +0300
Subject: [PATCH] add/remove publication

---
 src/components/InfobaseListItem.vue |  8 +--
 src/store/modules/infobases.js      | 96 ++++++++++++++++-------------
 2 files changed, 56 insertions(+), 48 deletions(-)

diff --git a/src/components/InfobaseListItem.vue b/src/components/InfobaseListItem.vue
index b97c88a..c1bd0e6 100644
--- a/src/components/InfobaseListItem.vue
+++ b/src/components/InfobaseListItem.vue
@@ -100,18 +100,16 @@ export default {
   methods: {
     ...mapMutations({
       //set_url: "setInfobaseURL",
-      set_publication: "setInfobasePublication",
+      //set_publication: "setInfobasePublication",
     }),
     ...mapActions({
       set_url: "updateInfobaseURL",
     }),
     add_publication() {
-      let name = this.name;
-      this.set_publication({ name, publicated: true });
+      this.$store.dispatch("addInfobasePublication", this.name);
     },
     remove_publication() {
-      let name = this.name;
-      this.set_publication({ name, publicated: false });
+      this.$store.dispatch("removeInfobasePublication", this.name);
     },
     start_edit_url() {
       this.url_edit = true;
diff --git a/src/store/modules/infobases.js b/src/store/modules/infobases.js
index bf1d1e3..208c69b 100644
--- a/src/store/modules/infobases.js
+++ b/src/store/modules/infobases.js
@@ -1,7 +1,7 @@
 import axios from "axios";
 
 //const api_base = "http://localhost:5000/api/v1"; // test1
-const api_base = "http://localhost:17653/api/v1"; // test2 - mock
+const api_base = "http://localhost:17653"; // test2 - mock
 
 const LoadingStatus = Object.freeze({
   Loaded: { loading: false, error: false },
@@ -9,11 +9,11 @@ const LoadingStatus = Object.freeze({
   Error: { loading: false, error: true },
 });
 
-const new_infobase = (name) => ({
-  name,
-  url: "",
-  publicated: false,
-});
+// const new_infobase = (name) => ({
+//   name,
+//   url: "",
+//   publicated: false,
+// });
 
 const infobaseByName = (state) => (name) =>
   state.infobases.find((infobase) => name === infobase.name);
@@ -25,7 +25,7 @@ function setInfobaseLock(state, { name, lock }) {
     state.locked_bases.push(name);
   } else {
     const idx = state.locked_bases.findIndex((x) => name === x);
-    delete state.locked_bases[idx];
+    state.locked_bases.splice(idx, 1);
   }
 }
 
@@ -59,17 +59,17 @@ export default {
     unlockInfobase(state, name) {
       setInfobaseLock(state, { name, lock: false });
     },
-    setInfobase(state, { name, infobase_new }) {
-      const idx = state.infobases.findIndex(
-        (infobase) => name === infobase.name
-      );
+    setInfobase(state, { name, infobase }) {
+      const idx = state.infobases.findIndex((x) => name === x.name);
       if (-1 === idx) {
-        state.infobases.push(infobase_new);
+        state.infobases.push(infobase);
       } else {
-        if (null == infobase_new) {
-          delete state.infobases[idx];
+        if (null == infobase) {
+          state.infobases.splice(idx, 1);
+          setInfobaseLock(state, { name, lock: false });
         } else {
-          state.infobases[idx] = infobase_new;
+          infobase.publicated = true;
+          state.infobases[idx] = infobase;
         }
       }
     },
@@ -104,42 +104,24 @@ export default {
         ctx.commit("setLoadingStatus", LoadingStatus.Error);
       }
     },
-    async fetchInfobaseAvailable(ctx, { name }) {
+    async fetchInfobase(ctx, name) {
       ctx.commit("lockInfobase", name);
       try {
-        const list = await axios.get(`${api_base}/infobases-available`);
-        if (!list) {
-          return;
-        }
-        if (!Array.isArray(list)) {
-          return;
-        }
-        const available = name in list;
-        let infobase = null;
-        if (available) {
-          infobase = new_infobase(name);
-        }
-        ctx.commit("setInfobase", { name, infobase });
-      } catch (err) {
-        console.error(err);
-      } finally {
-        ctx.commit("unlockInfobase", name);
-      }
-    },
-    async fetchPublication(ctx, { name }) {
-      ctx.commit("lockInfobase", name);
-      try {
-        const res = await axios.get(`${api_base}/publications`);
+        const res = await axios.get(`${api_base}/infobases-all`);
         if (!res) {
           return;
         }
-        const infobase = res.data;
+        const infobase = res.data.find((i) => name === i.name);
         ctx.commit("setInfobase", { name, infobase });
         ctx.commit("unlockInfobase", name);
       } catch (err) {
-        if (err.response && err.response.status === 404) {
-          ctx.dispatch("fetchInfobaseAvailable", { name });
-        }
+        ctx.commit(
+          "setErrorMessage",
+          `Ошибка получения информации о базе: ${name}`
+        );
+        ctx.commit("setInfobase", { name, infobase: null });
+      } finally {
+        ctx.commit("unlockInfobase", name);
       }
     },
     async updateInfobaseURL(ctx, { name, url }) {
@@ -161,6 +143,34 @@ export default {
         ctx.commit("unlockInfobase", name);
       }
     },
+    async addInfobasePublication(ctx, name) {
+      const infobase = ctx.getters.getInfobaseByName(name);
+      if (infobase.publicated) return;
+
+      ctx.commit("lockInfobase", name);
+      try {
+        await axios.put(`${api_base}/publications`, { id:name, name });
+        ctx.commit("setErrorMessage", "");
+        ctx.dispatch("fetchInfobase", name);
+      } catch (err) {
+        ctx.commit("setErrorMessage", `Ошибка публикации базы ${name}`);
+        ctx.commit("unlockInfobase", name);
+      }
+    },
+    async removeInfobasePublication(ctx, name) {
+      const infobase = ctx.getters.getInfobaseByName(name);
+      if (!infobase.publicated) return;
+
+      ctx.commit("lockInfobase", name);
+      try {
+        await axios.delete(`${api_base}/publications/${name}`);
+        ctx.commit("setErrorMessage", "");
+        ctx.dispatch("fetchInfobase", name);
+      } catch (err) {
+        ctx.commit("setErrorMessage", `Ошибка отмены публикации базы ${name}`);
+        ctx.commit("unlockInfobase", name);
+      }
+    },
   },
   getters: {
     errorMessage(state) {