Skip to content

Commit ee341e9

Browse files
fix: unfriendly api configuration edition (#1937)
--------- Co-authored-by: Antoine Hurard <[email protected]>
1 parent 6bdbfe4 commit ee341e9

File tree

2 files changed

+76
-23
lines changed

2 files changed

+76
-23
lines changed

apps/back-office/src/app/dashboard/pages/api-configuration/api-configuration.component.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ <h2 class="mt-8">
140140
[placeholder]="
141141
'pages.apiConfiguration.placeholder.tokenUrl' | translate
142142
"
143-
onfocus="this.value=''"
143+
(focus)="clearControl('settings.authTargetUrl')"
144144
/>
145145
</div>
146146
<!-- Client id -->
@@ -154,7 +154,7 @@ <h2 class="mt-8">
154154
[placeholder]="
155155
'pages.apiConfiguration.placeholder.clientId' | translate
156156
"
157-
onfocus="this.value=''"
157+
(focus)="clearControl('settings.apiClientID')"
158158
/>
159159
</div>
160160
<!-- Client secret -->
@@ -168,7 +168,7 @@ <h2 class="mt-8">
168168
[placeholder]="
169169
'pages.apiConfiguration.placeholder.secret' | translate
170170
"
171-
onfocus="this.value=''"
171+
(focus)="clearControl('settings.safeSecret')"
172172
/>
173173
</div>
174174
<!-- Token scope -->
@@ -182,7 +182,7 @@ <h2 class="mt-8">
182182
[placeholder]="
183183
'pages.apiConfiguration.placeholder.scope' | translate
184184
"
185-
onfocus="this.value=''"
185+
(focus)="clearControl('settings.scope')"
186186
/>
187187
</div>
188188
</div>
@@ -201,7 +201,7 @@ <h2 class="mt-8">
201201
[placeholder]="
202202
'pages.apiConfiguration.placeholder.token' | translate
203203
"
204-
onfocus="this.value=''"
204+
(focus)="clearControl('settings.token')"
205205
/>
206206
</div>
207207
</div>

apps/back-office/src/app/dashboard/pages/api-configuration/api-configuration.component.ts

Lines changed: 71 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ import { EDIT_API_CONFIGURATION } from './graphql/mutations';
2424
import { GET_API_CONFIGURATION } from './graphql/queries';
2525
import { 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

Comments
 (0)