feat: 🎸 apply confirmation modal to deletes
This commit is contained in:
9 files changed
+314
-30
No files matched your search
@@ -0,0 +1,6 @@
|
||||
{{#each this.confirm.pending as |confirmation|}}
|
||||
{{yield
|
||||
confirmation.text
|
||||
(fn this.accept confirmation)
|
||||
(fn this.deny confirmation)}}
|
||||
{{/each}}
|
||||
@@ -0,0 +1,40 @@
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
+117
-22
@@ -1,10 +1,20 @@
|
||||
import Service from '@ember/service';
|
||||
import { getOwner } from '@ember/application';
|
||||
import { bind } from '@ember/runloop';
|
||||
import { Promise, resolve } from 'rsvp';
|
||||
import { A } from '@ember/array';
|
||||
import { tracked } from '@glimmer/tracking';
|
||||
import { defer, resolve } from 'rsvp';
|
||||
import { filterBy } from '@ember/object/computed';
|
||||
import config from '../config/environment';
|
||||
|
||||
export default class ConfirmService extends Service {
|
||||
/**
|
||||
* A simple service that emits Confirmation instances and
|
||||
* exposes a `pending` property to discover pending
|
||||
* confirmations.
|
||||
* @augments Service
|
||||
*
|
||||
* @example
|
||||
* const confirmation = confirmationService.getConfirmation();
|
||||
*/
|
||||
export default class ConfirmationsService extends Service {
|
||||
|
||||
// =attributes
|
||||
|
||||
@@ -15,18 +25,15 @@ export default class ConfirmService extends Service {
|
||||
enabled = config.enableConfirmService;
|
||||
|
||||
/**
|
||||
* The window's confirm function, bound to the window scope.
|
||||
* @type {Function}
|
||||
* @type {Array}
|
||||
*/
|
||||
get confirmFn() {
|
||||
// The Ember way of accessing globals...
|
||||
const document =
|
||||
getOwner(this).lookup('service:-document').documentElement;
|
||||
// defaultView === window, but without using globals directly
|
||||
const windowRef = document.parentNode.defaultView;
|
||||
const { confirm } = windowRef;
|
||||
return bind(windowRef, confirm);
|
||||
}
|
||||
confirmations = A();
|
||||
|
||||
/**
|
||||
* An array of pending confirmations (those where `done === false`).
|
||||
* @type {Array}
|
||||
*/
|
||||
@filterBy('confirmations', 'done', false) pending;
|
||||
|
||||
// =methods
|
||||
|
||||
@@ -39,15 +46,103 @@ export default class ConfirmService extends Service {
|
||||
*/
|
||||
confirm(text) {
|
||||
if (this.enabled) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (this.confirmFn(text)) {
|
||||
return resolve();
|
||||
} else {
|
||||
return reject();
|
||||
}
|
||||
});
|
||||
const confirmation = new Confirmation(text);
|
||||
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();
|
||||
* 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();
|
||||
* 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) {
|
||||
this.text = text;
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -83,3 +83,23 @@
|
||||
{{message.text}}
|
||||
</Rose::Notification>
|
||||
</EmberNotify>
|
||||
|
||||
<PendingConfirmations as |text accept deny|>
|
||||
<Rose::Dialog
|
||||
@heading={{t "actions.confirm"}}
|
||||
@modal={{true}}
|
||||
@dismiss={{deny}}
|
||||
as |dialog|>
|
||||
<dialog.body>
|
||||
<p>{{text}}</p>
|
||||
</dialog.body>
|
||||
<dialog.footer>
|
||||
<Rose::Button @style="primary" {{on "click" accept}}>
|
||||
{{t "actions.ok"}}
|
||||
</Rose::Button>
|
||||
<Rose::Button @style="secondary" {{on "click" deny}}>
|
||||
{{t "actions.cancel"}}
|
||||
</Rose::Button>
|
||||
</dialog.footer>
|
||||
</Rose::Dialog>
|
||||
</PendingConfirmations>
|
||||
Reference in new issue
Block a user