From b3f77fc067f59440d811eba7f4518104375822a4 Mon Sep 17 00:00:00 2001 From: Mads Rasmussen Date: Thu, 16 May 2024 14:32:28 +0200 Subject: [PATCH] listen and respond on structure reload requests --- .../default/collection-default.context.ts | 44 ++++++++++++++++++- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/packages/core/collection/default/collection-default.context.ts b/src/Umbraco.Web.UI.Client/src/packages/core/collection/default/collection-default.context.ts index 76f087c353..d71b2c56f4 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/core/collection/default/collection-default.context.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/core/collection/default/collection-default.context.ts @@ -6,6 +6,7 @@ import type { UmbCollectionContext, UmbCollectionLayoutConfiguration, } from '../types.js'; +import type { UmbEntityModel } from '@umbraco-cms/backoffice/entity'; import { umbExtensionsRegistry } from '@umbraco-cms/backoffice/extension-registry'; import { UmbArrayState, UmbNumberState, UmbObjectState } from '@umbraco-cms/backoffice/observable-api'; import { UmbChangeEvent } from '@umbraco-cms/backoffice/event'; @@ -17,12 +18,15 @@ import type { ManifestCollection, ManifestRepository } from '@umbraco-cms/backof import type { UmbApi } from '@umbraco-cms/backoffice/extension-api'; import type { UmbCollectionFilterModel, UmbCollectionRepository } from '@umbraco-cms/backoffice/collection'; import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api'; +import { UmbRequestReloadStructureForEntityEvent } from '@umbraco-cms/backoffice/entity-action'; +import type { UmbActionEventContext } from '@umbraco-cms/backoffice/action'; +import { UMB_ACTION_EVENT_CONTEXT } from '@umbraco-cms/backoffice/action'; const LOCAL_STORAGE_KEY = 'umb-collection-view'; export class UmbDefaultCollectionContext< - CollectionItemType = any, - FilterModelType extends UmbCollectionFilterModel = any, + CollectionItemType extends UmbEntityModel = UmbEntityModel, + FilterModelType extends UmbCollectionFilterModel = UmbCollectionFilterModel, > extends UmbContextBase implements UmbCollectionContext, UmbApi @@ -63,6 +67,8 @@ export class UmbDefaultCollectionContext< this.#initialized ? resolve() : (this.#initResolver = resolve); }); + #actionEventContext: UmbActionEventContext | undefined; + constructor(host: UmbControllerHost, defaultViewAlias: string, defaultFilter: Partial = {}) { super(host, UMB_COLLECTION_CONTEXT); @@ -70,6 +76,23 @@ export class UmbDefaultCollectionContext< this.#defaultFilter = defaultFilter; this.pagination.addEventListener(UmbChangeEvent.TYPE, this.#onPageChange); + this.#listenToEntityEvents(); + } + + async #listenToEntityEvents() { + this.consumeContext(UMB_ACTION_EVENT_CONTEXT, (context) => { + this.#actionEventContext = context; + + context?.removeEventListener( + UmbRequestReloadStructureForEntityEvent.TYPE, + this.#onReloadStructureRequest as unknown as EventListener, + ); + + context?.addEventListener( + UmbRequestReloadStructureForEntityEvent.TYPE, + this.#onReloadStructureRequest as unknown as EventListener, + ); + }); } #configured = false; @@ -222,6 +245,23 @@ export class UmbDefaultCollectionContext< localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(layouts)); } + + #onReloadStructureRequest = async (event: UmbRequestReloadStructureForEntityEvent) => { + const items = this.#items.getValue(); + const hasItem = items.some((item) => item.unique === event.getUnique()); + if (hasItem) { + this.requestCollection(); + } + }; + + destroy(): void { + this.#actionEventContext?.removeEventListener( + UmbRequestReloadStructureForEntityEvent.TYPE, + this.#onReloadStructureRequest as unknown as EventListener, + ); + + super.destroy(); + } } export const UMB_COLLECTION_CONTEXT = new UmbContextToken('UmbCollectionContext');