ui_migration refactor(store) update mixins
This commit is contained in:
parent
3ecb5b32e4
commit
ed158a0b60
2 changed files with 41 additions and 47 deletions
|
|
@ -5,8 +5,10 @@
|
||||||
* This mixin is registered globally in `main.js` using. Do not
|
* This mixin is registered globally in `main.js` using. Do not
|
||||||
* manually import it in components.
|
* manually import it in components.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
|
import { useWotStore } from "@/stores/wot.js";
|
||||||
|
import { useSettingsStore } from "@/stores/settings.js";
|
||||||
|
import { mapState, mapStores } from "pinia";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
data() {
|
data() {
|
||||||
|
|
@ -15,32 +17,35 @@ export default {
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
|
computed: {
|
||||||
|
...mapState(useSettingsStore, ["baseUri"]),
|
||||||
|
...mapStores(useWotStore),
|
||||||
|
},
|
||||||
|
|
||||||
methods: {
|
methods: {
|
||||||
thingDescription(thing) {
|
thingDescription(thing) {
|
||||||
return this.$store.getters["wot/thingDescription"](thing);
|
return this.wotStore.thingDescriptions[thing];
|
||||||
},
|
},
|
||||||
thingList() {
|
thingList() {
|
||||||
return this.$store.getters["wot/thingList"];
|
return this.wotStore.thingList();
|
||||||
},
|
},
|
||||||
thingAvailable(thing) {
|
thingAvailable(thing) {
|
||||||
return this.$store.getters["wot/thingAvailable"](thing);
|
return this.wotStore.thingAvailable(thing);
|
||||||
},
|
},
|
||||||
thingPropertyUrl(thing, property, allowUndefined = false) {
|
thingPropertyUrl(thing, property, allowUndefined = false) {
|
||||||
return this.$store.getters["wot/thingPropertyUrl"](
|
return this.wotStore.thingPropertyUrl(thing, property, "readproperty", allowUndefined);
|
||||||
thing,
|
},
|
||||||
property,
|
thingActionUrl(thing, action, allowUndefined = false) {
|
||||||
"readproperty",
|
return this.wotStore.thingActionUrl(thing, action, "invokeaction", allowUndefined);
|
||||||
allowUndefined,
|
|
||||||
);
|
|
||||||
},
|
},
|
||||||
thingActionAvailable(thing, action) {
|
thingActionAvailable(thing, action) {
|
||||||
return this.$store.getters["wot/thingAffordanceAvailable"](thing, "actions", action);
|
return this.wotStore.affordanceAvailable(thing, "actions", action);
|
||||||
},
|
},
|
||||||
thingPropertyAvailable(thing, property) {
|
thingPropertyAvailable(thing, property) {
|
||||||
return this.$store.getters["wot/thingAffordanceAvailable"](thing, "properties", property);
|
return this.wotStore.affordanceAvailable(thing, "properties", property);
|
||||||
},
|
},
|
||||||
async readThingProperty(thing, property, silenceErrors = false) {
|
async readThingProperty(thing, property, silenceErrors = false) {
|
||||||
let url = this.$store.getters["wot/thingPropertyUrl"](thing, property, "readproperty", false);
|
let url = this.wotStore.thingPropertyUrl(thing, property, "readproperty", false);
|
||||||
try {
|
try {
|
||||||
let response = await axios.get(url);
|
let response = await axios.get(url);
|
||||||
return response.data;
|
return response.data;
|
||||||
|
|
@ -50,12 +55,7 @@ export default {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
async writeThingProperty(thing, property, value) {
|
async writeThingProperty(thing, property, value) {
|
||||||
let url = this.$store.getters["wot/thingPropertyUrl"](
|
let url = this.wotStore.thingPropertyUrl(thing, property, "writeproperty", false);
|
||||||
thing,
|
|
||||||
property,
|
|
||||||
"writeproperty",
|
|
||||||
false,
|
|
||||||
);
|
|
||||||
// `false` and 0 fail because axios turns it to ""
|
// `false` and 0 fail because axios turns it to ""
|
||||||
// Other values should not be stringified or pydantic
|
// Other values should not be stringified or pydantic
|
||||||
// can't parse them.
|
// can't parse them.
|
||||||
|
|
@ -88,7 +88,7 @@ export default {
|
||||||
let response;
|
let response;
|
||||||
let finalMethodCalled = false;
|
let finalMethodCalled = false;
|
||||||
try {
|
try {
|
||||||
response = await axios.get(taskUrl, { baseURL: this.$store.getters.baseUri });
|
response = await axios.get(taskUrl, { baseURL: this.baseUri });
|
||||||
const result = response.data.status;
|
const result = response.data.status;
|
||||||
|
|
||||||
if (result === "running" || result === "pending") {
|
if (result === "running" || result === "pending") {
|
||||||
|
|
@ -115,7 +115,7 @@ export default {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
terminateAction(taskUrl) {
|
terminateAction(taskUrl) {
|
||||||
axios.delete(taskUrl, { baseURL: this.$store.getters.baseUri });
|
axios.delete(taskUrl, { baseURL: this.baseUri });
|
||||||
},
|
},
|
||||||
async findOngoingActions(thing, action) {
|
async findOngoingActions(thing, action) {
|
||||||
let url = this.thingActionUrl(thing, action);
|
let url = this.thingActionUrl(thing, action);
|
||||||
|
|
@ -131,7 +131,7 @@ export default {
|
||||||
*
|
*
|
||||||
* Returns null if there is no ongoing action
|
* Returns null if there is no ongoing action
|
||||||
*/
|
*/
|
||||||
async getOngingAction(thing, action) {
|
async getOngoingAction(thing, action) {
|
||||||
let response = await this.findOngoingActions(thing, action);
|
let response = await this.findOngoingActions(thing, action);
|
||||||
// Exit if response is null, due to an error.
|
// Exit if response is null, due to an error.
|
||||||
if (response == null) return null;
|
if (response == null) return null;
|
||||||
|
|
@ -140,17 +140,9 @@ export default {
|
||||||
// ?? Is the "Nullish Coalescing-Operator" to turn undefined into null.
|
// ?? Is the "Nullish Coalescing-Operator" to turn undefined into null.
|
||||||
return response.data.find((t) => ["pending", "running"].includes(t.status)) ?? null;
|
return response.data.find((t) => ["pending", "running"].includes(t.status)) ?? null;
|
||||||
},
|
},
|
||||||
thingActionUrl(thing, action, allowUndefined = false) {
|
|
||||||
let url = this.$store.getters["wot/thingActionUrl"](
|
|
||||||
thing,
|
|
||||||
action,
|
|
||||||
"invokeaction",
|
|
||||||
allowUndefined,
|
|
||||||
);
|
|
||||||
return url;
|
|
||||||
},
|
|
||||||
async getThingEndpoint(thing, endpoint) {
|
async getThingEndpoint(thing, endpoint) {
|
||||||
let url = `${this.$store.getters.baseUri}/${thing}/${endpoint}`;
|
let url = `${this.baseUri}/${thing}/${endpoint}`;
|
||||||
try {
|
try {
|
||||||
const response = await axios.get(url);
|
const response = await axios.get(url);
|
||||||
return response.data;
|
return response.data;
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@
|
||||||
|
|
||||||
import UIkit from "uikit";
|
import UIkit from "uikit";
|
||||||
import { eventBus } from "@/eventBus";
|
import { eventBus } from "@/eventBus";
|
||||||
|
import { useSettingsStore } from "@/stores/settings.js";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
methods: {
|
methods: {
|
||||||
|
|
@ -55,9 +56,10 @@ export default {
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|
||||||
modalError: function (error) {
|
modalError(set_error) {
|
||||||
var errormsg = this.getErrorMessage(error);
|
const store = useSettingsStore();
|
||||||
this.$store.commit("setErrorMessage", errormsg);
|
var errormsg = this.getErrorMessage(set_error);
|
||||||
|
store.error = errormsg;
|
||||||
UIkit.notification({
|
UIkit.notification({
|
||||||
message: `${errormsg}`,
|
message: `${errormsg}`,
|
||||||
status: "danger",
|
status: "danger",
|
||||||
|
|
@ -78,30 +80,30 @@ export default {
|
||||||
return String(data);
|
return String(data);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
getErrorData: function (error) {
|
getErrorData: function (data_error) {
|
||||||
// If a response was obtained, extract the most specific message
|
// If a response was obtained, extract the most specific message
|
||||||
if (error.response) {
|
if (data_error.response) {
|
||||||
// If the response is a nicely formatted JSON response from the server
|
// If the response is a nicely formatted JSON response from the server
|
||||||
if (error.response.data.message) {
|
if (data_error.response.data.message) {
|
||||||
return error.response.data.message;
|
return data_error.response.data.message;
|
||||||
}
|
}
|
||||||
if (error.response.data.detail) {
|
if (data_error.response.data.detail) {
|
||||||
try {
|
try {
|
||||||
return error.response.data.detail[0].msg;
|
return data_error.response.data.detail[0].msg;
|
||||||
} catch {
|
} catch {
|
||||||
return error.response.data.detail;
|
return data_error.response.data.detail;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// If the response is just some generic error response
|
// If the response is just some generic error response
|
||||||
if (error.response.data) {
|
if (data_error.response.data) {
|
||||||
return error.response.data;
|
return data_error.response.data;
|
||||||
}
|
}
|
||||||
return error.response;
|
return data_error.response;
|
||||||
}
|
}
|
||||||
// If we have an error object with a message, use that
|
// If we have an error object with a message, use that
|
||||||
if (error.message) return error.message;
|
if (data_error.message) return data_error.message;
|
||||||
// At this point just formatting the whole error object is the best we can do.
|
// At this point just formatting the whole error object is the best we can do.
|
||||||
return error;
|
return data_error;
|
||||||
},
|
},
|
||||||
|
|
||||||
showModalElement: function (element) {
|
showModalElement: function (element) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue