Skip to content

Instantly share code, notes, and snippets.

@zimejin
Forked from Bilkiss/confirmable.decorator.ts
Created January 19, 2022 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zimejin/67556e5c786f85aece84efedd5fbe064 to your computer and use it in GitHub Desktop.
Save zimejin/67556e5c786f85aece84efedd5fbe064 to your computer and use it in GitHub Desktop.
// we are now also importing SweetAlertOptions in our decorator
import Swal, {SweetAlertOptions} from 'sweetalert2';
// Confirmable is now a factory function, with an optional parameter object
export function Confirmable(options?: SweetAlertOptions) {
// our factory function will return our actual decorator function, but now we have
// an actual options object to configure our alert box :)
return (target: Object, propertyKey: string, descriptor: PropertyDescriptor) => {
// the usual, caching the original implementation
const originalMethod = descriptor.value;
// default values for our config, we’ll overwrite this with our options parameter
const config: SweetAlertOptions = {
title: 'Are you sure?',
html: 'Do you want to perform this action?',
showDenyButton: true,
confirmButtonText: 'Yes',
denyButtonText: 'No',
icon: 'question'
};
// overwrite any keys passed in to our decorator in the config object
if (options){
Object.keys(options).forEach( x => config[x] = options[x]);
}
// from here it’s the same as before. We write the new implementation
descriptor.value = async function (...args) {
// ask for confirmation
const res = await Swal.fire(config);
if (res.isConfirmed){
// run original implementation if user confirms
const result = originalMethod.apply(this, args);
return result;
}
};
return descriptor;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment