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

@@ -0,0 +1,6 @@
{{#each this.confirm.pending as |confirmation|}}
{{yield
confirmation
(fn this.accept confirmation)
(fn this.deny confirmation)}}
{{/each}}
@@ -0,0 +1,38 @@
import Component from '@glimmer/component';
import { inject as service } from '@ember/service';
import { action } from '@ember/object';
/**
* A component that yields for each pending confirmation in the confirmations
* service, passing the confirmation instance, a confirm action, and a dismiss
* action. Useful for rendering confirm modals and awaiting user action.
*/
export default class PendingConfirmationsComponent extends Component {
// =services
@service confirm;
// =actions
/**
* Accepts the passed confirmation.
* @param {Confirmation} confirmation
* @return {Confirmation}
*/
@action
accept(confirmation) {
confirmation.confirm();
return confirmation;
}
/**
* Denies the passed confirmation.
* @param {Confirmation} confirmation
* @return {Confirmation}
*/
@action
deny(confirmation) {
confirmation.dismiss();
return confirmation;
}
}
+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)
);
};
};
}
+146
View File
@@ -0,0 +1,146 @@
import Service from '@ember/service';
import { filterBy } from '@ember/object/computed';
import { A } from '@ember/array';
import { tracked } from '@glimmer/tracking';
import { defer, resolve } from 'rsvp';
import config from 'ember-get-config';
/**
* A simple service that emits Confirmation instances and
* exposes a `pending` property to discover pending
* confirmations.
* @augments Service
*
* @example
* const confirmation = confirmationService.confirm('Text', { type: 'discard' });
*/
export default class ConfirmationsService extends Service {
// =attributes
/**
* When the confirm service is disabled, it always returns resolving promises.
* @type {boolean}
*/
enabled = config.enableConfirmService;
/**
* @type {Array}
*/
confirmations = A();
/**
* An array of pending confirmations (those where `done === false`).
* @type {Array}
*/
@filterBy('confirmations', 'done', false) pending;
// =methods
/**
* When the service is enabled, returns a promise that resolves if the user
* accepts the confirmation, otherwise rejects. If the service is disabled,
* this method always returns a resolving promise.
* @param {string} text
* @param {object} options
* @return {Promise}
*/
confirm(text, options) {
if (this.enabled) {
const confirmation = new Confirmation(text, options);
this.confirmations.addObject(confirmation);
return confirmation;
} else {
return resolve();
}
}
}
/**
* A promise-like object that is either "confirmed" or "dismissed", represented
* internally by a promise that resolves or rejects. Confirmations may be used
* as promises via `then`, `catch`, and `finally`.
*
* Not intended for instantiation directly. Use the confirmation service
* to create confirmation instances.
*
* @example
* const confirmation = new Confirmation('Text', { type: 'discard' });
* confirmation.confirm();
* // confirmation.done now equals true
* confirmation
* .then(() => { }) // do something
* .catch(() => { }) // not called, since the confirmation is confirmed
* .finally(() => { }); // do something
*
* @example
* const confirmation = new Confirmation('Text', { type: 'discard' });
* confirmation.dismiss();
* // confirmation.done now equals true
* confirmation
* .then(() => { }) // not called, since the confirmation is dismissed
* .catch(() => { }) // do something
* .finally(() => { }); // do something
*/
class Confirmation {
// =properties
/**
* Indicates whether the confirmation has been confirmed or dismissed.
* Automatically set to `true` after `confirm` or `dismiss` are called.
* @type {Boolean}
*/
@tracked done = false;
#deferred = null;
/**
* Sets the type field to the specified type and sets up a defer.
* @param {String} type An optional type annotation for convenience.
* Has no effect on confirmation.
*/
constructor(text, options = {}) {
this.text = text;
this.options = options;
this.#deferred = defer();
}
/**
* Sets the confirmation to done and resolves the defer.
*/
confirm() {
this.done = true;
this.#deferred.resolve();
}
/**
* Sets the confirmation to done and rejects the defer.
*/
dismiss() {
this.done = true;
this.#deferred.reject();
}
/**
* Delegates to the internal defer promise, allowing the
* confirmation to be treated like a promise.
*/
then() {
return this.#deferred.promise.then(...arguments);
}
/**
* Delegates to the internal defer promise, allowing the
* confirmation to be treated like a promise.
*/
catch() {
return this.#deferred.promise.catch(...arguments);
}
/**
* Delegates to the internal defer promise, allowing the
* confirmation to be treated like a promise.
*/
finally() {
return this.#deferred.promise.finally(...arguments);
}
}