From 9e1cb645d99e41609e75d56fea78a1c6e5c48f53 Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Fri, 16 Feb 2024 15:03:58 +0000 Subject: [PATCH] Extract delete logic to a function --- assets/js/translation-events.js | 45 +++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/assets/js/translation-events.js b/assets/js/translation-events.js index 5efd5539..608f282f 100644 --- a/assets/js/translation-events.js +++ b/assets/js/translation-events.js @@ -18,26 +18,13 @@ } ); - $('.delete-event').on('click', function(e) { - e.preventDefault(); - if ( ! confirm( 'Are you sure you want to delete this event?' ) ) { - return; + $('.delete-event').on( + 'click', + function ( e ) { + e.preventDefault(); + handleDelete() } - const $form = $('.translation-event-form'); - $('#form-name').val('delete_event'); - $('#event-form-action').val('delete'); - $.ajax({ - type: 'POST', - url: $translation_event.url, - data:$form.serialize(), - success: function(response) { - window.location = response.data.eventDeleteUrl; - }, - error: function(error) { - $gp.notices.error(response.data.message); - }, - }); - }); + ); } ); @@ -89,6 +76,26 @@ ); } + function handleDelete() { + if ( ! confirm( 'Are you sure you want to delete this event?' ) ) { + return; + } + const $form = $('.translation-event-form'); + $('#form-name').val('delete_event'); + $('#event-form-action').val('delete'); + $.ajax({ + type: 'POST', + url: $translation_event.url, + data:$form.serialize(), + success: function(response) { + window.location = response.data.eventDeleteUrl; + }, + error: function(error) { + $gp.notices.error(response.data.message); + }, + }); + } + function validateEventDates() { const startDateTimeInput = $('#event-start'); const endDateTimeInput = $('#event-end');