refactor: 💡 port notification decorators to core addon
This commit is contained in:
28 files changed
+44
-25
No files matched your search
@@ -1,81 +0,0 @@
|
||||
import { getOwner } from '@ember/application';
|
||||
|
||||
/**
|
||||
* Decorates a method and, if the method does not error, shows a success
|
||||
* notification via the notify service. The text of the notification is derived
|
||||
* from the argument `notification`, which may be either a string or a function
|
||||
* that returns a string. This string may optionally be an intl key.
|
||||
* @param {string|function} notification
|
||||
*/
|
||||
export function notifySuccess(notification) {
|
||||
return function (_target, _propertyKey, desc) {
|
||||
const method = desc.value;
|
||||
desc.value = async function () {
|
||||
const owner = getOwner(this);
|
||||
const notifyService = owner.lookup('service:notify');
|
||||
const intlService = owner.lookup('service:intl');
|
||||
const candidateKey =
|
||||
typeof notification === 'function'
|
||||
? notification.apply(this, arguments)
|
||||
: notification;
|
||||
// As of now, we only use translated strings for notifications,
|
||||
// but this code would support both translated and arbitrary strings.
|
||||
// const text = intlService.exists(candidateKey)
|
||||
// ? intlService.t(candidateKey)
|
||||
// : candidateKey;
|
||||
const text = intlService.t(candidateKey);
|
||||
const value = await method.apply(this, arguments);
|
||||
notifyService.success(text);
|
||||
return value;
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Decorates a method and, if the method errors, shows an error
|
||||
* notification via the notify service. The text of the notification is derived
|
||||
* from the argument `notification`, which may be either a string or a function
|
||||
* that returns a string. This string may optionally be an intl key.
|
||||
*
|
||||
* If `options.catch` is `true`, any error occurring in the decorated method
|
||||
* will be rethrown after notification. The default is to rethrow errors.
|
||||
*
|
||||
* @param {string|function} notification
|
||||
* @param {object} options
|
||||
* @param {object} options.catch - defaults to false
|
||||
*/
|
||||
export function notifyError(notification, options = { catch: false }) {
|
||||
return function (_target, _propertyKey, desc) {
|
||||
const method = desc.value;
|
||||
desc.value = async function () {
|
||||
const owner = getOwner(this);
|
||||
const notifyService = owner.lookup('service:notify');
|
||||
//const intlService = owner.lookup('service:intl');
|
||||
try {
|
||||
return await method.apply(this, arguments);
|
||||
} catch (error) {
|
||||
// As of now all error notifications pass a function, but this code
|
||||
// could support either a function or a string.
|
||||
// const candidateKey = (typeof notification === 'function')
|
||||
// ? notification.apply(this, [error])
|
||||
// : notification;
|
||||
const candidateKey = notification.apply(this, [error]);
|
||||
|
||||
// As of now, we only use untranslated strings for error notifications,
|
||||
// but this code would support both translated and arbitrary strings.
|
||||
// const text = intlService.exists(candidateKey)
|
||||
// ? intlService.t(candidateKey)
|
||||
// : candidateKey;
|
||||
const text = candidateKey;
|
||||
|
||||
notifyService.error(text, { closeAfter: null });
|
||||
if (options.catch) {
|
||||
// squelch the error
|
||||
} else {
|
||||
// rethrow the error
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
import Route from '@ember/routing/route';
|
||||
import { inject as service } from '@ember/service';
|
||||
import { action } from '@ember/object';
|
||||
import { notifySuccess, notifyError } from '../../decorators/notify';
|
||||
import { notifySuccess, notifyError } from 'core/decorators/notify';
|
||||
|
||||
/**
|
||||
* The change password flow is associated only with password-type auth methods.
|
||||
|
||||
@@ -4,7 +4,7 @@ import { A } from '@ember/array';
|
||||
import { action } from '@ember/object';
|
||||
import loading from 'ember-loading/decorator';
|
||||
import { confirm } from 'core/decorators/confirm';
|
||||
import { notifySuccess, notifyError } from '../../decorators/notify';
|
||||
import { notifySuccess, notifyError } from 'core/decorators/notify';
|
||||
|
||||
export default class ScopesScopeRoute extends Route {
|
||||
// =services
|
||||
|
||||
@@ -3,7 +3,7 @@ import { inject as service } from '@ember/service';
|
||||
import { action } from '@ember/object';
|
||||
import loading from 'ember-loading/decorator';
|
||||
import { confirm } from 'core/decorators/confirm';
|
||||
import { notifySuccess, notifyError } from '../../../decorators/notify';
|
||||
import { notifySuccess, notifyError } from 'core/decorators/notify';
|
||||
|
||||
export default class ScopesScopeAuthMethodsRoute extends Route {
|
||||
// =services
|
||||
|
||||
@@ -3,7 +3,7 @@ import { action } from '@ember/object';
|
||||
import { inject as service } from '@ember/service';
|
||||
import loading from 'ember-loading/decorator';
|
||||
import { confirm } from 'core/decorators/confirm';
|
||||
import { notifySuccess, notifyError } from '../../../../../decorators/notify';
|
||||
import { notifySuccess, notifyError } from 'core/decorators/notify';
|
||||
|
||||
export default class ScopesScopeAuthMethodsAuthMethodAccountsRoute extends Route {
|
||||
// =services
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@ import loading from 'ember-loading/decorator';
|
||||
import {
|
||||
notifySuccess,
|
||||
notifyError,
|
||||
} from '../../../../../../../decorators/notify';
|
||||
} from 'core/decorators/notify';
|
||||
|
||||
export default class ScopesScopeAuthMethodsAuthMethodAccountsAccountSettingsRoute extends Route {
|
||||
// =services
|
||||
|
||||
@@ -3,7 +3,7 @@ import { inject as service } from '@ember/service';
|
||||
import { action } from '@ember/object';
|
||||
import loading from 'ember-loading/decorator';
|
||||
import { confirm } from 'core/decorators/confirm';
|
||||
import { notifySuccess, notifyError } from '../../../decorators/notify';
|
||||
import { notifySuccess, notifyError } from 'core/decorators/notify';
|
||||
|
||||
export default class ScopesScopeGroupsRoute extends Route {
|
||||
// =services
|
||||
|
||||
@@ -3,7 +3,7 @@ import { inject as service } from '@ember/service';
|
||||
import { action } from '@ember/object';
|
||||
import { hash } from 'rsvp';
|
||||
import loading from 'ember-loading/decorator';
|
||||
import { notifySuccess, notifyError } from '../../../../../decorators/notify';
|
||||
import { notifySuccess, notifyError } from 'core/decorators/notify';
|
||||
|
||||
export default class ScopesScopeGroupsGroupAddMembersRoute extends Route {
|
||||
// =services
|
||||
|
||||
@@ -4,7 +4,7 @@ import { inject as service } from '@ember/service';
|
||||
import { hash, all } from 'rsvp';
|
||||
import loading from 'ember-loading/decorator';
|
||||
import { confirm } from 'core/decorators/confirm';
|
||||
import { notifySuccess, notifyError } from '../../../../../decorators/notify';
|
||||
import { notifySuccess, notifyError } from 'core/decorators/notify';
|
||||
|
||||
export default class ScopesScopeGroupsGroupMembersRoute extends Route {
|
||||
// =services
|
||||
|
||||
@@ -3,7 +3,7 @@ import { inject as service } from '@ember/service';
|
||||
import { action } from '@ember/object';
|
||||
import loading from 'ember-loading/decorator';
|
||||
import { confirm } from 'core/decorators/confirm';
|
||||
import { notifySuccess, notifyError } from '../../../decorators/notify';
|
||||
import { notifySuccess, notifyError } from 'core/decorators/notify';
|
||||
|
||||
export default class ScopesScopeHostCatalogsRoute extends Route {
|
||||
// =services
|
||||
|
||||
@@ -3,7 +3,7 @@ import { inject as service } from '@ember/service';
|
||||
import { action } from '@ember/object';
|
||||
import loading from 'ember-loading/decorator';
|
||||
import { confirm } from 'core/decorators/confirm';
|
||||
import { notifySuccess, notifyError } from '../../../../../decorators/notify';
|
||||
import { notifySuccess, notifyError } from 'core/decorators/notify';
|
||||
|
||||
export default class ScopesScopeHostCatalogsHostCatalogHostSetsRoute extends Route {
|
||||
// =services
|
||||
|
||||
+1
-1
@@ -6,7 +6,7 @@ import loading from 'ember-loading/decorator';
|
||||
import {
|
||||
notifySuccess,
|
||||
notifyError,
|
||||
} from '../../../../../../../decorators/notify';
|
||||
} from 'core/decorators/notify';
|
||||
|
||||
export default class ScopesScopeHostCatalogsHostCatalogHostSetsHostSetAddHostsRoute extends Route {
|
||||
// =services
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@ import loading from 'ember-loading/decorator';
|
||||
import {
|
||||
notifySuccess,
|
||||
notifyError,
|
||||
} from '../../../../../../../decorators/notify';
|
||||
} from 'core/decorators/notify';
|
||||
|
||||
export default class ScopesScopeHostCatalogsHostCatalogHostSetsHostSetCreateAndAddHostRoute extends Route {
|
||||
// =services
|
||||
|
||||
+1
-1
@@ -7,7 +7,7 @@ import { confirm } from 'core/decorators/confirm';
|
||||
import {
|
||||
notifySuccess,
|
||||
notifyError,
|
||||
} from '../../../../../../../decorators/notify';
|
||||
} from 'core/decorators/notify';
|
||||
|
||||
export default class ScopesScopeHostCatalogsHostCatalogHostSetsHostSetHostsRoute extends Route {
|
||||
// =services
|
||||
|
||||
@@ -3,7 +3,7 @@ import { inject as service } from '@ember/service';
|
||||
import { action } from '@ember/object';
|
||||
import loading from 'ember-loading/decorator';
|
||||
import { confirm } from 'core/decorators/confirm';
|
||||
import { notifySuccess, notifyError } from '../../../../../decorators/notify';
|
||||
import { notifySuccess, notifyError } from 'core/decorators/notify';
|
||||
|
||||
export default class ScopesScopeHostCatalogsHostCatalogHostsRoute extends Route {
|
||||
// =services
|
||||
|
||||
@@ -3,7 +3,7 @@ import { inject as service } from '@ember/service';
|
||||
import { action } from '@ember/object';
|
||||
import loading from 'ember-loading/decorator';
|
||||
import { confirm } from 'core/decorators/confirm';
|
||||
import { notifySuccess, notifyError } from '../../../decorators/notify';
|
||||
import { notifySuccess, notifyError } from 'core/decorators/notify';
|
||||
|
||||
export default class ScopesScopeRolesRoute extends Route {
|
||||
// =services
|
||||
|
||||
@@ -3,7 +3,7 @@ import { inject as service } from '@ember/service';
|
||||
import { action } from '@ember/object';
|
||||
import { hash } from 'rsvp';
|
||||
import loading from 'ember-loading/decorator';
|
||||
import { notifySuccess, notifyError } from '../../../../../decorators/notify';
|
||||
import { notifySuccess, notifyError } from 'core/decorators/notify';
|
||||
|
||||
export default class ScopesScopeRolesRoleAddPrincipalsRoute extends Route {
|
||||
// =services
|
||||
|
||||
@@ -2,7 +2,7 @@ import Route from '@ember/routing/route';
|
||||
import { inject as service } from '@ember/service';
|
||||
import { action } from '@ember/object';
|
||||
import loading from 'ember-loading/decorator';
|
||||
import { notifySuccess, notifyError } from '../../../../../decorators/notify';
|
||||
import { notifySuccess, notifyError } from 'core/decorators/notify';
|
||||
|
||||
export default class ScopesScopeRolesRoleGrantsRoute extends Route {
|
||||
// =services
|
||||
|
||||
@@ -4,7 +4,7 @@ import { action } from '@ember/object';
|
||||
import { inject as service } from '@ember/service';
|
||||
import loading from 'ember-loading/decorator';
|
||||
import { confirm } from 'core/decorators/confirm';
|
||||
import { notifySuccess, notifyError } from '../../../../../decorators/notify';
|
||||
import { notifySuccess, notifyError } from 'core/decorators/notify';
|
||||
|
||||
export default class ScopesScopeRolesRolePrincipalsRoute extends Route {
|
||||
// =services
|
||||
|
||||
@@ -4,7 +4,7 @@ import { action } from '@ember/object';
|
||||
import { all, hash } from 'rsvp';
|
||||
import { task, timeout } from 'ember-concurrency';
|
||||
import { A } from '@ember/array';
|
||||
import { notifySuccess, notifyError } from '../../../decorators/notify';
|
||||
import { notifySuccess, notifyError } from 'core/decorators/notify';
|
||||
import config from '../../../config/environment';
|
||||
|
||||
const POLL_TIMEOUT_SECONDS = config.sessionPollingTimeoutSeconds;
|
||||
|
||||
@@ -3,7 +3,7 @@ import { inject as service } from '@ember/service';
|
||||
import { action } from '@ember/object';
|
||||
import loading from 'ember-loading/decorator';
|
||||
import { confirm } from 'core/decorators/confirm';
|
||||
import { notifySuccess, notifyError } from '../../../decorators/notify';
|
||||
import { notifySuccess, notifyError } from 'core/decorators/notify';
|
||||
|
||||
export default class ScopesScopeTargetsRoute extends Route {
|
||||
// =services
|
||||
|
||||
@@ -3,7 +3,7 @@ import { inject as service } from '@ember/service';
|
||||
import { action } from '@ember/object';
|
||||
import { all } from 'rsvp';
|
||||
import loading from 'ember-loading/decorator';
|
||||
import { notifySuccess, notifyError } from '../../../../../decorators/notify';
|
||||
import { notifySuccess, notifyError } from 'core/decorators/notify';
|
||||
|
||||
export default class ScopesScopeTargetsTargetAddHostSetsRoute extends Route {
|
||||
// =services
|
||||
|
||||
@@ -4,7 +4,7 @@ import { action } from '@ember/object';
|
||||
import { inject as service } from '@ember/service';
|
||||
import loading from 'ember-loading/decorator';
|
||||
import { confirm } from 'core/decorators/confirm';
|
||||
import { notifySuccess, notifyError } from '../../../../../decorators/notify';
|
||||
import { notifySuccess, notifyError } from 'core/decorators/notify';
|
||||
|
||||
export default class ScopesScopeTargetsTargetHostSetsRoute extends Route {
|
||||
// =services
|
||||
|
||||
@@ -3,7 +3,7 @@ import { inject as service } from '@ember/service';
|
||||
import { action } from '@ember/object';
|
||||
import loading from 'ember-loading/decorator';
|
||||
import { confirm } from 'core/decorators/confirm';
|
||||
import { notifySuccess, notifyError } from '../../../decorators/notify';
|
||||
import { notifySuccess, notifyError } from 'core/decorators/notify';
|
||||
|
||||
export default class ScopesScopeUsersRoute extends Route {
|
||||
// =services
|
||||
|
||||
@@ -3,7 +3,7 @@ import { action } from '@ember/object';
|
||||
import { inject as service } from '@ember/service';
|
||||
import { hash, all } from 'rsvp';
|
||||
import loading from 'ember-loading/decorator';
|
||||
import { notifySuccess, notifyError } from '../../../../../decorators/notify';
|
||||
import { notifySuccess, notifyError } from 'core/decorators/notify';
|
||||
|
||||
export default class ScopesScopeUsersUserAccountsRoute extends Route {
|
||||
// =services
|
||||
|
||||
@@ -3,7 +3,7 @@ import { inject as service } from '@ember/service';
|
||||
import { action } from '@ember/object';
|
||||
import { all } from 'rsvp';
|
||||
import loading from 'ember-loading/decorator';
|
||||
import { notifySuccess, notifyError } from '../../../../../decorators/notify';
|
||||
import { notifySuccess, notifyError } from 'core/decorators/notify';
|
||||
|
||||
export default class ScopesScopeUsersUserAddAccountsRoute extends Route {
|
||||
// =services
|
||||
|
||||
Reference in new issue
Block a user