Currently, I am facing an issue with my AlertService that uses Sweet Alert. My goal is to confirm whether the user truly wants to delete something before proceeding with the action. Despite utilizing arrow functions and attempting bind(), I am encountering difficulties with callbacks context switching. Below is the code snippet:
AlertService
/**
* This function presents a generic confirmation alert.
* @param object containing properties to display
* @return {boolean} true if confirmed, otherwise null
*
* For example, pass 'warning' in the type attribute to generate a warning alert.
*/
confirmAlert(object: any, callback: Function): any {
swal({
title: object.title,
text: object.text,
type: object.type,
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'OK'
}).then(function (res) {
swal({
title: object.confirmTitle,
text: object.confirmText,
type: 'success'
});
return callback(true);
}, function (cancel) {
swal({
title: 'Cancelled',
text: 'Action cancelled',
type: 'error'
})
return callback(null);
});
}
UsersComponent
/**
* Removes the selected user after confirming with an alert.
* Updates the current users array displayed in a table.
*/
deleteUser() {
let usersToDelete = {
'users': this.users
};
this.alertService.confirmAlert(this.createMessages.alert.delete, (confirm) => {
if (confirm) {
this.webService.delete(environment.routes.users.userUrl, usersToDelete).subscribe(res => {
if (res.status == 200) {
this.alertService.okAlert(this.createMessages.alert.deleteSuccess);
}
});
}
});
this.refreshUsers();
}
The issue lies in the fact that the object containing the selected users does not reach the webService. Upon examining all logs, I identified this as the root cause of the problem, but I am unsure how to address it.