add/remove publication

This commit is contained in:
Dmitry Belyaev 2021-06-03 17:34:01 +03:00
parent 1bb3bdb24a
commit 833995e739
Signed by: b4tman
GPG Key ID: 41A00BF15EA7E5F3
2 changed files with 56 additions and 48 deletions

View File

@ -100,18 +100,16 @@ export default {
methods: { methods: {
...mapMutations({ ...mapMutations({
//set_url: "setInfobaseURL", //set_url: "setInfobaseURL",
set_publication: "setInfobasePublication", //set_publication: "setInfobasePublication",
}), }),
...mapActions({ ...mapActions({
set_url: "updateInfobaseURL", set_url: "updateInfobaseURL",
}), }),
add_publication() { add_publication() {
let name = this.name; this.$store.dispatch("addInfobasePublication", this.name);
this.set_publication({ name, publicated: true });
}, },
remove_publication() { remove_publication() {
let name = this.name; this.$store.dispatch("removeInfobasePublication", this.name);
this.set_publication({ name, publicated: false });
}, },
start_edit_url() { start_edit_url() {
this.url_edit = true; this.url_edit = true;

View File

@ -1,7 +1,7 @@
import axios from "axios"; import axios from "axios";
//const api_base = "http://localhost:5000/api/v1"; // test1 //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({ const LoadingStatus = Object.freeze({
Loaded: { loading: false, error: false }, Loaded: { loading: false, error: false },
@ -9,11 +9,11 @@ const LoadingStatus = Object.freeze({
Error: { loading: false, error: true }, Error: { loading: false, error: true },
}); });
const new_infobase = (name) => ({ // const new_infobase = (name) => ({
name, // name,
url: "", // url: "",
publicated: false, // publicated: false,
}); // });
const infobaseByName = (state) => (name) => const infobaseByName = (state) => (name) =>
state.infobases.find((infobase) => name === infobase.name); state.infobases.find((infobase) => name === infobase.name);
@ -25,7 +25,7 @@ function setInfobaseLock(state, { name, lock }) {
state.locked_bases.push(name); state.locked_bases.push(name);
} else { } else {
const idx = state.locked_bases.findIndex((x) => name === x); 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) { unlockInfobase(state, name) {
setInfobaseLock(state, { name, lock: false }); setInfobaseLock(state, { name, lock: false });
}, },
setInfobase(state, { name, infobase_new }) { setInfobase(state, { name, infobase }) {
const idx = state.infobases.findIndex( const idx = state.infobases.findIndex((x) => name === x.name);
(infobase) => name === infobase.name
);
if (-1 === idx) { if (-1 === idx) {
state.infobases.push(infobase_new); state.infobases.push(infobase);
} else { } else {
if (null == infobase_new) { if (null == infobase) {
delete state.infobases[idx]; state.infobases.splice(idx, 1);
setInfobaseLock(state, { name, lock: false });
} else { } else {
state.infobases[idx] = infobase_new; infobase.publicated = true;
state.infobases[idx] = infobase;
} }
} }
}, },
@ -104,42 +104,24 @@ export default {
ctx.commit("setLoadingStatus", LoadingStatus.Error); ctx.commit("setLoadingStatus", LoadingStatus.Error);
} }
}, },
async fetchInfobaseAvailable(ctx, { name }) { async fetchInfobase(ctx, name) {
ctx.commit("lockInfobase", name); ctx.commit("lockInfobase", name);
try { try {
const list = await axios.get(`${api_base}/infobases-available`); const res = await axios.get(`${api_base}/infobases-all`);
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`);
if (!res) { if (!res) {
return; return;
} }
const infobase = res.data; const infobase = res.data.find((i) => name === i.name);
ctx.commit("setInfobase", { name, infobase }); ctx.commit("setInfobase", { name, infobase });
ctx.commit("unlockInfobase", name); ctx.commit("unlockInfobase", name);
} catch (err) { } catch (err) {
if (err.response && err.response.status === 404) { ctx.commit(
ctx.dispatch("fetchInfobaseAvailable", { name }); "setErrorMessage",
} `Ошибка получения информации о базе: ${name}`
);
ctx.commit("setInfobase", { name, infobase: null });
} finally {
ctx.commit("unlockInfobase", name);
} }
}, },
async updateInfobaseURL(ctx, { name, url }) { async updateInfobaseURL(ctx, { name, url }) {
@ -161,6 +143,34 @@ export default {
ctx.commit("unlockInfobase", name); 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: { getters: {
errorMessage(state) { errorMessage(state) {