pub1c-web.frontend/src/store/modules/infobases.js

195 lines
5.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import axios from "axios";
//const api_base = "http://localhost:5000/api/v1"; // test1
const api_base = "http://localhost:17653"; // test2 - mock
const LoadingStatus = Object.freeze({
Loaded: { loading: false, error: false },
Loading: { loading: true, error: false },
Error: { loading: false, error: true },
});
// const new_infobase = (name) => ({
// name,
// url: "",
// publicated: false,
// });
const infobaseByName = (state) => (name) =>
state.infobases.find((infobase) => name === infobase.name);
function setInfobaseLock(state, { name, lock }) {
const locked = state.locked_bases.includes(name);
if (locked === lock) return;
if (lock) {
state.locked_bases.push(name);
} else {
const idx = state.locked_bases.findIndex((x) => name === x);
state.locked_bases.splice(idx, 1);
}
}
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 }) {
const idx = state.infobases.findIndex((x) => name === x.name);
if (-1 === idx) {
state.infobases.push(infobase);
} else {
if (null == infobase) {
state.infobases.splice(idx, 1);
setInfobaseLock(state, { name, lock: false });
} else {
infobase.publicated = true;
state.infobases[idx] = infobase;
}
}
},
setInfobaseURL(state, { name, url }) {
const infobase = infobaseByName(state)(name);
if (infobase === undefined) return;
infobase.url = url;
},
setInfobasePublication(state, { name, publicated }) {
const infobase = infobaseByName(state)(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 fetchInfobase(ctx, name) {
ctx.commit("lockInfobase", name);
try {
const res = await axios.get(`${api_base}/infobases-all`);
if (!res) {
return;
}
const infobase = res.data.find((i) => name === i.name);
ctx.commit("setInfobase", { name, infobase });
ctx.commit("unlockInfobase", name);
} catch (err) {
ctx.commit(
"setErrorMessage",
`Ошибка получения информации о базе: ${name}`
);
ctx.commit("setInfobase", { name, infobase: null });
} finally {
ctx.commit("unlockInfobase", name);
}
},
async updateInfobaseURL(ctx, { name, url }) {
const infobase = ctx.getters.getInfobaseByName(name);
const data = {
...infobase,
url,
};
ctx.commit("lockInfobase", name);
try {
await axios.post(`${api_base}/publications`, data);
ctx.commit("setInfobaseURL", { name, url });
ctx.commit("setErrorMessage", "");
} catch (err) {
ctx.commit("setErrorMessage", `Ошибка обновления URL для базы ${name}`);
} finally {
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) {
return state.error_message;
},
allInfobases(state) {
return state.infobases;
},
getLockedInfobases(state) {
return state.locked_bases;
},
isInfobaseLocked: (state) => (name) => state.locked_bases.includes(name),
getInfobaseByName: infobaseByName,
isLoading(state) {
return state.is_loading;
},
isLoadingError(state) {
return state.loading_error;
},
},
};