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

79 lines
2.3 KiB
JavaScript
Raw Normal View History

2021-05-30 15:17:06 +00:00
import axios from "axios";
2021-05-31 13:09:36 +00:00
//const api_base = "http://localhost:5000/api/v1"; // test1
const api_base = "http://localhost:17653/api/v1"; // test2 - mock
2021-05-30 15:17:06 +00:00
const LoadingStatus = Object.freeze({
2021-05-31 13:09:36 +00:00
Loaded: { loading: false, error: false },
Loading: { loading: true, error: false },
Error: { loading: false, error: true },
2021-05-30 15:17:06 +00:00
});
export default {
state: {
publicated: [],
available: [],
infobases: [],
loading_error: false,
is_loading: false,
},
mutations: {
2021-05-31 13:09:36 +00:00
setLoadingStatus(state, { loading, error }) {
state.is_loading = loading && !error;
2021-05-30 15:17:06 +00:00
state.loading_error = error;
},
2021-05-31 13:09:36 +00:00
setInfobases(state, infobases) {
2021-05-30 15:17:06 +00:00
state.infobases = infobases;
},
2021-05-31 13:09:36 +00:00
setInfobase(state, { name, infobase }) {
let idx = state.infobases.findindex((infobase) => name === infobase.name);
if (-1 === idx) return;
state.infobases[idx] = infobase;
},
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;
},
2021-05-30 15:17:06 +00:00
},
actions: {
async fetchInfobases(ctx) {
ctx.commit("setLoadingStatus", LoadingStatus.Loading);
try {
2021-05-31 13:09:36 +00:00
const res = await axios.get(`${api_base}/infobases-all`);
2021-05-30 15:17:06 +00:00
if (!res) {
ctx.commit("setLoadingStatus", LoadingStatus.Error);
return;
}
2021-05-31 13:09:36 +00:00
const infobases = res.data;
if (!Array.isArray(infobases)) {
2021-05-30 15:17:06 +00:00
ctx.commit("setLoadingStatus", LoadingStatus.Error);
return;
}
2021-05-31 13:09:36 +00:00
ctx.commit("setInfobases", infobases);
2021-05-30 15:17:06 +00:00
ctx.commit("setLoadingStatus", LoadingStatus.Loaded);
} catch (err) {
ctx.commit("setLoadingStatus", LoadingStatus.Error);
}
},
},
getters: {
allInfobases(state) {
return state.infobases;
},
2021-05-31 13:09:36 +00:00
getInfobaseByName: (state) => (name) =>
state.infobases.find((infobase) => name === infobase.name),
2021-05-30 15:17:06 +00:00
isLoading(state) {
return state.is_loading;
},
isLoadingError(state) {
return state.loading_error;
},
},
};