refactor: 💡 remove usage of route-action for app route (#2135)

✅ Closes: https://hashicorp.atlassian.net/browse/ICU-12615
This commit is contained in:
Lisbet Alvarez authored and GitHub committed 2024-03-04 10:13:59 -08:00
1 parent d07c33ca41
commit f068ac0b0c
3 files changed
+80 -44

No files matched your search

+63 -1
View File
@@ -5,11 +5,73 @@
import Controller from '@ember/controller';
import { inject as service } from '@ember/service';
import { getOwner } from '@ember/application';
import { action } from '@ember/object';
export default class ApplicationController extends Controller {
// =services
@service session;
@service flashMessages;
@service features;
@service featureEdition;
@service flashMessages;
// =actions
/**
* Delegates invalidation to the session service.
*/
@action
invalidateSession() {
this.session.invalidate();
}
/**
* Applies the specified color theme to the root ember element.
* @param {string} theme - "light", "dark", or nullish (system default)
*/
@action
toggleTheme(theme) {
const rootElementSelector = getOwner(this).rootElement;
const rootEl = getOwner(this)
.lookup('service:-document')
.querySelector(rootElementSelector);
this.session.set('data.theme', theme);
switch (theme) {
case 'light':
rootEl.classList.add('rose-theme-light');
rootEl.classList.remove('rose-theme-dark');
break;
case 'dark':
rootEl.classList.add('rose-theme-dark');
rootEl.classList.remove('rose-theme-light');
break;
default:
rootEl.classList.remove('rose-theme-dark');
rootEl.classList.remove('rose-theme-light');
}
}
/**
* Switches to specified edition.
* Options are oss, enterprise, and hcp.
* @param {string} edition
*/
@action
toggleEdition(edition) {
this.featureEdition.setEdition(edition);
}
/**
*
* @param {string} feature
*/
@action
toggleFeature(feature) {
if (this.features.isEnabled(feature)) {
this.features.disable(feature);
} else {
this.features.enable(feature);
}
}
}
+13 -39
View File
@@ -19,8 +19,6 @@ export default class ApplicationRoute extends Route {
@service confirm;
@service router;
@service intl;
@service features;
@service featureEdition;
// =attributes
@@ -64,33 +62,6 @@ export default class ApplicationRoute extends Route {
this.toggleTheme(theme);
}
// =actions
/**
* Delegates invalidation to the session service.
*/
@action
invalidateSession() {
this.session.invalidate();
}
/**
* Invalidates the session if a 401 error occurs and returns false to
* prevent further error handling.
* Returns true in all other cases, allowing error handling to occur (such
* as displaying the `error.hbs` template, if one exists).
* @param {Error} e
*/
@action
error(e) {
const isUnauthenticated = A(e?.errors)?.[0]?.isUnauthenticated;
if (isUnauthenticated) {
this.session.invalidate();
return false;
}
return true;
}
/**
* Applies the specified color theme to the root ember element.
* @param {string} theme - "light", "dark", or nullish (system default)
@@ -117,17 +88,20 @@ export default class ApplicationRoute extends Route {
}
}
/**
* Invalidates the session if a 401 error occurs and returns false to
* prevent further error handling.
* Returns true in all other cases, allowing error handling to occur (such
* as displaying the `error.hbs` template, if one exists).
* @param {Error} e
*/
@action
toggleEdition(edition) {
this.featureEdition.setEdition(edition);
}
@action
toggleFeature(feature) {
if (this.features.isEnabled(feature)) {
this.features.disable(feature);
} else {
this.features.enable(feature);
error(e) {
const isUnauthenticated = A(e?.errors)?.[0]?.isUnauthenticated;
if (isUnauthenticated) {
this.session.invalidate();
return false;
}
return true;
}
}
+4 -4
View File
@@ -27,7 +27,7 @@
</dropdown.link>
<dropdown.separator />
<dropdown.button {{on 'click' (route-action 'invalidateSession')}}>
<dropdown.button {{on 'click' this.invalidateSession}}>
{{t 'actions.deauthenticate'}}
</dropdown.button>
@@ -41,7 +41,7 @@
this.session.data.theme
'system-default-theme'
}}
@changed={{route-action 'toggleTheme'}}
@changed={{this.toggleTheme}}
as |radioGroup|
>
<dropdown.item>
@@ -83,7 +83,7 @@
<form.radioGroup
@name='edition'
@selectedValue={{this.featureEdition.edition}}
@changed={{route-action 'toggleEdition'}}
@changed={{this.toggleEdition}}
as |radioGroup|
>
{{#each this.featureEdition.editions as |edition|}}
@@ -105,7 +105,7 @@
<form.checkbox
@label={{feature}}
@checked={{feature-flag feature}}
@onChange={{route-action 'toggleFeature' feature}}
@onChange={{fn this.toggleFeature feature}}
/>
</dropdown.item>
{{/each}}