From 7071019acfe398302872e098b509b01404638a57 Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Mon, 14 Oct 2024 11:52:48 +0000 Subject: [PATCH] Bug 37301: DRY delete --- .../prog/js/vue/components/BaseResource.vue | 67 +++++++++++++++++++ .../vue/components/ERM/AgreementResource.vue | 7 ++ .../js/vue/components/ERM/AgreementsList.vue | 29 +------- .../js/vue/components/ERM/AgreementsShow.vue | 32 +-------- 4 files changed, 76 insertions(+), 59 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/js/vue/components/BaseResource.vue b/koha-tmpl/intranet-tmpl/prog/js/vue/components/BaseResource.vue index 65f93af2ce..3e08316701 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/vue/components/BaseResource.vue +++ b/koha-tmpl/intranet-tmpl/prog/js/vue/components/BaseResource.vue @@ -48,6 +48,73 @@ export default { name: this.add_component, }) }, + /** + * Navigates to the list page of the given resource. + * + * @return {void} + */ + goToResourceList: function () { + this.$router.push({ + name: this.list_component, + }) + }, + /** + * Resource deletion handler. + * Accepts an optional callback function to run after deletion. + * If no callback is provided, does the following: + * - If deleting from show component, navigates to resource list component. + * - If deleting from resource list component, redraws the table. + * + * @param {Object} resource - The resource to delete (optional) + * @param {Object} callback - Callback to call after deletion (optional) + * @return {void} + */ + doResourceDelete: function (resource, callback) { + let resource_id = resource + ? resource[this.id_attr] + : this[this.resource_name][this.id_attr] + let resource_name = resource + ? resource[this.name_attr] + : this[this.resource_name][this.name_attr] + + this.setConfirmationDialog( + { + title: this.$__( + "Are you sure you want to remove this %s?" + ).format(this.i18n.display_name.toLowerCase()), + message: resource_name, + accept_label: this.$__("Yes, delete"), + cancel_label: this.$__("No, do not delete"), + }, + () => { + this.api_client.delete(resource_id).then( + success => { + this.setMessage( + this.$__("%s %s deleted").format( + this.i18n.display_name, + resource_name + ), + true + ) + if (typeof callback === "function") { + callback() + } else { + if ( + this.$options.name === this.list_component + ) { + this.$refs.table.redraw(this.table_url()) + } else if ( + this.$options.name === this.show_component + ) { + this.goToResourceList() + } + } + }, + error => {} + ) + } + ) + }, }, name: "BaseResource", props: { diff --git a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementResource.vue b/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementResource.vue index 8802518814..6e0fd17327 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementResource.vue +++ b/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementResource.vue @@ -1,5 +1,6 @@