@@ -4,6 +4,8 @@ import { UmbArrayState } from '@umbraco-cms/backoffice/observable-api';
4
4
import { type ManifestRepository , umbExtensionsRegistry } from '@umbraco-cms/backoffice/extension-registry' ;
5
5
import { UmbExtensionApiInitializer } from '@umbraco-cms/backoffice/extension-api' ;
6
6
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' ;
7
9
8
10
const ObserveRepositoryAlias = Symbol ( ) ;
9
11
@@ -14,6 +16,7 @@ export class UmbRepositoryItemsManager<ItemType extends { unique: string }> exte
14
16
15
17
#init: Promise < unknown > ;
16
18
#currentRequest?: Promise < unknown > ;
19
+ #eventContext?: typeof UMB_ACTION_EVENT_CONTEXT . TYPE ;
17
20
18
21
// the init promise is used externally for recognizing when the manager is ready.
19
22
public get init ( ) {
@@ -70,6 +73,20 @@ export class UmbRepositoryItemsManager<ItemType extends { unique: string }> exte
70
73
} ,
71
74
null ,
72
75
) ;
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
+ } ) ;
73
90
}
74
91
75
92
getUniques ( ) : Array < string > {
@@ -122,6 +139,25 @@ export class UmbRepositoryItemsManager<ItemType extends { unique: string }> exte
122
139
}
123
140
}
124
141
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
+
125
161
#sortByUniques( data : Array < ItemType > ) : Array < ItemType > {
126
162
const uniques = this . getUniques ( ) ;
127
163
return [ ...data ] . sort ( ( a , b ) => {
@@ -130,4 +166,25 @@ export class UmbRepositoryItemsManager<ItemType extends { unique: string }> exte
130
166
return aIndex - bIndex ;
131
167
} ) ;
132
168
}
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
+ }
133
190
}
0 commit comments