Skip to content

Commit ac75087

Browse files
authored
Merge pull request #69 from fleetbase/dev-v0.3.9
v0.3.9 ~ New language service, and improvements to currentUser servic…
2 parents 2999dcc + 1405f71 commit ac75087

File tree

7 files changed

+125
-1
lines changed

7 files changed

+125
-1
lines changed

addon/exports/host-services.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export const hostServices = [
2121
'universe',
2222
'intl',
2323
'abilities',
24+
'language',
2425
{ hostRouter: 'router' },
2526
];
2627

addon/exports/services.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export const services = [
2323
'universe',
2424
'intl',
2525
'abilities',
26+
'language',
2627
];
2728

2829
export default services;

addon/services/current-user.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export default class CurrentUserService extends Service.extend(Evented) {
2626
@tracked locale = 'en-us';
2727

2828
@storageFor('user-options') options;
29+
@storageFor('local-cache') cache;
2930
@alias('userSnapshot.id') id;
3031
@alias('userSnapshot.name') name;
3132
@alias('userSnapshot.phone') phone;
@@ -260,6 +261,11 @@ export default class CurrentUserService extends Service.extend(Evented) {
260261
const whois = this.getOption('whois');
261262

262263
if (!whois || typeof whois !== 'object') {
264+
// Fallback to lookup/whois in local-cache
265+
const cachedWhois = this.cache.get('lookup/whois');
266+
if (cachedWhois) {
267+
return get(cachedWhois, prop);
268+
}
263269
return null;
264270
}
265271

addon/services/language.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import Service from '@ember/service';
2+
import { tracked } from '@glimmer/tracking';
3+
import { inject as service } from '@ember/service';
4+
import { action } from '@ember/object';
5+
import { debug } from '@ember/debug';
6+
import { task } from 'ember-concurrency';
7+
8+
export default class LanguageService extends Service {
9+
@service intl;
10+
@service fetch;
11+
@tracked locales = [];
12+
@tracked countries = [];
13+
@tracked currentLocale;
14+
@tracked availableLocales = {};
15+
16+
get languages() {
17+
const availableLanguages = [];
18+
for (let key in this.availableLocales) {
19+
if (!key || !this.availableLocales[key]) continue;
20+
21+
availableLanguages.push({
22+
...this.availableLocales[key],
23+
locale: key,
24+
});
25+
}
26+
27+
return availableLanguages;
28+
}
29+
30+
constructor() {
31+
super(...arguments);
32+
33+
this.locales = this.intl.locales;
34+
this.currentLocale = this.intl.primaryLocale;
35+
this.loadAvailableCountries.perform();
36+
37+
// Check for locale change
38+
this.intl.onLocaleChanged(() => {
39+
this.currentLocale = this.intl.primaryLocale;
40+
});
41+
}
42+
43+
@action changeLocale(selectedLocale) {
44+
this.currentLocale = selectedLocale;
45+
this.intl.setLocale(selectedLocale);
46+
this.saveUserLocale.perform(selectedLocale);
47+
}
48+
49+
@task *loadAvailableCountries() {
50+
try {
51+
this.countries = yield this.fetch.get(
52+
'lookup/countries',
53+
{ columns: ['name', 'cca2', 'flag', 'emoji', 'languages'] },
54+
{ fromCache: true, expirationInterval: 1, expirationIntervalUnit: 'week' }
55+
);
56+
this.availableLocales = this._createAvailableLocaleMap();
57+
} catch (error) {
58+
debug('Locale Error: ' + error.message);
59+
}
60+
}
61+
62+
@task *saveUserLocale(locale) {
63+
try {
64+
yield this.fetch.post('users/locale', { locale });
65+
} catch (err) {
66+
debug('[LanguageService] Unable to save user locale: ' + err.message);
67+
}
68+
}
69+
70+
getLanguage(languageName, options = { prop: 'language' }) {
71+
const { prop } = options;
72+
73+
return this.languages?.find((l) => l[prop] === languageName) ?? null;
74+
}
75+
76+
hasLanguage(languageName, options = { prop: 'language' }) {
77+
return this.getLanguage(languageName, options) !== null;
78+
}
79+
80+
_createAvailableLocaleMap() {
81+
const localeMap = {};
82+
83+
for (let i = 0; i < this.locales.length; i++) {
84+
const locale = this.locales.objectAt(i);
85+
86+
localeMap[locale] = this._findCountryDataForLocale(locale);
87+
}
88+
89+
return localeMap;
90+
}
91+
92+
_findCountryDataForLocale(locale) {
93+
const localeCountry = locale.split('-')[1];
94+
const country = this.countries.find((country) => country.cca2.toLowerCase() === localeCountry);
95+
96+
if (country) {
97+
// get the language
98+
country.language = Object.values(country.languages)[0];
99+
}
100+
101+
return country;
102+
}
103+
}

app/services/language.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from '@fleetbase/ember-core/services/language';

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@fleetbase/ember-core",
3-
"version": "0.3.8",
3+
"version": "0.3.9",
44
"description": "Provides all the core services, decorators and utilities for building a Fleetbase extension for the Console.",
55
"keywords": [
66
"fleetbase-core",
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { module, test } from 'qunit';
2+
import { setupTest } from 'dummy/tests/helpers';
3+
4+
module('Unit | Service | language', function (hooks) {
5+
setupTest(hooks);
6+
7+
// TODO: Replace this with your real tests.
8+
test('it exists', function (assert) {
9+
let service = this.owner.lookup('service:language');
10+
assert.ok(service);
11+
});
12+
});

0 commit comments

Comments
 (0)