Skip to content

Commit e6cc9cf

Browse files
committed
reload items
1 parent 4ff07d3 commit e6cc9cf

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

src/Umbraco.Web.UI.Client/src/packages/core/repository/repository-items.manager.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { UmbArrayState } from '@umbraco-cms/backoffice/observable-api';
44
import { type ManifestRepository, umbExtensionsRegistry } from '@umbraco-cms/backoffice/extension-registry';
55
import { UmbExtensionApiInitializer } from '@umbraco-cms/backoffice/extension-api';
66
import { UmbControllerBase } from '@umbraco-cms/backoffice/class-api';
7+
import { UMB_ACTION_EVENT_CONTEXT } from '@umbraco-cms/backoffice/action';
8+
import { UmbEntityUpdatedEvent } from '@umbraco-cms/backoffice/entity-action';
79

810
const ObserveRepositoryAlias = Symbol();
911

@@ -14,6 +16,7 @@ export class UmbRepositoryItemsManager<ItemType extends { unique: string }> exte
1416

1517
#init: Promise<unknown>;
1618
#currentRequest?: Promise<unknown>;
19+
#eventContext?: typeof UMB_ACTION_EVENT_CONTEXT.TYPE;
1720

1821
// the init promise is used externally for recognizing when the manager is ready.
1922
public get init() {
@@ -70,6 +73,20 @@ export class UmbRepositoryItemsManager<ItemType extends { unique: string }> exte
7073
},
7174
null,
7275
);
76+
77+
this.consumeContext(UMB_ACTION_EVENT_CONTEXT, (context) => {
78+
this.#eventContext = context;
79+
80+
this.#eventContext.removeEventListener(
81+
UmbEntityUpdatedEvent.TYPE,
82+
this.#onEntityDetailUpdatedEvent as unknown as EventListener,
83+
);
84+
85+
this.#eventContext.addEventListener(
86+
UmbEntityUpdatedEvent.TYPE,
87+
this.#onEntityDetailUpdatedEvent as unknown as EventListener,
88+
);
89+
});
7390
}
7491

7592
getUniques(): Array<string> {
@@ -122,6 +139,25 @@ export class UmbRepositoryItemsManager<ItemType extends { unique: string }> exte
122139
}
123140
}
124141

142+
async #reloadItem(unique: string): Promise<void> {
143+
await this.#init;
144+
if (!this.repository) throw new Error('Repository is not initialized');
145+
146+
const { data } = await this.repository.requestItems([unique]);
147+
148+
if (data) {
149+
const items = this.getItems();
150+
const item = items.find((item) => this.#getUnique(item) === unique);
151+
152+
if (item) {
153+
const index = items.indexOf(item);
154+
const newItems = [...items];
155+
newItems[index] = data[0];
156+
this.#items.setValue(this.#sortByUniques(newItems));
157+
}
158+
}
159+
}
160+
125161
#sortByUniques(data: Array<ItemType>): Array<ItemType> {
126162
const uniques = this.getUniques();
127163
return [...data].sort((a, b) => {
@@ -130,4 +166,25 @@ export class UmbRepositoryItemsManager<ItemType extends { unique: string }> exte
130166
return aIndex - bIndex;
131167
});
132168
}
169+
170+
#onEntityDetailUpdatedEvent = (event: UmbEntityUpdatedEvent) => {
171+
const eventUnique = event.getUnique();
172+
173+
const items = this.getItems();
174+
if (items.length === 0) return;
175+
176+
// Ignore events if the entity is not in the list of items.
177+
const item = items.find((item) => this.#getUnique(item) === eventUnique);
178+
if (!item) return;
179+
180+
this.#reloadItem(item.unique);
181+
};
182+
183+
override destroy(): void {
184+
this.#eventContext?.removeEventListener(
185+
UmbEntityUpdatedEvent.TYPE,
186+
this.#onEntityDetailUpdatedEvent as unknown as EventListener,
187+
);
188+
super.destroy();
189+
}
133190
}

0 commit comments

Comments
 (0)