71 lines
1.8 KiB
JavaScript
71 lines
1.8 KiB
JavaScript
import axios from "axios";
|
|
|
|
const api_base = "http://localhost:17653/api/v1";
|
|
const LoadingStatus = Object.freeze({
|
|
Loaded: {loading: false, error: false},
|
|
Loading: {loading: true, error: false},
|
|
Error: {loading: false, error: true},
|
|
});
|
|
|
|
export default {
|
|
state: {
|
|
publicated: [],
|
|
available: [],
|
|
infobases: [],
|
|
loading_error: false,
|
|
is_loading: false,
|
|
},
|
|
mutations: {
|
|
setLoadingStatus(state, {loading, error}) {
|
|
state.is_loading = loading && ! error;
|
|
state.loading_error = error;
|
|
},
|
|
updateInfobases(state, infobases) {
|
|
state.infobases = infobases;
|
|
},
|
|
},
|
|
actions: {
|
|
async fetchInfobases(ctx) {
|
|
ctx.commit("setLoadingStatus", LoadingStatus.Loading);
|
|
try {
|
|
const res = await axios.get(`${api_base}/infobases`);
|
|
if (!res) {
|
|
ctx.commit("setLoadingStatus", LoadingStatus.Error);
|
|
return;
|
|
}
|
|
const names = res.data;
|
|
if (!Array.isArray(names)) {
|
|
ctx.commit("setLoadingStatus", LoadingStatus.Error);
|
|
return;
|
|
}
|
|
|
|
if (0 < names.length) {
|
|
if ("string" !== typeof names[0]) {
|
|
ctx.commit("setLoadingStatus", LoadingStatus.Error);
|
|
return;
|
|
}
|
|
}
|
|
|
|
ctx.commit("setLoadingStatus", LoadingStatus.Loaded);
|
|
let infobases = names.map((name) => {
|
|
return { name: name, publicated: "bpdemo" === name, url: "/" };
|
|
});
|
|
ctx.commit("updateInfobases", infobases);
|
|
} catch (err) {
|
|
ctx.commit("setLoadingStatus", LoadingStatus.Error);
|
|
}
|
|
},
|
|
},
|
|
getters: {
|
|
allInfobases(state) {
|
|
return state.infobases;
|
|
},
|
|
isLoading(state) {
|
|
return state.is_loading;
|
|
},
|
|
isLoadingError(state) {
|
|
return state.loading_error;
|
|
},
|
|
},
|
|
};
|