@@ -70,11 +70,8 @@ export class CreateResourceModalComponent implements OnInit {
7070 }
7171
7272 open ( resource ?: Resource ) {
73- if ( resource ) {
74- this . prepareForEdit ( resource ) ;
75- } else {
76- this . prepareForCreate ( ) ;
77- }
73+ this . originalResource . set ( resource ) ;
74+ this . form = this . fb . group ( this . createControls ( resource ) ) ;
7875 const dialog = this . dialog ( ) ;
7976 if ( dialog ) {
8077 dialog . open = true ;
@@ -86,7 +83,6 @@ export class CreateResourceModalComponent implements OnInit {
8683 if ( dialog ) {
8784 dialog . open = false ;
8885 this . form . reset ( ) ;
89- this . setEditFieldsDisabled ( false ) ;
9086 this . originalResource . set ( null ) ;
9187 }
9288 }
@@ -98,11 +94,8 @@ export class CreateResourceModalComponent implements OnInit {
9894 set ( result , key . replaceAll ( '_' , '.' ) , this . form . value [ key ] ) ;
9995 }
10096
101- const orig = this . originalResource ( ) ;
102- if ( orig ) {
103- if ( orig . metadata ) {
104- result [ 'metadata' ] = { ...orig . metadata , ...result [ 'metadata' ] } ;
105- }
97+ console . log ( 'result: ' , result ) ;
98+ if ( this . isEditMode ( ) ) {
10699 this . updateResource . emit ( result ) ;
107100 } else {
108101 this . resource . emit ( result ) ;
@@ -145,57 +138,25 @@ export class CreateResourceModalComponent implements OnInit {
145138 ) ;
146139 }
147140
148- private prepareForEdit ( resource : Resource ) {
149- this . originalResource . set ( resource ) ;
150- this . populateFormFromResource ( resource ) ;
151- this . setEditFieldsDisabled ( true ) ;
152- }
153-
154- private prepareForCreate ( ) {
155- this . originalResource . set ( null ) ;
156- this . form . reset ( ) ;
157- this . setEditFieldsDisabled ( false ) ;
158- }
159-
160- private populateFormFromResource ( resource : Resource ) {
161- const fields = this . fields ( ) ;
162- fields ?. forEach ( ( field ) => {
163- const controlName = this . sanitizePropertyName ( field . property ) ;
164- const path = typeof field . property === 'string' ? field . property : '' ;
165- const value = path ? getValueByPath ( resource , path ) : '' ;
166- if ( this . form . controls [ controlName ] ) {
167- this . form . controls [ controlName ] . setValue ( value ) ;
168- this . form . controls [ controlName ] . markAsPristine ( ) ;
169- this . form . controls [ controlName ] . markAsUntouched ( ) ;
170- }
171- } ) ;
172- }
173-
174- private createControls ( ) {
141+ private createControls ( resource ?: Resource ) {
175142 return this . fields ( ) . reduce (
176143 ( obj , fieldDefinition ) => {
177144 const validator = fieldDefinition . required ? Validators . required : null ;
178- obj [ this . sanitizePropertyName ( fieldDefinition . property ) ] =
179- new FormControl ( '' , validator ) ;
145+ const fieldName = this . sanitizePropertyName ( fieldDefinition . property ) ;
146+ const fieldValue =
147+ resource && typeof fieldDefinition . property === 'string'
148+ ? getValueByPath ( resource , fieldDefinition . property )
149+ : '' ;
150+ obj [ fieldName ] = new FormControl ( fieldValue , validator ) ;
151+ const isDisabled =
152+ ! ! resource && this . isCreateFieldOnly ( fieldDefinition ) ;
153+ if ( isDisabled ) {
154+ obj [ fieldName ] . disable ( { onlySelf : true } ) ;
155+ }
156+
180157 return obj ;
181158 } ,
182159 { } as Record < string , FormControl > ,
183160 ) ;
184161 }
185-
186- private setEditFieldsDisabled ( disabled : boolean ) {
187- const fields = this . fields ( ) || [ ] ;
188- fields . forEach ( ( f ) => {
189- const prop = typeof f . property === 'string' ? f . property : '' ;
190- if ( this . isCreateFieldOnly ( f ) ) {
191- const ctrlName = this . sanitizePropertyName ( prop ) ;
192- const ctrl = this . form . controls [ ctrlName ] ;
193- if ( ctrl ) {
194- disabled
195- ? ctrl . disable ( { emitEvent : false } )
196- : ctrl . enable ( { emitEvent : false } ) ;
197- }
198- }
199- } ) ;
200- }
201162}
0 commit comments