Skip to content

Commit

Permalink
new container register utilites and optimization on current user service
Browse files Browse the repository at this point in the history
  • Loading branch information
roncodes committed Sep 25, 2024
1 parent ecb18f4 commit 2146a83
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 6 deletions.
28 changes: 23 additions & 5 deletions addon/services/current-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,26 @@ export default class CurrentUserService extends Service.extend(Evented) {
this.set('user', user);
this.trigger('user.loaded', user);

console.log(user);

// Set permissions
this.permissions = this.getUserPermissions(user);

// Set environment from user option
this.theme.setEnvironment();

// Load user preferces
await this.loadPreferences();
// Set locale
if (user.locale) {
this.setLocale(user.locale);
} else {
await this.loadLocale();
}

// Load user whois data
await this.loadWhois();

// Load user organizations
await this.loadOrganizations();

// Optional callback
if (typeof options?.onUserResolved === 'function') {
Expand All @@ -118,9 +130,7 @@ export default class CurrentUserService extends Service.extend(Evented) {
async loadLocale() {
try {
const { locale } = await this.fetch.get('users/locale');
this.setOption('locale', locale);
this.intl.setLocale(locale);
this.locale = locale;
this.setLocale(locale);

return locale;
} catch (error) {
Expand Down Expand Up @@ -207,6 +217,14 @@ export default class CurrentUserService extends Service.extend(Evented) {
return this.getWhoisProperty(key);
}

setLocale(locale) {
this.setOption('locale', locale);
this.intl.setLocale(locale);
this.locale = locale;

return this;
}

setOption(key, value) {
key = `${this.optionsPrefix}${dasherize(key)}`;

Expand Down
1 change: 1 addition & 0 deletions addon/services/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ export default class FetchService extends Service {
const pathKeyVersion = new Date().toISOString();

const request = () => {
delete options.fromCache;
return this.get(path, query, options).then((response) => {
// cache the response
this.localCache.set(pathKey, response);
Expand Down
8 changes: 8 additions & 0 deletions addon/utils/register-component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { dasherize } from '@ember/string';

export default function registerComponent(owner, componentClass, options = {}) {
const registrationName = options && options.as ? `component:${options.as}` : `component:${dasherize(componentClass.name).replace('-component', '')}`;
if (!owner.hasRegistration(registrationName)) {
owner.register(registrationName, componentClass);
}
}
8 changes: 8 additions & 0 deletions addon/utils/register-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { dasherize } from '@ember/string';

export default function registerHelper(owner, name, helperFn) {
const registrationName = `helper:${dasherize(name)}`;
if (!owner.hasRegistration(registrationName)) {
owner.register(registrationName, helperFn);
}
}
1 change: 1 addition & 0 deletions app/utils/register-component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from '@fleetbase/ember-core/utils/register-component';
1 change: 1 addition & 0 deletions app/utils/register-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from '@fleetbase/ember-core/utils/register-helper';
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fleetbase/ember-core",
"version": "0.2.18",
"version": "0.2.19",
"description": "Provides all the core services, decorators and utilities for building a Fleetbase extension for the Console.",
"keywords": [
"fleetbase-core",
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/utils/register-component-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import registerComponent from 'dummy/utils/register-component';
import { module, test } from 'qunit';

module('Unit | Utility | register-component', function () {
// TODO: Replace this with your real tests.
test('it works', function (assert) {
let result = registerComponent();
assert.ok(result);
});
});
10 changes: 10 additions & 0 deletions tests/unit/utils/register-helper-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import registerHelper from 'dummy/utils/register-helper';
import { module, test } from 'qunit';

module('Unit | Utility | register-helper', function () {
// TODO: Replace this with your real tests.
test('it works', function (assert) {
let result = registerHelper();
assert.ok(result);
});
});

0 comments on commit 2146a83

Please sign in to comment.