sweetalert
sweetalert copied to clipboard
Prevent run onClose hook when clicking cancel button
I need onClose to run when the user clicks outside the sweet alert or on confirm, but not on cancel. The cancel button makes an async call, which calls another sweet alert. I have tried the following setups: First this:
MySwal.fire({
type: 'success',
showCancelButton: userId === 'new',
onClose: () => {
if (userId === 'new') {
history.push(`/users`);
}
},
})
.then(myswallResult => {
if (myswallResult.dismiss === MySwal.DismissReason.cancel) {
return sendWelcomeEmail(result.data.id);
}
})
.then(emailResult => {
MySwal.fire({
type: 'success',
onClose: () => {
if (userId === 'new') {
history.push(`/users`);
}
},
});
})
.catch(err => {
MySwal.fire({
type: 'error',
onClose: () => {
if (userId === 'new') {
history.push(`/users`);
}
},
});
});
Then this:
MySwal.fire({
type: 'success',
showCancelButton: userId === 'new',
onClose: () => {
if (userId === 'new') {
history.push(`/users`);
}
},
})
.then(myswallResult => {
if (myswallResult.dismiss === MySwal.DismissReason.cancel) {
sendWelcomeEmail(result.data.id)
.then(emailResult => {
MySwal.fire({
type: 'success',
onClose: () => {
if (userId === 'new') {
history.push(`/users`);
}
},
});
})
.catch(err => {
MySwal.fire({
type: 'error',
onClose: () => {
if (userId === 'new') {
history.push(`/users`);
}
},
});
});
}
});
But instead of waiting the second sweet alert, it pushes to '/users' before.