From 1936a5b08be8d8f1331440eaad8bdc009ab35298 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Thu, 17 Dec 2020 11:53:16 -0300 Subject: [PATCH] Bug 27250: Fix stacked event listeners in SMTP delete modal There is a design issue on the click event listener that makes the modal get added new event listeners on each row being deleted. This makes the page call all the event listeners and generates an error. This patch replaces the flawed logic (adding the event listener inside the event listener). It makes the event listener be defined at startup time, and make the .on('click') action just pass information around as required. To test: 1. Create at least 2 SMTP servers 2. Open the browser inspector, on the network tab 3. Click "Delete" for server 1 => SUCCESS: You get a successful message (OK) 4. Click "Delete" for server 2 => SUCCESS: You get a successful message (OK) => FAIL: You get also an alert message about server 1 (KO) => FAIL: The inspector shows more than one AJAX call has been made 5. Apply this patch and reload the page 6. Repeat 3-4 => SUCCESS: Everything goes smooth this time 7. Sign off :-D Signed-off-by: Jonathan Druart --- .../prog/en/modules/admin/smtp_servers.tt | 37 +++++++++++-------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/smtp_servers.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/smtp_servers.tt index 66975366db..ca915b2038 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/smtp_servers.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/smtp_servers.tt @@ -366,23 +366,30 @@ SMTP servers $("#delete_confirm_dialog").html( _("You are about to delete the '%s' SMTP server.").format(smtp_server_name) ); + $("#delete_confirm_modal_button").data('smtp-server-id', smtp_server_id); + $("#delete_confirm_modal_button").data('smtp-server-name', smtp_server_name); + }); - $("#delete_confirm_modal_button").on( "click", function () { - $.ajax({ - method: "DELETE", - url: "/api/v1/config/smtp_servers/"+smtp_server_id - }).success(function() { - $("#delete_confirm_modal").modal('hide'); - window.smtp_servers.api().ajax.reload(function (data) { - if (data.recordsTotal == 0) { - $("#smtp_servers_wrapper").hide(); - } - $("#smtp_action_result_dialog").hide(); - $("#smtp_delete_success").html(_("Server '%s' deleted successfully.").format(smtp_server_name)).show(); - }); - }).error(function () { - $("#smtp_delete_error").html(_("Error deleting server '%s'. Check the logs.").format(smtp_server_name)).show(); + $("#delete_confirm_modal_button").on( "click", function () { + + var smtp_server_id = $(this).data('smtp-server-id'); + var smtp_server_name = $(this).data('smtp-server-name'); + + $.ajax({ + method: "DELETE", + url: "/api/v1/config/smtp_servers/"+smtp_server_id + }).success(function() { + window.smtp_servers.api().ajax.reload(function (data) { + if (data.recordsTotal == 0) { + $("#smtp_servers_wrapper").hide(); + } + $("#smtp_action_result_dialog").hide(); + $("#smtp_delete_success").html(_("Server '%s' deleted successfully.").format(smtp_server_name)).show(); }); + }).fail(function () { + $("#smtp_delete_error").html(_("Error deleting server '%s'. Check the logs.").format(smtp_server_name)).show(); + }).done(function () { + $("#delete_confirm_modal").modal('hide'); }); }); }); -- 2.20.1