Update action button to allow ID to be forced and to check tasks via mixin

This commit is contained in:
Julian Stirling 2026-03-10 19:48:13 +00:00
parent 84b96809ad
commit b4d6436bf3
2 changed files with 47 additions and 19 deletions

View file

@ -50,6 +50,18 @@ export default {
type: String, type: String,
required: true, required: true,
}, },
// forceId and forceUrl can be used if it is known that when this button is mounted
// the action should be running.
forceId: {
type: [String, null],
required: false,
default: null,
},
forceUrl: {
type: [String, null],
required: false,
default: null,
},
submitData: { submitData: {
type: [Object, Array], type: [Object, Array],
required: false, required: false,
@ -197,17 +209,22 @@ export default {
}, },
); );
// Check for already running tasks if (this.forceId && this.forceUrl) {
if (this.taskStarted != true) { this.taskStarted = true;
this.checkExistingTasks(); this.startPollingTask(this.forceId, this.forceUrl);
} } else {
// A global signal listener to perform the action // Check for already running tasks
if (this.submitOnEvent) { if (this.taskStarted != true) {
eventBus.on(this.submitOnEvent, () => { this.checkExistingTasks();
if (this.isDisabled) return; }
// Bootstrap task if button is not disabled. // A global signal listener to perform the action
this.bootstrapTask(); if (this.submitOnEvent) {
}); eventBus.on(this.submitOnEvent, () => {
if (this.isDisabled) return;
// Bootstrap task if button is not disabled.
this.bootstrapTask();
});
}
} }
}, },
@ -240,18 +257,13 @@ export default {
* *
*/ */
async checkExistingTasks() { async checkExistingTasks() {
let response = await this.findOngoingActions(this.thing, this.action); const ongoingTask = await this.getOngingAction(this.thing, this.action);
// Exit if response is null, due to an error.
if (response == null) return;
// Check for a task that is ongoing.
// We can't handle multiple tasks ongoing, so this picks the first.
const ongoingTask = response.data.find((t) => ["pending", "running"].includes(t.status));
if (ongoingTask) { if (ongoingTask) {
// There is a started task // There is a started task
this.taskStarted = true; this.taskStarted = true;
this.$emit("taskStarted");
// Find its URL // Find its URL
const taskUrl = ongoingTask.links.find((t) => t.rel == "self").href; const taskUrl = ongoingTask.links.find((t) => t.rel == "self").href;
this.$emit("taskStarted", ongoingTask.id, taskUrl);
this.startPollingTask(ongoingTask.id, taskUrl); this.startPollingTask(ongoingTask.id, taskUrl);
} }
}, },
@ -275,7 +287,6 @@ export default {
this.$emit("submit", this.submitData); this.$emit("submit", this.submitData);
// Send a request to start a task // Send a request to start a task
this.taskStarted = true; this.taskStarted = true;
this.$emit("taskStarted");
let response; let response;
try { try {
response = await this.invokeAction( response = await this.invokeAction(
@ -289,6 +300,9 @@ export default {
this.onTaskEnd(); this.onTaskEnd();
return; return;
} }
// Wait until after the task is acknowledged before confirming to parent
// component that the task is started.
this.$emit("taskStarted", response.data.id, response.data.href);
if (this.modalProgress) { if (this.modalProgress) {
this.$refs.statusModal.show(); this.$refs.statusModal.show();
} }

View file

@ -126,6 +126,20 @@ export default {
return null; return null;
} }
}, },
/**
* Find and ongoing Action and return its task response.
*
* Returns null if there is no ongoing action
*/
async getOngingAction(thing, action) {
let response = await this.findOngoingActions(thing, action);
// Exit if response is null, due to an error.
if (response == null) return null;
// Check for a task that is ongoing.
// We can't handle multiple tasks ongoing, so this picks the first.
// ?? Is the "Nullish Coalescing-Operator" to turn undefined into null.
return response.data.find((t) => ["pending", "running"].includes(t.status)) ?? null;
},
thingActionUrl(thing, action, allowUndefined = false) { thingActionUrl(thing, action, allowUndefined = false) {
let url = this.$store.getters["wot/thingActionUrl"]( let url = this.$store.getters["wot/thingActionUrl"](
thing, thing,