Update sweetalert 1.x for sweetalert 2 + javascript promises

1

When updating the library SweetAlert it stopped working, debugging the error saw that it had many modifications that made version 1.x incompatible with the new version, so I would like help transcribing my deletarRegistro() function.

Function code with sweetalert 1.x

function deletarRegistro() {
    if (id_row > 0) {
        swal({
                title: "Você tem certeza disso?",
                text: "Uma vez deletado, não há como desfazer!",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "Sim, delete isto!",
                showLoaderOnConfirm: true,
                closeOnConfirm: false
            },
            function () {
                $.post('/api/tdocumentos/delete', {id: id_row})
                    .done(function () {
                        tbl_api.row('.info').remove().draw(false);
                        swal("Deletado!", "Seu registro foi deletado.", "success");
                        id_row = null;
                        formulario.reset();
                        $(tab_lista).click();
                    })
                    .fail(function (response) {
                        console.log(response.responseText);
                        swal("Erro!", response.responseText, "error");
                    })
                ;
            })
        ;
    } else {
        tbl_dependentes.effect('shake');
        return false;
    }
}

The SweetAlert 2.0 documentation for this example:

swal({
  title: "Are you sure?",
  text: "Once deleted, you will not be able to recover this imaginary 
file!",
  icon: "warning",
  buttons: true,
  dangerMode: true,
})
.then((willDelete) => {
  if (willDelete) {
    swal("Poof! Your imaginary file has been deleted!", {
      icon: "success",
    });
  } else {
    swal("Your imaginary file is safe!");
  }
});

Question: I would like to know how I can transcribe the function using promisses, and know what promisses use improves function.

    
asked by anonymous 13.02.2018 / 18:04

1 answer

0

After a few more hours of video lessons, I was able to solve this stop, follow the updated code to see how it was, unfortunately I can not explain the details: (

function deleteData() {
    if ( id_row > 0 ) {
        swal({
            title: "Você tem certeza disso?",
            text: "Uma vez deletado, não há como desfazer!",
            icon: "warning",
            dangerMode: true,
            buttons: {
                cancel: {
                    text: "Cancelar",
                    visible: true,
                    value: false,
                    closeModal: true,
                },
                confirm: true
            },
            closeModal: false,
            closeOnClickOutside: false,
        })
            .then((resolve) => {
                if (resolve) {
                    $.post('/api/sinodos/delete', {id: id_row})
                        .done(function () {
                            tbl_api.row('.active').remove().draw(false);
                            swal("Deletado!", "Seu registro foi deletado.", "success");
                            id_row = null;
                            cadastros_sinodos.reset();
                        })
                        .fail(function (response) {
                            console.log(response.responseText);
                            swal("Erro!", response.responseText, "error");
                        })
                    ;
                } else {
                    swal.close();
                }
            })
        ;
    }
}
    
13.02.2018 / 19:20