sweetalert icon indicating copy to clipboard operation
sweetalert copied to clipboard

Prevent run onClose hook when clicking cancel button

Open fershibli opened this issue 4 years ago • 0 comments

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.

fershibli avatar Oct 16 '19 13:10 fershibli