Skip to content

Instantly share code, notes, and snippets.

@lenivene
Created October 23, 2019 16:04
Show Gist options
  • Save lenivene/0b5c1c2ac71f5fb83d2c2bce3f8d99ba to your computer and use it in GitHub Desktop.
Save lenivene/0b5c1c2ac71f5fb83d2c2bce3f8d99ba to your computer and use it in GitHub Desktop.
A CONTROLLER TO ALERTS USING SWEETALERT
import Swal from "sweetalert2";
export default {
success: function(options, callback) {
if ("string" == typeof options) {
options = { title: options };
}
var settings = Object.assign(
{},
{
title: null,
description: null
},
options
);
Swal.fire({
title: settings.title,
text: settings.description,
type: "success",
allowEscapeKey: true,
showCloseButton: false,
showCancelButton: false,
showConfirmButton: false,
timer: Number(options.timeout) || 2000,
onAfterClose: "function" == typeof callback ? callback : () => {}
});
if ("function" == typeof callback) {
callback();
}
},
error: function(options, callback) {
if ("string" == typeof options) {
options = { title: options };
}
var settings = Object.assign(
{},
{
title: null,
description: null
},
options
);
Swal.fire({
title: settings.title,
text: settings.description,
type: "error",
allowEscapeKey: true,
showCloseButton: false,
showCancelButton: false,
showConfirmButton: false,
timer: Number(options.timeout) || 2000,
onAfterClose: "function" == typeof callback ? callback : () => {}
});
},
/**
* Show modal confirm
*
* @param object (Required) Title `string` confirm or options.
* Ex: { title : 'Do you is World?', description: 'Answer, please!!' }
* @param function callback Check is cancelled or 'confirmed'.
*
* @return bool
*/
confirm: function(options, callback) {
if ("string" == typeof options) {
options = { title: options };
}
var settings = Object.assign(
{},
{
title: "Gostaria de continuar?",
description: "Se você clicou por engano, pode cancelar.",
iconName: "warning",
btnConfirmText: "Continuar",
btnConfirmClassName: "m-2 btn btn-success",
btnCancelText: "Cancelar",
btnCancelClassName: "m-2 btn btn-danger"
},
options
);
const swalMixin = Swal.mixin({
customClass: {
confirmButton: settings.btnConfirmClassName,
cancelButton: settings.btnCancelClassName
},
buttonsStyling: false
});
swalMixin
.fire({
title: settings.title,
text: settings.description,
type: settings.iconName,
showCancelButton: true,
confirmButtonText: settings.btnConfirmText,
cancelButtonText: settings.btnCancelText,
reverseButtons: false
})
.then(result => {
if ("function" == typeof callback) {
if (result.value) {
callback(true);
} else if (result.dismiss === Swal.DismissReason.cancel) {
callback(false);
}
}
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment