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: Support Language Permissions in Dictionary Section #2225

Merged
merged 2 commits into from
Aug 21, 2024
Merged
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
Expand Up @@ -5,26 +5,50 @@ import { UUITextareaEvent } from '@umbraco-cms/backoffice/external/uui';
import { css, html, customElement, state, repeat, ifDefined, unsafeHTML } from '@umbraco-cms/backoffice/external/lit';
import { UmbLitElement } from '@umbraco-cms/backoffice/lit-element';
import { UmbLanguageCollectionRepository, type UmbLanguageDetailModel } from '@umbraco-cms/backoffice/language';
import { UMB_CURRENT_USER_CONTEXT } from '@umbraco-cms/backoffice/current-user';

@customElement('umb-workspace-view-dictionary-editor')
export class UmbWorkspaceViewDictionaryEditorElement extends UmbLitElement {
@state()
private _dictionary?: UmbDictionaryDetailModel;

#languageCollectionRepository = new UmbLanguageCollectionRepository(this);

@state()
private _languages: Array<UmbLanguageDetailModel> = [];

@state()
private _currentUserLanguageAccess?: Array<string> = [];

@state()
private _currentUserHasAccessToAllLanguages?: boolean = false;

#languageCollectionRepository = new UmbLanguageCollectionRepository(this);
#workspaceContext!: typeof UMB_DICTIONARY_WORKSPACE_CONTEXT.TYPE;
#currentUserContext?: typeof UMB_CURRENT_USER_CONTEXT.TYPE;

override async connectedCallback() {
super.connectedCallback();
constructor() {
super();

this.consumeContext(UMB_DICTIONARY_WORKSPACE_CONTEXT, (_instance) => {
this.#workspaceContext = _instance;
this.#observeDictionary();
});

this.consumeContext(UMB_CURRENT_USER_CONTEXT, (context) => {
this.#currentUserContext = context;
this.#observeCurrentUserLanguageAccess();
});
}

#observeCurrentUserLanguageAccess() {
if (!this.#currentUserContext) return;

this.observe(this.#currentUserContext.languages, (languages) => {
this._currentUserLanguageAccess = languages;
});

this.observe(this.#currentUserContext.hasAccessToAllLanguages, (hasAccess) => {
this._currentUserHasAccessToAllLanguages = hasAccess;
});
}

override async firstUpdated() {
Expand All @@ -40,19 +64,11 @@ export class UmbWorkspaceViewDictionaryEditorElement extends UmbLitElement {
});
}

#renderTranslation(language: UmbLanguageDetailModel) {
if (!language.unique) return;

const translation = this._dictionary?.translations?.find((x) => x.isoCode === language.unique);

return html` <umb-property-layout label=${language.name ?? language.unique}>
<uui-textarea
slot="editor"
name=${language.unique}
label="translation"
@change=${this.#onTextareaChange}
value=${ifDefined(translation?.translation)}></uui-textarea>
</umb-property-layout>`;
#isReadOnly(culture: string | null) {
if (!this.#currentUserContext) return true;
if (!culture) return false;
if (this._currentUserHasAccessToAllLanguages) return false;
return !this._currentUserLanguageAccess?.includes(culture);
}

#onTextareaChange(e: Event) {
Expand All @@ -78,6 +94,22 @@ export class UmbWorkspaceViewDictionaryEditorElement extends UmbLitElement {
`;
}

#renderTranslation(language: UmbLanguageDetailModel) {
if (!language.unique) return;

const translation = this._dictionary?.translations?.find((x) => x.isoCode === language.unique);

return html` <umb-property-layout label=${language.name ?? language.unique}>
<uui-textarea
slot="editor"
name=${language.unique}
label="translation"
@change=${this.#onTextareaChange}
value=${ifDefined(translation?.translation)}
?readonly=${this.#isReadOnly(language.unique)}></uui-textarea>
</umb-property-layout>`;
}

static override styles = [
css`
:host {
Expand Down
Loading