2023-05-15 18:49:02 +02:00
$ ( '.frontendForm' ) . each ( function ( ) {
console . log ( 'frontendForm found' ) ;
$ ( this ) . on ( 'submit' , function ( e ) {
e . preventDefault ( ) ;
var form = $ ( this ) ;
// Show overlay with spinner
$ ( '.loader-overlay' ) . addClass ( 'loaderActive' ) ;
formData = form . serializeArray ( ) ;
console . log ( formData , $ ( this ) . attr ( 'method' ) , $ ( this ) . attr ( 'data-target' ) ) ;
console . log ( 'submitting form' ) ;
$ . ajax ( {
type : $ ( this ) . attr ( 'method' ) ,
url : $ ( this ) . attr ( 'data-target' ) ,
data : formData ,
dataType : 'json' ,
success : function ( data ) {
console . log ( 'success' ) ;
// Hide overlay with spinner
$ ( '.loader-overlay' ) . removeClass ( 'loaderActive' ) ;
// Close the modal
$ ( '.modal' ) . modal ( 'hide' ) ;
// Clear all fields
form . find ( 'input, textarea' ) . val ( '' ) ;
2023-05-16 17:44:31 +02:00
normalizeToast ( )
2023-05-15 18:49:02 +02:00
$ ( '#generalToast' ) . addClass ( 'text-bg-success' ) ;
$ ( '#generalToast' ) . toast ( 'show' ) ;
$ ( '#generalToast' ) . children ( '.d-flex' ) . children ( '.toast-body' ) . html ( '<i class="bi bi-check2"></i> Changes saved successfully.' ) ;
setTimeout ( ( ) => {
$ ( '#generalToast' ) . toast ( 'hide' ) ; ;
window . location . reload ( ) ;
} , 1500 ) ;
} ,
error : function ( data ) {
console . log ( 'error' ) ;
// Hide overlay with spinner
$ ( '.loader-overlay' ) . removeClass ( 'loaderActive' ) ;
2023-05-16 17:44:31 +02:00
// Check for response code 409
normalizeToast ( )
2023-05-15 18:49:02 +02:00
$ ( '#generalToast' ) . addClass ( 'text-bg-danger' ) ;
$ ( '#generalToast' ) . toast ( 'show' ) ;
2023-05-16 17:44:31 +02:00
if ( data . status == 409 ) {
$ ( '#generalToast' ) . children ( '.d-flex' ) . children ( '.toast-body' ) . html ( '<i class="bi bi-exclamation-triangle-fill"></i> The element you tried to create already exists.' ) ;
} else {
$ ( '#generalToast' ) . children ( '.d-flex' ) . children ( '.toast-body' ) . html ( '<i class="bi bi-exclamation-triangle-fill"></i> Something went wrong. Please try again later.' ) ;
}
2023-05-15 18:49:02 +02:00
setTimeout ( ( ) => {
$ ( '#generalToast' ) . toast ( 'hide' ) ;
} , 3000 ) ;
}
} ) ;
} )
} ) ;