2023-05-15 18:49:02 +02:00
function getDataForEdit ( name ) {
$ . ajax ( {
type : 'get' ,
url : ` /api/v1/categories?name= ${ name } ` ,
success : function ( data ) {
const result = JSON . parse ( data ) ;
// Get elements inside the editCategoryModal
const modal _categoryName = document . getElementById ( 'editCategoryModalName' ) ;
const modal _categoryDescription = document . getElementById ( 'editCategoryModalDescription' ) ;
const modal _categoryid = document . getElementById ( 'editCategoryModalId' ) ;
modal _categoryName . value = result . name ;
modal _categoryDescription . value = result . description ;
modal _categoryid . value = result . id ;
} ,
error : function ( data ) {
console . log ( '!!!! ERROR !!!!' , data ) ;
// Hide overlay with spinner
$ ( '.loader-overlay' ) . removeClass ( 'active' ) ;
// Close the modal
$ ( '.modal' ) . modal ( 'hide' ) ;
2023-05-16 17:44:31 +02:00
normalizeToast ( )
2023-05-15 18:49:02 +02:00
$ ( '#generalToast' ) . addClass ( 'text-bg-danger' ) ;
$ ( '#generalToast' ) . toast ( 'show' ) ;
$ ( '#generalToast' ) . children ( '.d-flex' ) . children ( '.toast-body' ) . html ( '<i class="bi bi-exclamation-triangle-fill"></i> Something went wrong. The category does no longer exist.' ) ;
setTimeout ( ( ) => {
window . location . reload ( ) ;
} , 3000 ) ;
}
} ) ;
}
2023-05-15 22:10:32 +02:00
function primeCreateNew ( ) {
const form = document . getElementById ( 'CategoryModalForm' ) ;
form . setAttribute ( 'method' , 'POST' ) ;
document . getElementById ( 'editCategoryModalLabel' ) . innerText = 'Create a new category' ;
return true ;
}
function primeEdit ( ) {
const form = document . getElementById ( 'CategoryModalForm' ) ;
document . getElementById ( 'editCategoryModalLabel' ) . innerText = 'Edit category' ;
form . setAttribute ( 'method' , 'PATCH' ) ;
return true ;
}
2023-05-15 18:49:02 +02:00
function deleteEntry ( id ) {
$ . ajax ( {
type : 'delete' ,
url : ` /api/v1/categories ` ,
data : { id : id } ,
success : function ( data ) {
$ ( '#staticBackdrop' ) . modal ( 'hide' ) ;
$ ( '#generalToast' ) . removeClass ( 'text-bg-primary' ) ;
$ ( '#generalToast' ) . addClass ( 'text-bg-success' ) ;
$ ( '#generalToast' ) . toast ( 'show' ) ;
$ ( '#generalToast' ) . children ( '.d-flex' ) . children ( '.toast-body' ) . html ( '<i class="bi bi-check2"></i> Category deleted successfully.' ) ;
confetti ( {
spread : 360 ,
ticks : 100 ,
gravity : 0.1 ,
decay : 0.94 ,
startVelocity : 30 ,
particleCount : 20 ,
scalar : 2 ,
shapes : [ 'text' ] ,
shapeOptions : {
text : {
value : [ '❌' , '🗑️' , '🚫' ]
}
}
} ) ;
setTimeout ( ( ) => {
$ ( '#generalToast' ) . toast ( 'hide' ) ;
$ ( '#generalToast' ) . removeClass ( 'text-bg-success' ) ;
$ ( '#generalToast' ) . addClass ( 'text-bg-primary' ) ;
window . location . reload ( ) ;
} , 2000 ) ;
} ,
error : function ( data ) {
// hide the staticBackdrop modal
$ ( '#staticBackdrop' ) . modal ( 'hide' ) ;
2023-05-16 17:44:31 +02:00
normalizeToast ( )
2023-05-15 18:49:02 +02:00
$ ( '#generalToast' ) . addClass ( 'text-bg-danger' ) ;
$ ( '#generalToast' ) . toast ( 'show' ) ;
$ ( '#generalToast' ) . children ( '.d-flex' ) . children ( '.toast-body' ) . html ( '<i class="bi bi-exclamation-triangle-fill"></i> Something went wrong. Please try again later.' ) ;
setTimeout ( ( ) => {
$ ( '#generalToast' ) . toast ( 'hide' ) ;
} , 3000 ) ;
}
} ) ;
}
function preFillDeleteModal ( name ) {
$ . ajax ( {
type : 'get' ,
url : ` /api/v1/categories?name= ${ name } ` ,
success : function ( data ) {
const result = JSON . parse ( data ) ;
// Get elements inside the editCategoryModal
const modal _categoryName = document . getElementById ( 'deleteNamePlaceholder' ) ;
const modal _deleteButton = document . getElementById ( 'deleteActionBtn' ) ;
//const modal_categoryDescription = document.getElementById('editCategoryModalDescription');
//const modal_categoryid = document.getElementById('editCategoryModalId');
modal _categoryName . innerText = result . name ;
modal _deleteButton . setAttribute ( 'onclick' , ` deleteEntry( ${ result . id } ) ` ) ;
//modal_categoryDescription.value = result.description;
//modal_categoryid.value = result.id;
} ,
error : function ( data ) {
console . log ( '!!!! ERROR !!!!' , data ) ;
document . getElementById ( 'deleteNamePlaceholder' ) . innerText = 'Deleted' ;
$ ( '#staticBackdrop' ) . modal ( 'hide' ) ;
2023-05-16 17:44:31 +02:00
normalizeToast ( )
2023-05-15 18:49:02 +02:00
$ ( '#generalToast' ) . addClass ( 'text-bg-danger' ) ;
$ ( '#generalToast' ) . toast ( 'show' ) ;
$ ( '#generalToast' ) . children ( '.d-flex' ) . children ( '.toast-body' ) . html ( '<i class="bi bi-exclamation-triangle-fill"></i> Something went wrong. The category does no longer exist.' ) ;
setTimeout ( ( ) => {
window . location . reload ( ) ;
} , 3000 ) ;
}
} ) ;
}