180 lines
5.0 KiB
JavaScript
180 lines
5.0 KiB
JavaScript
import axios from "axios";
|
|
|
|
//const api_base = "http://localhost:5000/api/v1"; // test1
|
|
const api_base = "http://localhost:17653/api/v1"; // test2 - mock
|
|
|
|
const LoadingStatus = Object.freeze({
|
|
Loaded: { loading: false, error: false },
|
|
Loading: { loading: true, error: false },
|
|
Error: { loading: false, error: true },
|
|
});
|
|
|
|
let new_infobase = (name) => ({
|
|
name,
|
|
url: "",
|
|
publicated: false,
|
|
});
|
|
|
|
function setInfobaseLock(state, { name, lock }) {
|
|
let locked = state.locked_bases.includes(name);
|
|
if (locked === lock) return;
|
|
if (lock) {
|
|
state.locked_bases.push(name);
|
|
} else {
|
|
let idx = state.locked_bases.findIndex((x) => name === x);
|
|
delete state.locked_bases[idx];
|
|
}
|
|
}
|
|
|
|
export default {
|
|
state: {
|
|
publicated: [],
|
|
available: [],
|
|
infobases: [],
|
|
error_message: "",
|
|
loading_error: false,
|
|
is_loading: false,
|
|
locked_bases: [],
|
|
},
|
|
mutations: {
|
|
setLoadingStatus(state, { loading, error }) {
|
|
state.is_loading = loading && !error;
|
|
state.loading_error = error;
|
|
},
|
|
setInfobases(state, infobases) {
|
|
state.infobases = infobases;
|
|
},
|
|
setErrorMessage(state, message) {
|
|
state.error_message = message;
|
|
},
|
|
setInfobaseLock(state, { name, lock }) {
|
|
setInfobaseLock(state, { name, lock });
|
|
},
|
|
lockInfobase(state, name) {
|
|
setInfobaseLock(state, { name, lock: true });
|
|
},
|
|
unlockInfobase(state, name) {
|
|
setInfobaseLock(state, { name, lock: false });
|
|
},
|
|
setInfobase(state, { name, infobase_new }) {
|
|
let idx = state.infobases.findIndex((infobase) => name === infobase.name);
|
|
if (-1 === idx) {
|
|
state.infobases.push(infobase_new);
|
|
} else {
|
|
if (null == infobase_new) {
|
|
delete state.infobases[idx];
|
|
} else {
|
|
state.infobases[idx] = infobase_new;
|
|
}
|
|
}
|
|
},
|
|
setInfobaseURL(state, { name, url }) {
|
|
let infobase = state.infobases.find((infobase) => name === infobase.name);
|
|
if (infobase === undefined) return;
|
|
infobase.url = url;
|
|
},
|
|
setInfobasePublication(state, { name, publicated }) {
|
|
let infobase = state.infobases.find((infobase) => name === infobase.name);
|
|
if (infobase === undefined) return;
|
|
infobase.publicated = publicated;
|
|
},
|
|
},
|
|
actions: {
|
|
async fetchInfobases(ctx) {
|
|
ctx.commit("setLoadingStatus", LoadingStatus.Loading);
|
|
try {
|
|
const res = await axios.get(`${api_base}/infobases-all`);
|
|
if (!res) {
|
|
ctx.commit("setLoadingStatus", LoadingStatus.Error);
|
|
return;
|
|
}
|
|
const infobases = res.data;
|
|
if (!Array.isArray(infobases)) {
|
|
ctx.commit("setLoadingStatus", LoadingStatus.Error);
|
|
return;
|
|
}
|
|
ctx.commit("setInfobases", infobases);
|
|
ctx.commit("setLoadingStatus", LoadingStatus.Loaded);
|
|
} catch (err) {
|
|
ctx.commit("setLoadingStatus", LoadingStatus.Error);
|
|
}
|
|
},
|
|
async fetchInfobaseAvailable(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`);
|
|
if (!res) {
|
|
return;
|
|
}
|
|
const infobase = res.data;
|
|
ctx.commit("setInfobase", { name, infobase });
|
|
ctx.commit("unlockInfobase", name);
|
|
} catch (err) {
|
|
if (err.response && err.response.status === 404) {
|
|
ctx.dispatch("fetchInfobaseAvailable", { name });
|
|
}
|
|
}
|
|
},
|
|
async updateInfobaseURL(ctx, { name, url }) {
|
|
let data = {
|
|
...new_infobase(name),
|
|
url,
|
|
publicated: true,
|
|
};
|
|
|
|
ctx.commit("lockInfobase", name);
|
|
|
|
try {
|
|
await axios.post(`${api_base}/publications`, data);
|
|
ctx.commit("setInfobaseURL", { name, url });
|
|
} catch (err) {
|
|
ctx.commit("setErrorMessage", `Ошибка обновления URL для базы ${name}`);
|
|
} finally {
|
|
ctx.commit("unlockInfobase", name);
|
|
}
|
|
},
|
|
},
|
|
getters: {
|
|
errorMessage(state) {
|
|
return state.error_message;
|
|
},
|
|
allInfobases(state) {
|
|
return state.infobases;
|
|
},
|
|
getLockedInfobases(state) {
|
|
return state.locked_bases;
|
|
},
|
|
isInfobaseLocked: (state) => (name) => state.locked_bases.includes(name),
|
|
getInfobaseByName: (state) => (name) =>
|
|
state.infobases.find((infobase) => name === infobase.name),
|
|
isLoading(state) {
|
|
return state.is_loading;
|
|
},
|
|
isLoadingError(state) {
|
|
return state.loading_error;
|
|
},
|
|
},
|
|
};
|