t4t5 / sweetalert

A beautiful replacement for JavaScript's "alert"
https://sweetalert.js.org
MIT License
22.4k stars 2.84k forks source link

Prevent run onClose hook when clicking cancel button #915

Open fershibli opened 4 years ago

fershibli commented 4 years ago

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.