Skip to content

Instantly share code, notes, and snippets.

@JeroenVinke
Created March 24, 2016 08:38
Show Gist options
  • Save JeroenVinke/34725aad9b7d584a3ef2 to your computer and use it in GitHub Desktop.
Save JeroenVinke/34725aad9b7d584a3ef2 to your computer and use it in GitHub Desktop.
import {logger} from 'shared/components/utilities/logger';
export function withSpinner(target, key, descriptor) {
let ptr = descriptor.value;
descriptor.value = function(...args) {
if (this.spinner) {
this.spinner.setLoading(true);
}
return ptr.apply(this, args)
.then(() => {
if (this.spinner) {
this.spinner.setLoading(false);
}
});
};
return descriptor;
}
export function catchErrors(target, key, descriptor) {
let ptr = descriptor.value;
descriptor.value = function(...args) {
return ptr.apply(this, args)
.catch(e => {
if (logger) {
logger.error(e);
}
if (this.toastr) {
this.toastr.genericError();
}
});
};
return descriptor;
}
export function callModal(modalClass, transformer) {
return function(target, key, descriptor) {
let ptr = descriptor.value;
descriptor.value = function(...args) {
let transformed;
if (transformer) {
transformed = transformer.apply(this, args);
}
return this.modalService.show(modalClass, transformed)
.then(response => {
if (!response.wasCancelled) {
return ptr.call(this, response.output);
}
});
};
return descriptor;
};
}
export function withConfirmation(message = 'AreYouSure') {
return function(target, key, descriptor) {
let ptr = descriptor.value;
descriptor.value = function(...args) {
return this.sweetAlert.confirmDialog(message)
.then(response => {
if (!response.wasCancelled) {
return ptr.apply(this, args);
}
});
};
return descriptor;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment