mock api rewtite + more vuex

This commit is contained in:
Dmitry Belyaev 2021-05-31 16:09:36 +03:00
parent bb204e12ab
commit 91f582c289
Signed by: b4tman
GPG Key ID: 41A00BF15EA7E5F3
4 changed files with 72 additions and 56 deletions

View File

@ -93,7 +93,7 @@
<script>
import "bootstrap-icons/icons/exclamation-triangle-fill.svg?sprite";
import { mapGetters } from "vuex";
import { mapGetters, mapMutations } from "vuex";
import InfobaseURLEditor from "@/components/InfobaseURLEditor.vue";
@ -109,31 +109,25 @@ export default {
infobases: [],
url_editors: {},
}),
computed: mapGetters(["allInfobases", "isLoading", "isLoadingError"]),
computed: mapGetters([
"allInfobases",
"getInfobaseByName",
"isLoading",
"isLoadingError",
]),
methods: {
infobase(name) {
return this.allInfobases.find((infobase) => name === infobase.name);
},
...mapMutations({
set_url: "setInfobaseURL",
set_publication: "setInfobasePublication",
}),
add_publication(name) {
let infobase = this.infobase(name);
if (infobase === undefined) return;
infobase.publicated = true;
this.set_publication({ name, publicated: true });
},
remove_publication(name) {
let infobase = this.infobase(name);
if (infobase === undefined) return;
infobase.publicated = false;
},
set_url(name, url) {
let infobase = this.infobase(name);
if (infobase === undefined) return;
infobase.url = url;
this.set_publication({ name, publicated: false });
},
start_edit_url(name) {
let infobase = this.infobase(name);
let infobase = this.getInfobaseByName(name);
if (infobase === undefined) return;
this.url_editors[name] = true;
@ -142,7 +136,7 @@ export default {
delete this.url_editors[name];
},
apply_edit_url({ name, url }) {
this.set_url(name, url);
this.set_url({ name, url });
delete this.url_editors[name];
},
is_edit_url_active(name) {

View File

@ -1,10 +1,12 @@
import axios from "axios";
const api_base = "http://localhost:17653/api/v1";
//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},
Loaded: { loading: false, error: false },
Loading: { loading: true, error: false },
Error: { loading: false, error: true },
});
export default {
@ -16,41 +18,45 @@ export default {
is_loading: false,
},
mutations: {
setLoadingStatus(state, {loading, error}) {
state.is_loading = loading && ! error;
setLoadingStatus(state, { loading, error }) {
state.is_loading = loading && !error;
state.loading_error = error;
},
updateInfobases(state, infobases) {
setInfobases(state, infobases) {
state.infobases = infobases;
},
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;
},
},
actions: {
async fetchInfobases(ctx) {
ctx.commit("setLoadingStatus", LoadingStatus.Loading);
try {
const res = await axios.get(`${api_base}/infobases`);
const res = await axios.get(`${api_base}/infobases-all`);
if (!res) {
ctx.commit("setLoadingStatus", LoadingStatus.Error);
return;
}
const names = res.data;
if (!Array.isArray(names)) {
const infobases = res.data;
if (!Array.isArray(infobases)) {
ctx.commit("setLoadingStatus", LoadingStatus.Error);
return;
}
if (0 < names.length) {
if ("string" !== typeof names[0]) {
ctx.commit("setLoadingStatus", LoadingStatus.Error);
return;
}
}
ctx.commit("setInfobases", infobases);
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);
}
@ -60,6 +66,8 @@ export default {
allInfobases(state) {
return state.infobases;
},
getInfobaseByName: (state) => (name) =>
state.infobases.find((infobase) => name === infobase.name),
isLoading(state) {
return state.is_loading;
},

View File

@ -1,11 +1,27 @@
{
"index": ["infobases", "publications", "module", "config"],
"infobases":["test1", "accounting", "bpdemo", "hrm31", "Trade-2021"],
"publications":["accounting", "hrm31"],
"publication":[
{"name": "accounting", "url": "/acc"},
{"name": "hrm31", "url": "/hrm"}
"index": ["infobases-all", "infobases-available", "publications", "module", "config", "config-test", "apache-restart"],
"infobases-available":["test1", "accounting", "bpdemo", "hrm31", "Trade-2021"],
"infobases-all":[
{"name": "test1", "url": "", "publicated": false},
{"name": "accounting", "url": "/acc", "publicated": true},
{"name": "bpdemo", "url": "", "publicated": false},
{"name": "hrm31", "url": "/hrm", "publicated": true},
{"name": "Trade-2021", "url": "", "publicated": false}
],
"publications":[
{"name": "accounting", "url": "/acc", "publicated": true},
{"name": "hrm31", "url": "/hrm", "publicated": true}
],
"module":{},
"config":{}
"config":{
"url_prefix": "http://localhost"
},
"config-test":{
"is_apache_cfg_valid": true,
"is_vrd_path_valid": true,
"is_dir_path_valid": true,
"is_url_base_valid": true,
"is_module_valid": true
}
}

View File

@ -1,7 +1,5 @@
{
"/api/v1/*": "/$1",
"/": "/index",
"": "/index",
"/publications/:name": "/publication/:name",
"/publications/:name/url": "/publication/:name"
}
"/api/v1/*": "/$1",
"/": "/index",
"": "/index"
}