@@ -24,6 +24,11 @@ import { EDIT_API_CONFIGURATION } from './graphql/mutations';
2424import { GET_API_CONFIGURATION } from './graphql/queries' ;
2525import { SnackbarService } from '@oort-front/ui' ;
2626
27+ /**
28+ * Default value shown for private settings fields
29+ */
30+ const ENCRYPTED_VALUE = '●●●●●●●●●●●●●' ;
31+
2732/**
2833 * API configuration page component.
2934 */
@@ -36,16 +41,21 @@ export class ApiConfigurationComponent
3641 extends SafeUnsubscribeComponent
3742 implements OnInit
3843{
39- // === DATA ===
44+ /** Loading indicator */
4045 public loading = true ;
46+ /** Api configuration id */
4147 public id = '' ;
48+ /** Edited api configuration */
4249 public apiConfiguration ?: ApiConfiguration ;
43-
44- // === FORM ===
50+ /** Api form group */
4551 public apiForm : UntypedFormGroup = new UntypedFormGroup ( { } ) ;
52+ /** Reference to status enum */
4653 public status = status ;
54+ /** Available status choices */
4755 public statusChoices = Object . values ( status ) ;
56+ /** Reference to auth type enum */
4857 public authType = authType ;
58+ /** Available auth types */
4959 public authTypeChoices = Object . values ( authType ) ;
5060
5161 /** @returns API configuration name */
@@ -118,11 +128,12 @@ export class ApiConfigurationComponent
118128 ) ,
119129 graphQLEndpoint : this . apiConfiguration ?. graphQLEndpoint ,
120130 } ) ;
121- this . apiForm . get ( 'authType' ) ?. valueChanges . subscribe ( ( value ) => {
122- this . apiForm . controls . settings . clearValidators ( ) ;
123- this . apiForm . controls . settings = this . buildSettingsForm ( value ) ;
124- this . apiForm . controls . settings . updateValueAndValidity ( ) ;
125- } ) ;
131+ this . apiForm
132+ . get ( 'authType' )
133+ ?. valueChanges . pipe ( takeUntil ( this . destroy$ ) )
134+ . subscribe ( ( value ) => {
135+ this . resetFormSettings ( value ) ;
136+ } ) ;
126137 this . loading = loading ;
127138 } else {
128139 this . snackBar . openSnackBar (
@@ -148,6 +159,17 @@ export class ApiConfigurationComponent
148159 }
149160 }
150161
162+ /**
163+ * Reset settings configuration form with the given API configuration
164+ *
165+ * @param authType current auth type of the API configuration
166+ */
167+ private resetFormSettings ( authType : string ) {
168+ this . apiForm . removeControl ( 'settings' , { emitEvent : false } ) ;
169+ this . apiForm . addControl ( 'settings' , this . buildSettingsForm ( authType ) ) ;
170+ this . apiForm . updateValueAndValidity ( ) ;
171+ }
172+
151173 /**
152174 * Create the settings form depending on the authType
153175 *
@@ -160,28 +182,28 @@ export class ApiConfigurationComponent
160182 authTargetUrl : [
161183 this . apiConfiguration ?. settings &&
162184 this . apiConfiguration ?. settings . authTargetUrl
163- ? '●●●●●●●●●●●●●'
185+ ? ENCRYPTED_VALUE
164186 : '' ,
165187 Validators . required ,
166188 ] ,
167189 apiClientID : [
168190 this . apiConfiguration ?. settings &&
169191 this . apiConfiguration ?. settings . apiClientID
170- ? '●●●●●●●●●●●●●'
192+ ? ENCRYPTED_VALUE
171193 : '' ,
172194 Validators . minLength ( 3 ) ,
173195 ] ,
174196 safeSecret : [
175197 this . apiConfiguration ?. settings &&
176198 this . apiConfiguration ?. settings . safeSecret
177- ? '●●●●●●●●●●●●●'
199+ ? ENCRYPTED_VALUE
178200 : '' ,
179201 Validators . minLength ( 3 ) ,
180202 ] ,
181203 scope : [
182204 this . apiConfiguration ?. settings &&
183205 this . apiConfiguration ?. settings . scope
184- ? '●●●●●●●●●●●●●'
206+ ? ENCRYPTED_VALUE
185207 : '' ,
186208 null ,
187209 ] ,
@@ -191,7 +213,7 @@ export class ApiConfigurationComponent
191213 token : [
192214 this . apiConfiguration ?. settings &&
193215 this . apiConfiguration ?. settings . token
194- ? '●●●●●●●●●●●●●'
216+ ? ENCRYPTED_VALUE
195217 : '' ,
196218 Validators . required ,
197219 ] ,
@@ -259,8 +281,30 @@ export class ApiConfigurationComponent
259281 this . apiForm . value . pingUrl !== this . apiConfiguration ?. pingUrl && {
260282 pingUrl : this . apiForm . value . pingUrl ,
261283 } ,
284+ // If settings is touched we will go through each settings param to save only the ones that are not the encrypted display value and that exist
262285 this . apiForm . controls . settings . touched && {
263- settings : this . apiForm . controls . settings . value ,
286+ settings : {
287+ ...( this . apiForm . value . authType === authType . serviceToService &&
288+ this . apiForm . value . settings . authTargetUrl !== ENCRYPTED_VALUE && {
289+ authTargetUrl : this . apiForm . value . settings . authTargetUrl ,
290+ } ) ,
291+ ...( this . apiForm . value . authType === authType . serviceToService &&
292+ this . apiForm . value . settings . apiClientID !== ENCRYPTED_VALUE && {
293+ apiClientID : this . apiForm . value . settings . apiClientID ,
294+ } ) ,
295+ ...( this . apiForm . value . authType === authType . serviceToService &&
296+ this . apiForm . value . settings . safeSecret !== ENCRYPTED_VALUE && {
297+ safeSecret : this . apiForm . value . settings . safeSecret ,
298+ } ) ,
299+ ...( this . apiForm . value . authType === authType . serviceToService &&
300+ this . apiForm . value . settings . scope !== ENCRYPTED_VALUE && {
301+ scope : this . apiForm . value . settings . scope ,
302+ } ) ,
303+ ...( this . apiForm . value . authType === authType . userToService &&
304+ this . apiForm . value . settings . token !== ENCRYPTED_VALUE && {
305+ token : this . apiForm . value . settings . token ,
306+ } ) ,
307+ } ,
264308 }
265309 ) ;
266310 this . apollo
@@ -280,10 +324,7 @@ export class ApiConfigurationComponent
280324 this . loading = false ;
281325 } else {
282326 this . apiConfiguration = data ?. editApiConfiguration ;
283- this . apiForm . controls . settings = this . buildSettingsForm (
284- this . apiForm . value . authType
285- ) ;
286- this . apiForm . markAsPristine ( ) ;
327+ this . resetFormSettings ( this . apiConfiguration ?. authType as string ) ;
287328 this . loading = loading || false ;
288329 }
289330 } ) ;
@@ -326,4 +367,16 @@ export class ApiConfigurationComponent
326367 }
327368 ) ;
328369 }
370+
371+ /**
372+ * Clear the value of the given control, if not updated by the user
373+ *
374+ * @param key control key from settings control to clear
375+ */
376+ clearControl ( key : string ) {
377+ const control = this . apiForm . get ( key ) ;
378+ if ( control && control . pristine ) {
379+ control . setValue ( '' ) ;
380+ }
381+ }
329382}
0 commit comments