1
1
import type { UmbDictionaryDetailModel } from '../types.js' ;
2
- import { UmbDictionaryDetailRepository } from '../repository/index.js' ;
2
+ import { UMB_DICTIONARY_DETAIL_REPOSITORY_ALIAS , type UmbDictionaryDetailRepository } from '../repository/index.js' ;
3
+ import { UMB_DICTIONARY_ENTITY_TYPE } from '../entity.js' ;
3
4
import { UmbDictionaryWorkspaceEditorElement } from './dictionary-workspace-editor.element.js' ;
5
+ import { UMB_DICTIONARY_WORKSPACE_ALIAS } from './manifests.js' ;
4
6
import {
5
7
type UmbSubmittableWorkspaceContext ,
6
- UmbSubmittableWorkspaceContextBase ,
7
8
UmbWorkspaceIsNewRedirectController ,
8
9
type UmbRoutableWorkspaceContext ,
10
+ UmbEntityDetailWorkspaceContextBase ,
9
11
} from '@umbraco-cms/backoffice/workspace' ;
10
12
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api' ;
11
- import { UmbObjectState } from '@umbraco-cms/backoffice/observable-api' ;
12
- import { UMB_ACTION_EVENT_CONTEXT } from '@umbraco-cms/backoffice/action' ;
13
- import {
14
- UmbRequestReloadChildrenOfEntityEvent ,
15
- UmbRequestReloadStructureForEntityEvent ,
16
- } from '@umbraco-cms/backoffice/entity-action' ;
17
13
18
14
export class UmbDictionaryWorkspaceContext
19
- extends UmbSubmittableWorkspaceContextBase < UmbDictionaryDetailModel >
15
+ extends UmbEntityDetailWorkspaceContextBase < UmbDictionaryDetailModel , UmbDictionaryDetailRepository >
20
16
implements UmbSubmittableWorkspaceContext , UmbRoutableWorkspaceContext
21
17
{
22
- //
23
- public readonly detailRepository = new UmbDictionaryDetailRepository ( this ) ;
24
-
25
- #parent = new UmbObjectState < { entityType : string ; unique : string | null } | undefined > ( undefined ) ;
26
- readonly parentUnique = this . #parent. asObservablePart ( ( parent ) => ( parent ? parent . unique : undefined ) ) ;
27
- readonly parentEntityType = this . #parent. asObservablePart ( ( parent ) => ( parent ? parent . entityType : undefined ) ) ;
18
+ readonly data = this . _data . current ;
28
19
29
- #data = new UmbObjectState < UmbDictionaryDetailModel | undefined > ( undefined ) ;
30
- readonly data = this . #data . asObservable ( ) ;
20
+ readonly unique = this . _data . createObservablePartOfCurrent ( ( data ) => data ?. unique ) ;
21
+ readonly entityType = this . _data . createObservablePartOfCurrent ( ( data ) => data ?. entityType ) ;
31
22
32
- readonly unique = this . #data. asObservablePart ( ( data ) => data ?. unique ) ;
33
- readonly entityType = this . #data. asObservablePart ( ( data ) => data ?. entityType ) ;
34
-
35
- readonly name = this . #data. asObservablePart ( ( data ) => data ?. name ) ;
36
- readonly dictionary = this . #data. asObservablePart ( ( data ) => data ) ;
23
+ readonly name = this . _data . createObservablePartOfCurrent ( ( data ) => data ?. name ) ;
24
+ readonly dictionary = this . _data . createObservablePartOfCurrent ( ( data ) => data ) ;
37
25
38
26
constructor ( host : UmbControllerHost ) {
39
- super ( host , 'Umb.Workspace.Dictionary' ) ;
27
+ super ( host , {
28
+ workspaceAlias : UMB_DICTIONARY_WORKSPACE_ALIAS ,
29
+ entityType : UMB_DICTIONARY_ENTITY_TYPE ,
30
+ detailRepositoryAlias : UMB_DICTIONARY_DETAIL_REPOSITORY_ALIAS ,
31
+ } ) ;
40
32
41
33
this . routes . setRoutes ( [
42
34
{
@@ -45,7 +37,7 @@ export class UmbDictionaryWorkspaceContext
45
37
setup : async ( _component , info ) => {
46
38
const parentEntityType = info . match . params . entityType ;
47
39
const parentUnique = info . match . params . parentUnique === 'null' ? null : info . match . params . parentUnique ;
48
- this . create ( { entityType : parentEntityType , unique : parentUnique } ) ;
40
+ this . createScaffold ( { parent : { entityType : parentEntityType , unique : parentUnique } } ) ;
49
41
50
42
new UmbWorkspaceIsNewRedirectController (
51
43
this ,
@@ -65,34 +57,18 @@ export class UmbDictionaryWorkspaceContext
65
57
] ) ;
66
58
}
67
59
68
- protected override resetState ( ) : void {
69
- super . resetState ( ) ;
70
- this . #data. setValue ( undefined ) ;
71
- }
72
-
73
- getData ( ) {
74
- return this . #data. getValue ( ) ;
75
- }
76
-
77
- getUnique ( ) {
78
- return this . getData ( ) ?. unique ;
79
- }
80
-
81
- getEntityType ( ) {
82
- return 'dictionary' ;
83
- }
84
-
85
60
setName ( name : string ) {
86
- this . #data . update ( { name } ) ;
61
+ this . _data . updateCurrent ( { name } ) ;
87
62
}
88
63
89
64
setPropertyValue ( isoCode : string , translation : string ) {
90
- if ( ! this . #data. value ) return ;
65
+ const currentData = this . _data . getCurrent ( ) ;
66
+ if ( ! currentData ) return ;
91
67
92
68
// TODO: This can use some of our own methods, to make it simpler. see appendToFrozenArray()
93
69
// update if the code already exists
94
70
const updatedValue =
95
- this . #data . value . translations ?. map ( ( translationItem ) => {
71
+ currentData . translations ?. map ( ( translationItem ) => {
96
72
if ( translationItem . isoCode === isoCode ) {
97
73
return { ...translationItem , translation } ;
98
74
}
@@ -104,70 +80,7 @@ export class UmbDictionaryWorkspaceContext
104
80
updatedValue ?. push ( { isoCode, translation } ) ;
105
81
}
106
82
107
- this . #data. setValue ( { ...this . #data. value , translations : updatedValue } ) ;
108
- }
109
-
110
- async load ( unique : string ) {
111
- this . resetState ( ) ;
112
- const { data } = await this . detailRepository . requestByUnique ( unique ) ;
113
- if ( data ) {
114
- this . setIsNew ( false ) ;
115
- this . #data. setValue ( data ) ;
116
- }
117
- }
118
-
119
- async create ( parent : { entityType : string ; unique : string | null } ) {
120
- this . resetState ( ) ;
121
- this . #parent. setValue ( parent ) ;
122
- const { data } = await this . detailRepository . createScaffold ( ) ;
123
- if ( ! data ) return ;
124
- this . setIsNew ( true ) ;
125
- this . #data. setValue ( data ) ;
126
- }
127
-
128
- async submit ( ) {
129
- if ( ! this . #data. value ) {
130
- throw new Error ( 'No data to submit.' ) ;
131
- }
132
- if ( ! this . #data. value . unique ) {
133
- throw new Error ( 'No unique value to submit.' ) ;
134
- }
135
-
136
- if ( this . getIsNew ( ) ) {
137
- const parent = this . #parent. getValue ( ) ;
138
- if ( ! parent ) {
139
- throw new Error ( 'Parent is not set' ) ;
140
- }
141
- const { error } = await this . detailRepository . create ( this . #data. value , parent . unique ) ;
142
- if ( error ) {
143
- throw new Error ( error . message ) ;
144
- }
145
-
146
- // TODO: this might not be the right place to alert the tree, but it works for now
147
- const eventContext = await this . getContext ( UMB_ACTION_EVENT_CONTEXT ) ;
148
- const event = new UmbRequestReloadChildrenOfEntityEvent ( {
149
- entityType : parent . entityType ,
150
- unique : parent . unique ,
151
- } ) ;
152
- eventContext . dispatchEvent ( event ) ;
153
-
154
- this . setIsNew ( false ) ;
155
- } else {
156
- await this . detailRepository . save ( this . #data. value ) ;
157
-
158
- const actionEventContext = await this . getContext ( UMB_ACTION_EVENT_CONTEXT ) ;
159
- const event = new UmbRequestReloadStructureForEntityEvent ( {
160
- unique : this . getUnique ( ) ! ,
161
- entityType : this . getEntityType ( ) ,
162
- } ) ;
163
-
164
- actionEventContext . dispatchEvent ( event ) ;
165
- }
166
- }
167
-
168
- public override destroy ( ) : void {
169
- this . #data. destroy ( ) ;
170
- super . destroy ( ) ;
83
+ this . _data . setCurrent ( { ...currentData , translations : updatedValue } ) ;
171
84
}
172
85
}
173
86
0 commit comments