From 85e2578cf37387f975afd56e3f2573bd57043a77 Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Thu, 15 Feb 2024 16:08:56 +0000 Subject: [PATCH] Extract submit logic to a function --- assets/js/translation-events.js | 86 +++++++++++++++++---------------- 1 file changed, 45 insertions(+), 41 deletions(-) diff --git a/assets/js/translation-events.js b/assets/js/translation-events.js index 3d8162d12..b6c46088b 100644 --- a/assets/js/translation-events.js +++ b/assets/js/translation-events.js @@ -14,52 +14,56 @@ 'click', function ( e ) { e.preventDefault(); - if ( $( '#event-end' ).val() <= $( '#event-start' ).val() ) { - $gp.notices.error( 'Event end date and time must be later than event start date and time.' ); - return; - } - const btnClicked = $( this ).data( 'event-status' ); - if ( btnClicked === 'publish' && '' === $( '#event-id' ).val() ) { - const submitPrompt = 'Are you sure you want to publish this event?'; - if ( ! confirm( submitPrompt ) ) { - return; - } - } - $( '#event-form-action' ).val( btnClicked ); - const $form = $( '.translation-event-form' ); - - $.ajax( - { - type: 'POST', - url: $translation_event.url, - data:$form.serialize(), - success: function ( response ) { - if ( response.data.eventId ) { - history.replaceState( '', '', response.data.eventEditUrl ); - $( '#form-name' ).val( 'edit_event' ); - $( '.event-page-title' ).text( 'Edit Event' ); - $( '#event-id' ).val( response.data.eventId ); - if ( btnClicked === 'publish' ) { - $( 'button[data-event-status="draft"]' ).hide(); - $( 'button[data-event-status="publish"]' ).text( 'Update Event' ); - } - if ( btnClicked === 'draft' ) { - $( 'button[data-event-status="draft"]' ).text( 'Update Draft' ); - } - $( '#event-url' ).removeClass( 'hide-event-url' ).find( 'a' ).attr( 'href', response.data.eventUrl ).text( response.data.eventUrl ); - $gp.notices.success( response.data.message ); - } - }, - error: function ( error ) { - $gp.notices.error( response.data.message ); - } - } - ); + handleSubmit(); } ); } ); + function handleSubmit() { + if ( $( '#event-end' ).val() <= $( '#event-start' ).val() ) { + $gp.notices.error( 'Event end date and time must be later than event start date and time.' ); + return; + } + const btnClicked = $( this ).data( 'event-status' ); + if ( btnClicked === 'publish' && '' === $( '#event-id' ).val() ) { + const submitPrompt = 'Are you sure you want to publish this event?'; + if ( ! confirm( submitPrompt ) ) { + return; + } + } + $( '#event-form-action' ).val( btnClicked ); + const $form = $( '.translation-event-form' ); + + $.ajax( + { + type: 'POST', + url: $translation_event.url, + data:$form.serialize(), + success: function ( response ) { + if ( response.data.eventId ) { + history.replaceState( '', '', response.data.eventEditUrl ); + $( '#form-name' ).val( 'edit_event' ); + $( '.event-page-title' ).text( 'Edit Event' ); + $( '#event-id' ).val( response.data.eventId ); + if ( btnClicked === 'publish' ) { + $( 'button[data-event-status="draft"]' ).hide(); + $( 'button[data-event-status="publish"]' ).text( 'Update Event' ); + } + if ( btnClicked === 'draft' ) { + $( 'button[data-event-status="draft"]' ).text( 'Update Draft' ); + } + $( '#event-url' ).removeClass( 'hide-event-url' ).find( 'a' ).attr( 'href', response.data.eventUrl ).text( response.data.eventUrl ); + $gp.notices.success( response.data.message ); + } + }, + error: function ( error ) { + $gp.notices.error( response.data.message ); + } + } + ); + } + function validateEventDates() { const startDateTimeInput = $( '#event-start' ); const endDateTimeInput = $( '#event-end' );