Bug 38201: DRY delete

Signed-off-by: Michaela Sieber <michaela.sieber@kit.edu>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
This commit is contained in:
Pedro Amorim 2024-10-14 11:52:48 +00:00 committed by Lucas Gass
parent b95a975d5a
commit db9c0be914
No known key found for this signature in database
GPG Key ID: 60B447398B0D36CA
4 changed files with 76 additions and 59 deletions

View File

@ -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: {

View File

@ -1,5 +1,6 @@
<script>
import BaseResource from "../BaseResource.vue";
import { APIClient } from "../../fetch/api-client.js";
export default {
extends: BaseResource,
@ -7,10 +8,16 @@ export default {
return {
...BaseResource.setup({
resource_name: "agreement",
name_attr: "name",
id_attr: "agreement_id",
show_component: "AgreementsShow",
list_component: "AgreementsList",
add_component: "AgreementsFormAdd",
edit_component: "AgreementsFormAddEdit",
api_client: APIClient.erm.agreements,
i18n: {
display_name: __("Agreement"),
},
}),
//AgreementResource specific setup here
};

View File

@ -47,7 +47,7 @@
:searchable_av_options="searchable_av_options"
@show="goToResourceShow"
@edit="goToResourceEdit"
@delete="doDelete"
@delete="doResourceDelete"
@select="doSelect"
></KohaTable>
</div>
@ -222,33 +222,6 @@ export default {
});
});
},
doDelete: function (agreement, dt, event) {
this.setConfirmationDialog(
{
title: this.$__(
"Are you sure you want to remove this agreement?"
),
message: agreement.name,
accept_label: this.$__("Yes, delete"),
cancel_label: this.$__("No, do not delete"),
},
() => {
const client = APIClient.erm;
client.agreements.delete(agreement.agreement_id).then(
success => {
this.setMessage(
this.$__("Agreement %s deleted").format(
agreement.name
),
true
);
dt.draw();
},
error => {}
);
}
);
},
doSelect: function (agreement, dt, event) {
this.$emit("select-agreement", agreement.agreement_id);
this.$emit("close");

View File

@ -8,7 +8,7 @@
/>
<ToolbarButton
action="delete"
@delete_resource="delete_agreement"
@delete-resource="doResourceDelete"
/>
</Toolbar>
@ -398,36 +398,6 @@ export default {
error => {}
);
},
delete_agreement: function () {
let agreement_id = this.agreement.agreement_id;
let agreement_name = this.agreement.name;
this.setConfirmationDialog(
{
title: this.$__(
"Are you sure you want to remove this agreement?"
),
message: agreement_name,
accept_label: this.$__("Yes, delete"),
cancel_label: this.$__("No, do not delete"),
},
() => {
const client = APIClient.erm;
client.agreements.delete(agreement_id).then(
success => {
this.setMessage(
this.$__("Agreement %s deleted").format(
agreement_name
),
true
);
this.$router.push({ name: "AgreementsList" });
},
error => {}
);
}
);
},
},
components: { Toolbar, ToolbarButton, AdditionalFieldsDisplay },
name: "AgreementsShow",