refactor: 💡 port confirmation service and components into core

This commit is contained in:
Randall Morey committed 2020-11-18 13:19:46 -05:00
1 parent 23e83143a0
commit a19bdaa245
26 files changed
+59 -17

No files matched your search

+26
View File
@@ -0,0 +1,26 @@
import { getOwner } from '@ember/application';
/**
* Decorates a method whereby calls to the method will first request
* confirmation via the confirmation service. If the user accepts, the
* original method is executed. If the user denies, it is not.
*/
export function confirm(confirmationTextKey) {
return function (_target, _propertyKey, desc) {
const method = desc.value;
desc.value = function () {
const owner = getOwner(this);
const confirmService = owner.lookup('service:confirm');
const intl = owner.lookup('service:intl');
const text = intl.t(confirmationTextKey);
return (
confirmService
.confirm(text)
// If user confirmed, execute the decorated method
.then(() => method.apply(this, arguments))
// If the user denied, gracefully handle promise rejection with no-op
.catch((e) => e)
);
};
};
}