add default language observable to language context
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { UmbLanguageCollectionRepository } from '../collection/index.js';
|
||||
import type { UmbLanguageDetailModel } from '../types.js';
|
||||
import { UmbObjectState } from '@umbraco-cms/backoffice/observable-api';
|
||||
import { UmbArrayState, UmbObjectState, createObservablePart } from '@umbraco-cms/backoffice/observable-api';
|
||||
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
|
||||
import { UmbControllerBase } from '@umbraco-cms/backoffice/class-api';
|
||||
import { UmbContextToken } from '@umbraco-cms/backoffice/context-api';
|
||||
@@ -8,11 +8,17 @@ import type { UmbApi } from '@umbraco-cms/backoffice/extension-api';
|
||||
|
||||
export class UmbAppLanguageContext extends UmbControllerBase implements UmbApi {
|
||||
#languageCollectionRepository: UmbLanguageCollectionRepository;
|
||||
#languages: Array<UmbLanguageDetailModel> = [];
|
||||
#languages = new UmbArrayState<UmbLanguageDetailModel>([], (x) => x.unique);
|
||||
|
||||
#appLanguage = new UmbObjectState<UmbLanguageDetailModel | undefined>(undefined);
|
||||
appLanguage = this.#appLanguage.asObservable();
|
||||
|
||||
appLanguageCulture = this.#appLanguage.asObservablePart((x) => x?.unique);
|
||||
|
||||
appDefaultLanguage = createObservablePart(this.#languages.asObservable(), (languages) =>
|
||||
languages.find((language) => language.isDefault),
|
||||
);
|
||||
|
||||
getAppCulture() {
|
||||
return this.#appLanguage.getValue()?.unique;
|
||||
}
|
||||
@@ -25,7 +31,8 @@ export class UmbAppLanguageContext extends UmbControllerBase implements UmbApi {
|
||||
}
|
||||
|
||||
setLanguage(unique: string) {
|
||||
const language = this.#languages.find((x) => x.unique === unique);
|
||||
const languages = this.#languages.getValue();
|
||||
const language = languages.find((x) => x.unique === unique);
|
||||
this.#appLanguage.update(language);
|
||||
}
|
||||
|
||||
@@ -34,7 +41,7 @@ export class UmbAppLanguageContext extends UmbControllerBase implements UmbApi {
|
||||
|
||||
// TODO: make this observable / update when languages are added/removed/updated
|
||||
if (data) {
|
||||
this.#languages = data.items;
|
||||
this.#languages.setValue(data.items);
|
||||
|
||||
// If the app language is not set, set it to the default language
|
||||
if (!this.#appLanguage.getValue()) {
|
||||
@@ -44,7 +51,7 @@ export class UmbAppLanguageContext extends UmbControllerBase implements UmbApi {
|
||||
}
|
||||
|
||||
#initAppLanguage() {
|
||||
const defaultLanguage = this.#languages.find((x) => x.isDefault);
|
||||
const defaultLanguage = this.#languages.getValue().find((x) => x.isDefault);
|
||||
// TODO: do we always have a default language?
|
||||
// do we always get the default language on the first request, or could it be on page 2?
|
||||
// in that case do we then need an endpoint to get the default language?
|
||||
|
||||
Reference in New Issue
Block a user