Skip to content

Commit

Permalink
Extract submit logic to a function
Browse files Browse the repository at this point in the history
  • Loading branch information
psrpinto committed Feb 16, 2024
1 parent 243b697 commit 85e2578
Showing 1 changed file with 45 additions and 41 deletions.
86 changes: 45 additions & 41 deletions assets/js/translation-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand Down

0 comments on commit 85e2578

Please sign in to comment.