This commit is contained in:
Dmitry Belyaev 2021-05-30 18:17:06 +03:00
parent 3c589a4b05
commit 33595cb322
Signed by: b4tman
GPG Key ID: 41A00BF15EA7E5F3
3 changed files with 83 additions and 38 deletions

View File

@ -1,16 +1,16 @@
<template> <template>
<div class="container-fluid"> <div class="container-fluid">
<h2>Список баз</h2> <h2>Список баз</h2>
<div v-if="loading_error" class="text-danger m-4"> <div v-if="isLoadingError" class="text-danger m-4">
<span class="fs-1">&#9940;</span><br /> <span class="fs-1">&#9940;</span><br />
<h3>Ошибка загрузки</h3> <h3>Ошибка загрузки</h3>
</div> </div>
<div v-else-if="is_loading" class="container"> <div v-else-if="isLoading" class="container">
<div class="spinner-border text-primary" role="status"> <div class="spinner-border text-primary" role="status">
<span class="visually-hidden">Loading...</span> <span class="visually-hidden">Loading...</span>
</div> </div>
</div> </div>
<div v-else-if="0 == infobases.length" class="container"> <div v-else-if="0 == allInfobases.length" class="container">
<div class="alert alert-warning" role="alert"> <div class="alert alert-warning" role="alert">
<svg class="m-2" width="32" height="32"> <svg class="m-2" width="32" height="32">
<use xlink:href="#exclamation-triangle-fill--sprite"></use> <use xlink:href="#exclamation-triangle-fill--sprite"></use>
@ -28,7 +28,7 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<tr v-for="(infobase, index) in infobases" :key="infobase.name"> <tr v-for="(infobase, index) in allInfobases" :key="infobase.name">
<th class="text-end" scope="row">{{ index + 1 }}</th> <th class="text-end" scope="row">{{ index + 1 }}</th>
<td class="text-center">{{ infobase.name }}</td> <td class="text-center">{{ infobase.name }}</td>
<td class="text-start"> <td class="text-start">
@ -93,12 +93,10 @@
<script> <script>
import "bootstrap-icons/icons/exclamation-triangle-fill.svg?sprite"; import "bootstrap-icons/icons/exclamation-triangle-fill.svg?sprite";
import axios from "axios"; import { mapGetters } from "vuex";
import InfobaseURLEditor from "@/components/InfobaseURLEditor.vue"; import InfobaseURLEditor from "@/components/InfobaseURLEditor.vue";
const api_base = "http://localhost:17653/api/v1";
export default { export default {
name: "InfobaseList", name: "InfobaseList",
components: { components: {
@ -111,9 +109,10 @@ export default {
infobases: [], infobases: [],
url_editors: {}, url_editors: {},
}), }),
computed: mapGetters(["allInfobases", "isLoading", "isLoadingError"]),
methods: { methods: {
infobase(name) { infobase(name) {
return this.infobases.find((infobase) => name === infobase.name); return this.allInfobases.find((infobase) => name === infobase.name);
}, },
add_publication(name) { add_publication(name) {
let infobase = this.infobase(name); let infobase = this.infobase(name);
@ -151,32 +150,7 @@ export default {
}, },
}, },
async mounted() { async mounted() {
try { this.$store.dispatch("fetchInfobases");
const res = await axios.get(`${api_base}/infobases`);
if (!res) {
return;
}
const names = res.data;
if (!Array.isArray(names)) {
this.loading_error = true;
return;
}
if (0 < names.length) {
if ("string" !== typeof names[0]) {
this.loading_error = true;
return;
}
}
this.is_loading = false;
this.loading_error = false;
this.infobases = names.map((name) => {
return { name: name, publicated: "bpdemo" === name, url: "/" };
});
} catch (err) {
this.loading_error = true;
}
}, },
}; };
</script> </script>

View File

@ -1,8 +1,9 @@
import { createStore } from "vuex"; import { createStore } from "vuex";
import infobases from "@/store/modules/infobases.js";
export default createStore({ export default createStore({
state: {}, modules: {
mutations: {}, infobases,
actions: {}, },
modules: {},
}); });

View File

@ -0,0 +1,70 @@
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;
},
},
};