Recycle Bin: Trigger cache invalidation for trashed document/media items (#20483)

* Configure document/media items to listen for `Trashed` server-events for cache invalidation

* Fire reload event on restore destination tree/menu

* Removed "trashed" part of the code comment
This commit is contained in:
Lee Kelleher
2025-10-13 16:32:48 +01:00
committed by GitHub
parent 0a027dd80d
commit 3ac37f3686
6 changed files with 27 additions and 10 deletions

View File

@@ -53,7 +53,7 @@ export class UmbRestoreFromRecycleBinModalElement extends UmbModalBaseElement<
this.#setDestinationValue({
unique: null,
entityType: 'unknown',
entityType: this.data?.entityType ?? 'unknown',
});
}

View File

@@ -1,7 +1,11 @@
import { UMB_RESTORE_FROM_RECYCLE_BIN_MODAL } from './modal/restore-from-recycle-bin-modal.token.js';
import type { MetaEntityActionRestoreFromRecycleBinKind } from './types.js';
import { umbOpenModal } from '@umbraco-cms/backoffice/modal';
import { UmbEntityActionBase, UmbRequestReloadStructureForEntityEvent } from '@umbraco-cms/backoffice/entity-action';
import {
UmbEntityActionBase,
UmbRequestReloadChildrenOfEntityEvent,
UmbRequestReloadStructureForEntityEvent,
} from '@umbraco-cms/backoffice/entity-action';
import { UMB_ACTION_EVENT_CONTEXT } from '@umbraco-cms/backoffice/action';
/**
@@ -33,15 +37,20 @@ export class UmbRestoreFromRecycleBinEntityAction extends UmbEntityActionBase<Me
if (!actionEventContext) {
throw new Error('Event context not found.');
}
const event = new UmbRequestReloadStructureForEntityEvent({
const sourceEvent = new UmbRequestReloadStructureForEntityEvent({
unique: this.args.unique,
entityType: this.args.entityType,
});
actionEventContext.dispatchEvent(event);
actionEventContext.dispatchEvent(sourceEvent);
// TODO: reload destination
console.log(destination.unique, destination.entityType);
const destinationEvent = new UmbRequestReloadChildrenOfEntityEvent({
unique: destination.unique,
entityType: destination.entityType,
});
actionEventContext.dispatchEvent(destinationEvent);
}
}

View File

@@ -10,9 +10,10 @@ export class UmbManagementApiDocumentItemDataCacheInvalidationManager extends Um
constructor(host: UmbControllerHost) {
super(host, {
dataCache: documentItemCache,
/* The Document item model includes info about the Document Type.
/* The Document item model includes info about the Document Type.
We need to invalidate the cache for both Document and DocumentType events. */
eventSources: ['Umbraco:CMS:Document', 'Umbraco:CMS:DocumentType'],
eventTypes: ['Updated', 'Deleted', 'Trashed'],
});
}

View File

@@ -7,11 +7,13 @@ import { UmbControllerBase } from '@umbraco-cms/backoffice/class-api';
export interface UmbManagementApiDetailDataInvalidationManagerArgs<DetailResponseModelType> {
dataCache: UmbManagementApiDetailDataCache<DetailResponseModelType>;
eventSources: Array<string>;
eventTypes?: Array<string>;
}
export class UmbManagementApiDetailDataCacheInvalidationManager<DetailResponseModelType> extends UmbControllerBase {
protected _dataCache: UmbManagementApiDetailDataCache<DetailResponseModelType>;
#eventSources: Array<string>;
#eventTypes: Array<string>;
#serverEventContext?: typeof UMB_MANAGEMENT_API_SERVER_EVENT_CONTEXT.TYPE;
constructor(
@@ -22,6 +24,7 @@ export class UmbManagementApiDetailDataCacheInvalidationManager<DetailResponseMo
{
this._dataCache = args.dataCache;
this.#eventSources = args.eventSources;
this.#eventTypes = args.eventTypes ?? ['Updated', 'Deleted'];
this.consumeContext(UMB_MANAGEMENT_API_SERVER_EVENT_CONTEXT, (context) => {
this.#serverEventContext = context;
@@ -42,7 +45,7 @@ export class UmbManagementApiDetailDataCacheInvalidationManager<DetailResponseMo
#observeServerEvents() {
this.observe(
this.#serverEventContext?.byEventSourcesAndEventTypes(this.#eventSources, ['Updated', 'Deleted']),
this.#serverEventContext?.byEventSourcesAndEventTypes(this.#eventSources, this.#eventTypes),
(event) => {
if (!event) return;
this._onServerEvent(event);

View File

@@ -7,11 +7,13 @@ import { UmbControllerBase } from '@umbraco-cms/backoffice/class-api';
export interface UmbManagementApiItemDataInvalidationManagerArgs<ItemResponseModelType> {
dataCache: UmbManagementApiItemDataCache<ItemResponseModelType>;
eventSources: Array<string>;
eventTypes?: Array<string>;
}
export class UmbManagementApiItemDataCacheInvalidationManager<ItemResponseModelType> extends UmbControllerBase {
protected _dataCache: UmbManagementApiItemDataCache<ItemResponseModelType>;
#eventSources: Array<string>;
#eventTypes: Array<string>;
#serverEventContext?: typeof UMB_MANAGEMENT_API_SERVER_EVENT_CONTEXT.TYPE;
constructor(host: UmbControllerHost, args: UmbManagementApiItemDataInvalidationManagerArgs<ItemResponseModelType>) {
@@ -19,6 +21,7 @@ export class UmbManagementApiItemDataCacheInvalidationManager<ItemResponseModelT
{
this._dataCache = args.dataCache;
this.#eventSources = args.eventSources;
this.#eventTypes = args.eventTypes ?? ['Updated', 'Deleted'];
this.consumeContext(UMB_MANAGEMENT_API_SERVER_EVENT_CONTEXT, (context) => {
this.#serverEventContext = context;
@@ -40,7 +43,7 @@ export class UmbManagementApiItemDataCacheInvalidationManager<ItemResponseModelT
#observeServerEvents() {
// Invalidate cache entries when entities are updated or deleted
this.observe(
this.#serverEventContext?.byEventSourcesAndEventTypes(this.#eventSources, ['Updated', 'Deleted']),
this.#serverEventContext?.byEventSourcesAndEventTypes(this.#eventSources, this.#eventTypes),
(event) => {
if (!event) return;
this._onServerEvent(event);

View File

@@ -10,9 +10,10 @@ export class UmbManagementApiMediaItemDataCacheInvalidationManager extends UmbMa
constructor(host: UmbControllerHost) {
super(host, {
dataCache: mediaItemCache,
/* The Media item model includes info about the Media Type.
/* The Media item model includes info about the Media Type.
We need to invalidate the cache for both Media and MediaType events. */
eventSources: ['Umbraco:CMS:Media', 'Umbraco:CMS:MediaType'],
eventTypes: ['Updated', 'Deleted', 'Trashed'],
});
}