Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Simpler app language request #18278

Open
wants to merge 4 commits into
base: v15/dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { UmbLanguageCollectionRepository } from '../collection/index.js';
import type { UmbLanguageDetailModel } from '../types.js';
import { UMB_APP_LANGUAGE_CONTEXT } from './app-language.context-token.js';
import { UmbArrayState, UmbObjectState, createObservablePart } from '@umbraco-cms/backoffice/observable-api';
import { UmbArrayState, UmbObjectState } from '@umbraco-cms/backoffice/observable-api';
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
import { UmbContextBase } from '@umbraco-cms/backoffice/class-api';
import type { UmbApi } from '@umbraco-cms/backoffice/extension-api';
import { UMB_AUTH_CONTEXT } from '@umbraco-cms/backoffice/auth';
import { UmbReadOnlyStateManager } from '@umbraco-cms/backoffice/utils';
import { UMB_CURRENT_USER_CONTEXT } from '@umbraco-cms/backoffice/current-user';

// TODO: Make a store for the App Languages.
// TODO: Implement default language end-point, in progress at backend team, so we can avoid getting all languages.
export class UmbAppLanguageContext extends UmbContextBase<UmbAppLanguageContext> implements UmbApi {
#languagesResolve!: () => void;
#languagesPromise = new Promise<void>((resolve) => {
this.#languagesResolve = resolve;
});
#languages = new UmbArrayState<UmbLanguageDetailModel>([], (x) => x.unique);

#appLanguage = new UmbObjectState<UmbLanguageDetailModel | undefined>(undefined);
Expand All @@ -20,9 +23,16 @@ export class UmbAppLanguageContext extends UmbContextBase<UmbAppLanguageContext>

public readonly appLanguageReadOnlyState = new UmbReadOnlyStateManager(this);

public readonly appDefaultLanguage = createObservablePart(this.#languages.asObservable(), (languages) =>
public readonly appDefaultLanguage = this.#languages.asObservablePart((languages) =>
languages.find((language) => language.isDefault),
);
public readonly appMandatoryLanguages = this.#languages.asObservablePart((languages) =>
languages.filter((language) => language.isMandatory),
);
async getMandatoryLanguages() {
await this.#languagesPromise;
return this.#languages.getValue().filter((language) => language.isMandatory);
}

public readonly moreThanOneLanguage = this.#languages.asObservablePart((x) => x.length > 1);

Expand All @@ -36,13 +46,7 @@ export class UmbAppLanguageContext extends UmbContextBase<UmbAppLanguageContext>
constructor(host: UmbControllerHost) {
super(host, UMB_APP_LANGUAGE_CONTEXT);

// TODO: We need to ensure this request is called every time the user logs in, but this should be done somewhere across the app and not here [JOV]
this.consumeContext(UMB_AUTH_CONTEXT, (authContext) => {
this.observe(authContext.isAuthorized, (isAuthorized) => {
if (!isAuthorized) return;
this.#observeLanguages();
});
});
this.#requestLanguages();

this.consumeContext(UMB_CURRENT_USER_CONTEXT, (context) => {
this.observe(context.languages, (languages) => {
Expand Down Expand Up @@ -85,12 +89,13 @@ export class UmbAppLanguageContext extends UmbContextBase<UmbAppLanguageContext>
this.#setIsReadOnly();
}

async #observeLanguages() {
async #requestLanguages() {
const { data } = await this.#languageCollectionRepository.requestCollection({});

// TODO: make this observable / update when languages are added/removed/updated
if (data) {
this.#languages.setValue(data.items);
this.#languagesResolve();

// If the app language is not set, set it to the default language
if (!this.#appLanguage.getValue()) {
Expand Down
Loading