From b0b45716863d9a76803c94d878770aca63aeb886 Mon Sep 17 00:00:00 2001 From: Mads Rasmussen Date: Wed, 18 Dec 2024 13:11:13 +0100 Subject: [PATCH] Bugfix: missing links for invariant documents with hostnames (#17808) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * always render the links we get from the server * render information about links with no url * pass correct state value * Update document-workspace-view-info-links.element.ts * reuse empty link, culture markup * remove state from urls --------- Co-authored-by: Niels Lyngsø --- ...ument-workspace-view-info-links.element.ts | 128 +++++++++++------- 1 file changed, 78 insertions(+), 50 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/packages/documents/documents/workspace/views/info/document-workspace-view-info-links.element.ts b/src/Umbraco.Web.UI.Client/src/packages/documents/documents/workspace/views/info/document-workspace-view-info-links.element.ts index 5a50a927e217..983a30607f93 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/documents/documents/workspace/views/info/document-workspace-view-info-links.element.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/documents/documents/workspace/views/info/document-workspace-view-info-links.element.ts @@ -1,7 +1,8 @@ import { UmbDocumentUrlRepository } from '../../../repository/url/document-url.repository.js'; import { UMB_DOCUMENT_WORKSPACE_CONTEXT } from '../../document-workspace.context-token.js'; import type { UmbDocumentVariantOptionModel } from '../../../types.js'; -import { css, customElement, html, map, nothing, state, when } from '@umbraco-cms/backoffice/external/lit'; +import type { UmbDocumentUrlModel } from '../../../repository/url/types.js'; +import { css, customElement, html, nothing, repeat, state, when } from '@umbraco-cms/backoffice/external/lit'; import { UmbLitElement } from '@umbraco-cms/backoffice/lit-element'; import type { UmbEntityActionEvent } from '@umbraco-cms/backoffice/entity-action'; import { UmbRequestReloadStructureForEntityEvent } from '@umbraco-cms/backoffice/entity-action'; @@ -10,6 +11,12 @@ import { observeMultiple } from '@umbraco-cms/backoffice/observable-api'; import { DocumentVariantStateModel } from '@umbraco-cms/backoffice/external/backend-api'; import { debounce } from '@umbraco-cms/backoffice/utils'; +interface UmbDocumentInfoViewLink { + culture: string; + url: string | undefined; + state: DocumentVariantStateModel | null | undefined; +} + @customElement('umb-document-workspace-view-info-links') export class UmbDocumentWorkspaceViewInfoLinksElement extends UmbLitElement { #documentUrlRepository = new UmbDocumentUrlRepository(this); @@ -24,10 +31,12 @@ export class UmbDocumentWorkspaceViewInfoLinksElement extends UmbLitElement { private _variantOptions?: Array; @state() - private _lookup: Record = {}; + private _loading = false; @state() - private _loading = false; + private _links: Array = []; + + #urls: Array = []; #documentWorkspaceContext?: typeof UMB_DOCUMENT_WORKSPACE_CONTEXT.TYPE; #eventContext?: typeof UMB_ACTION_EVENT_CONTEXT.TYPE; @@ -61,38 +70,49 @@ export class UmbDocumentWorkspaceViewInfoLinksElement extends UmbLitElement { } }); - this.observe(context.variantOptions, (variantOptions) => (this._variantOptions = variantOptions)); + this.observe(context.variantOptions, (variantOptions) => { + this._variantOptions = variantOptions; + this.#setLinks(); + }); }); } + #setLinks() { + const possibleVariantCultures = this._variantOptions?.map((variantOption) => variantOption.culture) ?? []; + const possibleUrlCultures = this.#urls.map((link) => link.culture); + const possibleCultures = [...new Set([...possibleVariantCultures, ...possibleUrlCultures])].filter( + Boolean, + ) as string[]; + + const links: Array = possibleCultures.map((culture) => { + const url = this.#urls.find((link) => link.culture === culture)?.url; + const state = this._variantOptions?.find((variantOption) => variantOption.culture === culture)?.variant?.state; + return { culture, url, state }; + }); + + this._links = links; + } + async #requestUrls() { if (this._isNew) return; if (!this._unique) return; this._loading = true; - this._lookup = {}; + this.#urls = []; const { data } = await this.#documentUrlRepository.requestItems([this._unique]); if (data?.length) { const item = data[0]; - - item.urls.forEach((item) => { - if (item.culture && item.url) { - if (this._lookup[item.culture] == null) { - this._lookup[item.culture] = []; - } - this._lookup[item.culture].push(item.url); - } - }); - this.requestUpdate('_lookup'); + this.#urls = item.urls; + this.#setLinks(); } this._loading = false; } - #getStateLocalizationKey(variantOption: UmbDocumentVariantOptionModel) { - switch (variantOption.variant?.state) { + #getStateLocalizationKey(state: DocumentVariantStateModel | null | undefined): string { + switch (state) { case null: case undefined: case DocumentVariantStateModel.NOT_CREATED: @@ -136,52 +156,60 @@ export class UmbDocumentWorkspaceViewInfoLinksElement extends UmbLitElement { ${when( this._isNew, () => this.#renderNotCreated(), - () => this.#renderUrls(), + () => (this._links.length === 0 ? this.#renderNoLinks() : this.#renderLinks()), )} `; } #renderNotCreated() { + return html`${this.#renderEmptyLink(null, null)}`; + } + + #renderLinks() { + return html` + ${repeat( + this._links, + (link) => link.url, + (link) => this.#renderLink(link), + )} + `; + } + + #renderLink(link: UmbDocumentInfoViewLink) { + if (!link.url) { + return this.#renderEmptyLink(link.culture, link.state); + } + return html` - + + `; } - #renderUrls() { - if (!this._variantOptions?.length) return nothing; - return map(this._variantOptions, (variantOption) => this.#renderUrl(variantOption)); + #renderNoLinks() { + return html` ${this._variantOptions?.map((variantOption) => + this.#renderEmptyLink(variantOption.culture, variantOption.variant?.state), + )}`; } - #renderUrl(variantOption: UmbDocumentVariantOptionModel) { - const varies = !!variantOption.culture; - const culture = varies ? variantOption.culture! : variantOption.language.unique; - const urls = this._lookup[culture]; - return when( - urls && urls.length >= 1, - () => - html` ${urls.map( - (url) => - html` - - ${varies ? culture : nothing} - ${url} - - - `, - )}`, - () => html` - - `, - ); + #renderEmptyLink(culture: string | null, state: DocumentVariantStateModel | null | undefined) { + return html``; + } + + #renderLinkCulture(culture: string | null) { + if (!culture) return nothing; + if (this._links.length === 1) return nothing; + return html`${culture}`; } override disconnectedCallback(): void {