@@ -6,17 +6,26 @@ import type {
6
6
UmbMemberVariantOptionModel ,
7
7
} from '../../types.js' ;
8
8
import { UmbMemberPropertyDatasetContext } from '../../property-dataset-context/member-property-dataset-context.js' ;
9
+ import { UMB_MEMBER_ENTITY_TYPE , UMB_MEMBER_ROOT_ENTITY_TYPE } from '../../entity.js' ;
10
+ import { sortVariants } from '../../utils.js' ;
11
+ import { UMB_MEMBER_MANAGEMENT_SECTION_PATH } from '../../../section/index.js' ;
9
12
import { UMB_MEMBER_WORKSPACE_ALIAS } from './manifests.js' ;
10
13
import { UmbMemberWorkspaceEditorElement } from './member-workspace-editor.element.js' ;
11
14
import { UMB_MEMBER_DETAIL_MODEL_VARIANT_SCAFFOLD } from './constants.js' ;
12
15
import { UmbMemberTypeDetailRepository , type UmbMemberTypeDetailModel } from '@umbraco-cms/backoffice/member-type' ;
13
16
import {
14
17
UmbSubmittableWorkspaceContextBase ,
15
18
UmbWorkspaceIsNewRedirectController ,
19
+ UmbWorkspaceIsNewRedirectControllerAlias ,
16
20
UmbWorkspaceSplitViewManager ,
17
21
} from '@umbraco-cms/backoffice/workspace' ;
18
22
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api' ;
19
- import { UmbArrayState , appendToFrozenArray , mergeObservables } from '@umbraco-cms/backoffice/observable-api' ;
23
+ import {
24
+ UmbArrayState ,
25
+ UmbObjectState ,
26
+ appendToFrozenArray ,
27
+ mergeObservables ,
28
+ } from '@umbraco-cms/backoffice/observable-api' ;
20
29
import { UmbContentTypeStructureManager } from '@umbraco-cms/backoffice/content-type' ;
21
30
import { UMB_INVARIANT_CULTURE , UmbVariantId } from '@umbraco-cms/backoffice/variant' ;
22
31
import type { UmbLanguageDetailModel } from '@umbraco-cms/backoffice/language' ;
@@ -25,6 +34,13 @@ import type { UmbDataSourceResponse } from '@umbraco-cms/backoffice/repository';
25
34
import { UmbContentWorkspaceDataManager , type UmbContentWorkspaceContext } from '@umbraco-cms/backoffice/content' ;
26
35
import { UmbReadOnlyVariantStateManager } from '@umbraco-cms/backoffice/utils' ;
27
36
import { UmbDataTypeItemRepositoryManager } from '@umbraco-cms/backoffice/data-type' ;
37
+ import { map } from '@umbraco-cms/backoffice/external/rxjs' ;
38
+ import { UmbEntityContext , type UmbEntityModel } from '@umbraco-cms/backoffice/entity' ;
39
+ import {
40
+ UmbRequestReloadChildrenOfEntityEvent ,
41
+ UmbRequestReloadStructureForEntityEvent ,
42
+ } from '@umbraco-cms/backoffice/entity-action' ;
43
+ import { UMB_ACTION_EVENT_CONTEXT } from '@umbraco-cms/backoffice/action' ;
28
44
29
45
type EntityModel = UmbMemberDetailModel ;
30
46
export class UmbMemberWorkspaceContext
@@ -35,6 +51,10 @@ export class UmbMemberWorkspaceContext
35
51
36
52
public readonly repository = new UmbMemberDetailRepository ( this ) ;
37
53
54
+ #parent = new UmbObjectState < UmbEntityModel | undefined > ( { entityType : UMB_MEMBER_ROOT_ENTITY_TYPE , unique : null } ) ;
55
+ readonly parentUnique = this . #parent. asObservablePart ( ( parent ) => ( parent ? parent . unique : undefined ) ) ;
56
+ readonly parentEntityType = this . #parent. asObservablePart ( ( parent ) => ( parent ? parent . entityType : undefined ) ) ;
57
+
38
58
readonly #data = new UmbContentWorkspaceDataManager < EntityModel > ( this , UMB_MEMBER_DETAIL_MODEL_VARIANT_SCAFFOLD ) ;
39
59
#getDataPromise?: Promise < UmbDataSourceResponse < UmbMemberDetailModel > > ;
40
60
@@ -43,7 +63,7 @@ export class UmbMemberWorkspaceContext
43
63
#languages = new UmbArrayState < UmbLanguageDetailModel > ( [ ] , ( x ) => x . unique ) ;
44
64
public readonly languages = this . #languages. asObservable ( ) ;
45
65
46
- readOnlyState = new UmbReadOnlyVariantStateManager ( this ) ;
66
+ public readonly readOnlyState = new UmbReadOnlyVariantStateManager ( this ) ;
47
67
48
68
public isLoaded ( ) {
49
69
return this . #getDataPromise;
@@ -54,7 +74,8 @@ export class UmbMemberWorkspaceContext
54
74
readonly createDate = this . #data. createObservablePartOfCurrent ( ( data ) => data ?. variants [ 0 ] . createDate ) ;
55
75
readonly updateDate = this . #data. createObservablePartOfCurrent ( ( data ) => data ?. variants [ 0 ] . updateDate ) ;
56
76
readonly contentTypeUnique = this . #data. createObservablePartOfCurrent ( ( data ) => data ?. memberType . unique ) ;
57
- readonly kind = this . #data. createObservablePartOfCurrent ( ( data ) => data ?. kind ) ;
77
+
78
+ readonly variants = this . #data. createObservablePartOfCurrent ( ( data ) => data ?. variants ?? [ ] ) ;
58
79
59
80
readonly structure = new UmbContentTypeStructureManager ( this , new UmbMemberTypeDetailRepository ( this ) ) ;
60
81
readonly variesByCulture = this . structure . ownerContentTypeObservablePart ( ( x ) => x ?. variesByCulture ) ;
@@ -66,7 +87,7 @@ export class UmbMemberWorkspaceContext
66
87
#variesByCulture?: boolean ;
67
88
#variesBySegment?: boolean ;
68
89
69
- readonly variants = this . #data. createObservablePartOfCurrent ( ( data ) => data ?. variants ?? [ ] ) ;
90
+ readonly kind = this . #data. createObservablePartOfCurrent ( ( data ) => data ?. kind ) ;
70
91
71
92
readonly #dataTypeItemManager = new UmbDataTypeItemRepositoryManager ( this ) ;
72
93
#dataTypeSchemaAliasMap = new Map < string , string > ( ) ;
@@ -102,35 +123,51 @@ export class UmbMemberWorkspaceContext
102
123
}
103
124
return [ ] as Array < UmbMemberVariantOptionModel > ;
104
125
} ,
105
- ) ;
126
+ ) . pipe ( map ( ( results ) => results . sort ( sortVariants ) ) ) ;
127
+
128
+ // TODO: this should be set up for all entity workspace contexts in a base class
129
+ #entityContext = new UmbEntityContext ( this ) ;
106
130
107
131
constructor ( host : UmbControllerHost ) {
108
132
super ( host , UMB_MEMBER_WORKSPACE_ALIAS ) ;
109
133
110
- this . observe ( this . contentTypeUnique , ( unique ) => this . structure . loadType ( unique ) ) ;
111
-
112
- this . observe ( this . variesByCulture , ( varies ) => {
113
- this . #data. setVariesByCulture ( varies ) ;
114
- this . #variesByCulture = varies ;
115
- } ) ;
116
- this . observe ( this . variesBySegment , ( varies ) => {
117
- this . #data. setVariesBySegment ( varies ) ;
118
- this . #variesBySegment = varies ;
119
- } ) ;
120
- this . observe ( this . varies , ( varies ) => ( this . #varies = varies ) ) ;
121
-
122
- this . observe ( this . structure . contentTypeDataTypeUniques , ( dataTypeUniques : Array < string > ) => {
123
- this . #dataTypeItemManager. setUniques ( dataTypeUniques ) ;
124
- } ) ;
125
-
126
- this . observe ( this . #dataTypeItemManager. items , ( dataTypes ) => {
127
- // Make a map of the data type unique and editorAlias:
128
- this . #dataTypeSchemaAliasMap = new Map (
129
- dataTypes . map ( ( dataType ) => {
130
- return [ dataType . unique , dataType . propertyEditorSchemaAlias ] ;
131
- } ) ,
132
- ) ;
133
- } ) ;
134
+ this . observe ( this . contentTypeUnique , ( unique ) => this . structure . loadType ( unique ) , null ) ;
135
+ this . observe ( this . varies , ( varies ) => ( this . #varies = varies ) , null ) ;
136
+ this . observe (
137
+ this . variesByCulture ,
138
+ ( varies ) => {
139
+ this . #data. setVariesByCulture ( varies ) ;
140
+ this . #variesByCulture = varies ;
141
+ } ,
142
+ null ,
143
+ ) ;
144
+ this . observe (
145
+ this . variesBySegment ,
146
+ ( varies ) => {
147
+ this . #data. setVariesBySegment ( varies ) ;
148
+ this . #variesBySegment = varies ;
149
+ } ,
150
+ null ,
151
+ ) ;
152
+ this . observe (
153
+ this . structure . contentTypeDataTypeUniques ,
154
+ ( dataTypeUniques : Array < string > ) => {
155
+ this . #dataTypeItemManager. setUniques ( dataTypeUniques ) ;
156
+ } ,
157
+ null ,
158
+ ) ;
159
+ this . observe (
160
+ this . #dataTypeItemManager. items ,
161
+ ( dataTypes ) => {
162
+ // Make a map of the data type unique and editorAlias:
163
+ this . #dataTypeSchemaAliasMap = new Map (
164
+ dataTypes . map ( ( dataType ) => {
165
+ return [ dataType . unique , dataType . propertyEditorSchemaAlias ] ;
166
+ } ) ,
167
+ ) ;
168
+ } ,
169
+ null ,
170
+ ) ;
134
171
this . loadLanguages ( ) ;
135
172
136
173
this . routes . setRoutes ( [
@@ -152,6 +189,7 @@ export class UmbMemberWorkspaceContext
152
189
path : 'edit/:unique' ,
153
190
component : ( ) => new UmbMemberWorkspaceEditorElement ( ) ,
154
191
setup : ( _component , info ) => {
192
+ this . removeUmbControllerByAlias ( UmbWorkspaceIsNewRedirectControllerAlias ) ;
155
193
const unique = info . match . params . unique ;
156
194
this . load ( unique ) ;
157
195
} ,
@@ -161,8 +199,7 @@ export class UmbMemberWorkspaceContext
161
199
162
200
override resetState ( ) {
163
201
super . resetState ( ) ;
164
- this . #data. setPersisted ( undefined ) ;
165
- this . #data. setCurrent ( undefined ) ;
202
+ this . #data. clear ( ) ;
166
203
}
167
204
168
205
async loadLanguages ( ) {
@@ -178,6 +215,8 @@ export class UmbMemberWorkspaceContext
178
215
const { data, asObservable } = ( await this . #getDataPromise) as GetDataType ;
179
216
180
217
if ( data ) {
218
+ this . #entityContext. setEntityType ( UMB_MEMBER_ENTITY_TYPE ) ;
219
+ this . #entityContext. setUnique ( unique ) ;
181
220
this . setIsNew ( false ) ;
182
221
this . #data. setPersisted ( data ) ;
183
222
this . #data. setCurrent ( data ) ;
@@ -189,7 +228,7 @@ export class UmbMemberWorkspaceContext
189
228
#onMemberStoreChange( member : EntityModel | undefined ) {
190
229
if ( ! member ) {
191
230
//TODO: This solution is alright for now. But reconsider when we introduce signal-r
192
- history . pushState ( null , '' , 'section/member-management' ) ;
231
+ history . pushState ( null , '' , UMB_MEMBER_MANAGEMENT_SECTION_PATH ) ;
193
232
}
194
233
}
195
234
@@ -203,6 +242,8 @@ export class UmbMemberWorkspaceContext
203
242
const { data } = await this . #getDataPromise;
204
243
if ( ! data ) return undefined ;
205
244
245
+ this . #entityContext. setEntityType ( UMB_MEMBER_ENTITY_TYPE ) ;
246
+ this . #entityContext. setUnique ( data . unique ) ;
206
247
this . setIsNew ( true ) ;
207
248
this . #data. setPersisted ( undefined ) ;
208
249
this . #data. setCurrent ( data ) ;
@@ -214,11 +255,11 @@ export class UmbMemberWorkspaceContext
214
255
}
215
256
216
257
getUnique ( ) {
217
- return this . getData ( ) ?. unique || '' ;
258
+ return this . getData ( ) ?. unique ;
218
259
}
219
260
220
261
getEntityType ( ) {
221
- return 'member' ;
262
+ return UMB_MEMBER_ENTITY_TYPE ;
222
263
}
223
264
224
265
getContentTypeId ( ) {
@@ -336,35 +377,56 @@ export class UmbMemberWorkspaceContext
336
377
this . #data. finishPropertyValueChange ( ) ;
337
378
} ;
338
379
339
- async submit ( ) {
380
+ async #handleSave ( ) {
340
381
const current = this . #data. getCurrent ( ) ;
341
382
if ( ! current ) throw new Error ( 'Data is missing' ) ;
342
383
if ( ! current . unique ) throw new Error ( 'Unique is missing' ) ;
343
384
344
- let newData = undefined ;
345
-
346
385
if ( this . getIsNew ( ) ) {
347
- const { data } = await this . repository . create ( current ) ;
348
- if ( ! data ) {
386
+ // Create:
387
+ const parent = this . #parent. getValue ( ) ;
388
+ if ( ! parent ) throw new Error ( 'Parent is not set' ) ;
389
+
390
+ const { data, error } = await this . repository . create ( current ) ;
391
+ if ( ! data || error ) {
349
392
throw new Error ( 'Could not create member.' ) ;
350
393
}
351
- newData = data ;
394
+
352
395
this . setIsNew ( false ) ;
396
+ this . #data. setPersisted ( data ) ;
397
+ // TODO: Missing variant data filtering.
398
+ this . #data. setCurrent ( data ) ;
399
+
400
+ const eventContext = await this . getContext ( UMB_ACTION_EVENT_CONTEXT ) ;
401
+ const event = new UmbRequestReloadChildrenOfEntityEvent ( {
402
+ entityType : parent . entityType ,
403
+ unique : parent . unique ,
404
+ } ) ;
405
+ eventContext . dispatchEvent ( event ) ;
353
406
} else {
354
- const { data } = await this . repository . save ( current ) ;
355
- if ( ! data ) {
356
- throw new Error ( 'Could not create member.' ) ;
407
+ // Save:
408
+ const { data, error } = await this . repository . save ( current ) ;
409
+ if ( ! data || error ) {
410
+ throw new Error ( 'Could not update member.' ) ;
357
411
}
358
- newData = data ;
359
- }
412
+ this . #data. setPersisted ( data ) ;
413
+ // TODO: Missing variant data filtering.
414
+ this . #data. setCurrent ( data ) ;
415
+
416
+ const eventContext = await this . getContext ( UMB_ACTION_EVENT_CONTEXT ) ;
417
+ const event = new UmbRequestReloadStructureForEntityEvent ( {
418
+ entityType : this . getEntityType ( ) ,
419
+ unique : this . getUnique ( ) ! ,
420
+ } ) ;
360
421
361
- if ( newData ) {
362
- this . #data. setPersisted ( newData ) ;
363
- // TODO: Only update the variants that was chosen to be saved:
364
- //this.#data.setCurrentData(newData);
422
+ eventContext . dispatchEvent ( event ) ;
365
423
}
366
424
}
367
425
426
+ async submit ( ) {
427
+ return this . #handleSave( ) ;
428
+ }
429
+
368
430
async delete ( ) {
369
431
const id = this . getUnique ( ) ;
370
432
if ( id ) {
0 commit comments