3

Having some issues with the plugin Bootstrap File Input and SweetAlert2

This is for showing a dialog box to confirm if the user really wants to proceed with deleting the file.

I need to return a boolean for filepredelete event if I'm going to abort the deletion of the file. The function is synchronous but I'm using an asynchronous plugin SweetAlert2 with promises.

Here's my code:

$("#gallery-images").fileinput({
           ...

}).on("filepredelete", function(event, key, jqXHR, data) {

    swal({
        title: 'Are you sure you want to delete this image?',
        showCancelButton: true,
        confirmButtonText: 'Confirm',
        cancelButtonText: 'Cancel',
        type: 'warning',
    }).then((result) => {
        if(result.value){
            return false;
        }
        return true;
    });

});

I can't find a way to wait for the confirmation. I'm thinking of just always abort the deletion and just manually delete the file when the user confirmed. I need to refactor my code.

Limon Monte
  • 52,539
  • 45
  • 182
  • 213
Vince Parker
  • 173
  • 1
  • 3
  • 13
  • You can't wait for an async operation to return. I agree that the only way is to `just always abort the deletion and just manually delete the file when the user confirmed.` – Jacob Goh May 21 '18 at 06:31

1 Answers1

2

Try the promises, I've been struggling the same problem for 3 days and figured out, hope this help for some one.

$("#gallery-images").fileinput({
       ...

    }).on("filepredelete", function(event, key, jqXHR, data) {

    return new Promise(function(resolve, reject) {
            swal.fire({
                title: 'Are you sure?',
                text: 'You will not be able to recover this file!',
                icon: 'warning',
                showCancelButton: true,
                confirmButtonText: 'Yes, delete it!',
                cancelButtonText: 'No, keep it'
            }).then((result) => {
                if (result.value) {
                    resolve();
                }
            });
        });

    });

Enjoy!

Abdul Hakeem
  • 405
  • 4
  • 11