From 698db0bf7cf2fbda411ea8bff71f86c570d19cd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Thu, 21 Apr 2022 17:15:19 +0200 Subject: [PATCH] Web: Cleanup, abstract some code away into separate functions No functional changes, just preparation for adding similar job actions. --- web/app/src/components/JobActionsBar.vue | 22 ++++++++++------------ web/app/src/stores/jobs.js | 23 ++++++++++++++++++----- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/web/app/src/components/JobActionsBar.vue b/web/app/src/components/JobActionsBar.vue index 903d08c8..bd1d0bfe 100644 --- a/web/app/src/components/JobActionsBar.vue +++ b/web/app/src/components/JobActionsBar.vue @@ -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); }) diff --git a/web/app/src/stores/jobs.js b/web/app/src/stores/jobs.js index cb7c79e7..4379bb70 100644 --- a/web/app/src/stores/jobs.js +++ b/web/app/src/stores/jobs.js @@ -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); + }, }, })