Web: Cleanup, abstract some code away into separate functions

No functional changes, just preparation for adding similar job actions.
This commit is contained in:
Sybren A. Stüvel 2022-04-21 17:15:19 +02:00
parent 2e2184df62
commit 698db0bf7c
2 changed files with 28 additions and 17 deletions

View File

@ -19,25 +19,23 @@ export default {
},
methods: {
onButtonDelete() {
const numJobs = this.jobs.numSelected;
this.jobs.deleteJobs()
.then(() => {
this.notifs.add(`${numJobs} jobs marked for deletion`);
})
.catch((error) => {
const errorMsg = JSON.stringify(error); // TODO: handle API errors better.
this.notifs.add(`Error: ${errorMsg}`);
})
return this._handleJobActionPromise(
this.jobs.deleteJobs(), "marked for deletion");
},
onButtonCancel() {
return this._handleJobActionPromise(
this.jobs.cancelJobs(), "marked for cancellation");
},
_handleJobActionPromise(promise, description) {
const numJobs = this.jobs.numSelected;
this.jobs.cancelJobs()
return promise
.then(() => {
let message;
if (numJobs == 1) {
message = `Job marked for cancellation`;
message = `Job ${description}`;
} else {
message = `${numJobs} jobs marked for cancellation`;
message = `${numJobs} jobs ${description}`;
}
this.notifs.add(message);
})

View File

@ -55,14 +55,27 @@ export const useJobs = defineStore('jobs', {
});
return deletionPromise;
},
cancelJobs() {
const statuschange = new API.JobStatusChange("cancel-requested", "requested from web interface");
return jobsAPI.setJobStatus(this.activeJob.id, statuschange);
},
cancelJobs() { return this._setJobStatus("cancel-requested"); },
// Internal methods.
/**
*
* @param {string[]} statuses
* @returns bool indicating whether there is a selected job with any of the given statuses.
*/
_anyJobWithStatus(statuses) {
return this.selectedJobs.reduce((foundJob, job) => (foundJob || statuses.includes(job.status)), false);
}
},
/**
* Transition the selected job(s) to the new status.
* @param {string} newStatus
* @returns a Promise for the API request.
*/
_setJobStatus(newStatus) {
const statuschange = new API.JobStatusChange(newStatus, "requested from web interface");
return jobsAPI.setJobStatus(this.activeJob.id, statuschange);
},
},
})