Skip to content

Commit 6f6c05a

Browse files
committed
refactor: get rid of UntypedFormBuilder
* In admin-gui, user-profile and libs we can use typed FormBuilder instead of untyped since the FormBuilder can create typed FormGroup according to the default values of defined form group items. * Some other refactoring was made in changed files (use PerunTranslateService, use non-deprecated type of subscribe).
1 parent 840577b commit 6f6c05a

File tree

8 files changed

+251
-314
lines changed

8 files changed

+251
-314
lines changed

apps/admin-gui/src/app/shared/components/create-service-member-dialog/create-service-member-dialog.component.ts

Lines changed: 88 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,7 @@ import {
2626
StoreService,
2727
} from '@perun-web-apps/perun/services';
2828
import { TranslateService } from '@ngx-translate/core';
29-
import {
30-
UntypedFormBuilder,
31-
UntypedFormControl,
32-
UntypedFormGroup,
33-
ValidatorFn,
34-
Validators,
35-
} from '@angular/forms';
29+
import { FormBuilder, FormControl, ValidatorFn, Validators } from '@angular/forms';
3630
import { SelectionModel } from '@angular/cdk/collections';
3731
import { TABLE_VO_MEMBERS } from '@perun-web-apps/config/table-config';
3832
import { CustomValidators } from '@perun-web-apps/perun/utils';
@@ -59,15 +53,45 @@ export interface CreateServiceMemberDialogResult {
5953
})
6054
export class CreateServiceMemberDialogComponent implements OnInit, AfterViewInit {
6155
@ViewChild('stepper') stepper: MatStepper;
62-
firstFormGroup: UntypedFormGroup;
63-
secondFormGroup: UntypedFormGroup;
56+
firstFormGroup = this._formBuilder.group({
57+
nameCtrl: ['', Validators.required],
58+
emailCtrl: [
59+
'',
60+
[Validators.required, Validators.pattern('\\w+([.-]?\\w+)*@\\w+([.-]?\\w+)*(.\\w{2,3})+')],
61+
],
62+
subjectCtrl: [''],
63+
issuerCtrl: [''],
64+
});
65+
secondFormGroup = this._formBuilder.group(
66+
{
67+
namespaceCtrl: ['Not selected'],
68+
loginCtrl: [
69+
'',
70+
[
71+
Validators.pattern('^[a-z][a-z0-9_-]+$'),
72+
Validators.maxLength(15),
73+
Validators.minLength(2),
74+
],
75+
],
76+
passwordCtrl: [
77+
'',
78+
Validators.required,
79+
[loginAsyncValidator(null, this.usersManagerService, this.apiRequestConfiguration)],
80+
],
81+
passwordAgainCtrl: [''],
82+
generatePasswordCtrl: [true],
83+
},
84+
{
85+
validators: CustomValidators.passwordMatchValidator as ValidatorFn,
86+
}
87+
);
6488

6589
parsedRules: Map<string, { login: string }> = new Map<string, { login: string }>();
6690

6791
loading: boolean;
6892
theme: string;
6993
firstSearchDone = false;
70-
searchCtrl = new UntypedFormControl('');
94+
searchCtrl = new FormControl('');
7195
members: RichMember[] = [];
7296
selection = new SelectionModel<RichMember>(true, []);
7397
tableId = TABLE_VO_MEMBERS;
@@ -89,7 +113,7 @@ export class CreateServiceMemberDialogComponent implements OnInit, AfterViewInit
89113
private translate: TranslateService,
90114
private store: StoreService,
91115
private apiRequestConfiguration: ApiRequestConfigurationService,
92-
private _formBuilder: UntypedFormBuilder,
116+
private _formBuilder: FormBuilder,
93117
private cd: ChangeDetectorRef,
94118
private authResolver: GuiAuthResolver,
95119
private findSponsors: FindSponsorsService
@@ -108,39 +132,6 @@ export class CreateServiceMemberDialogComponent implements OnInit, AfterViewInit
108132

109133
ngOnInit(): void {
110134
this.theme = this.data.theme;
111-
this.firstFormGroup = this._formBuilder.group({
112-
nameCtrl: ['', Validators.required],
113-
emailCtrl: [
114-
'',
115-
[Validators.required, Validators.pattern('\\w+([.-]?\\w+)*@\\w+([.-]?\\w+)*(.\\w{2,3})+')],
116-
],
117-
subjectCtrl: [null],
118-
issuerCtrl: [null],
119-
});
120-
this.secondFormGroup = this._formBuilder.group(
121-
{
122-
namespaceCtrl: ['Not selected'],
123-
loginCtrl: [
124-
'',
125-
[
126-
Validators.pattern('^[a-z][a-z0-9_-]+$'),
127-
Validators.maxLength(15),
128-
Validators.minLength(2),
129-
],
130-
],
131-
passwordCtrl: [
132-
'',
133-
Validators.required,
134-
[loginAsyncValidator(null, this.usersManagerService, this.apiRequestConfiguration)],
135-
],
136-
passwordAgainCtrl: [''],
137-
generatePasswordCtrl: [true],
138-
},
139-
{
140-
validators: CustomValidators.passwordMatchValidator as ValidatorFn,
141-
}
142-
);
143-
144135
const user = this.store.getPerunPrincipal().user;
145136
this.membersManagerService.getMembersByUser(user.id).subscribe((members) => {
146137
let tempMember: RichMember = {} as RichMember;
@@ -169,42 +160,37 @@ export class CreateServiceMemberDialogComponent implements OnInit, AfterViewInit
169160
onCreate(sponsor: boolean): void {
170161
this.processing = true;
171162
this.candidate['firstName'] = '';
172-
this.candidate['lastName'] = this.firstFormGroup.get('nameCtrl').value as string;
163+
this.candidate['lastName'] = this.firstFormGroup.value.nameCtrl;
173164
this.candidate['attributes'] = {};
174165
this.candidate['attributes']['urn:perun:member:attribute-def:def:mail'] =
175-
this.firstFormGroup.get('emailCtrl').value as string;
176-
const subject = this.firstFormGroup.get('subjectCtrl');
177-
if (subject?.value as string) {
166+
this.firstFormGroup.value.emailCtrl;
167+
const subject = this.firstFormGroup.value.subjectCtrl;
168+
if (subject) {
178169
this.candidate['userExtSource'] = {} as UserExtSource;
179-
this.candidate['userExtSource']['login'] = subject.value as string;
170+
this.candidate['userExtSource']['login'] = subject;
180171
this.candidate['userExtSource']['loa'] = 0;
181172
this.candidate['userExtSource']['extSource'] = {} as ExtSource;
182-
this.candidate['userExtSource']['extSource']['name'] = this.firstFormGroup.get('issuerCtrl')
183-
.value as string;
173+
this.candidate['userExtSource']['extSource']['name'] = this.firstFormGroup.value.issuerCtrl;
184174
this.candidate['userExtSource']['extSource']['type'] =
185175
'cz.metacentrum.perun.core.impl.ExtSourceX509';
186176
}
187177

188-
const namespace = (this.secondFormGroup.get('namespaceCtrl').value as string).toLowerCase();
178+
const namespace = this.secondFormGroup.value.namespaceCtrl.toLowerCase();
189179
const rules = this.parsedRules.get(namespace);
190180
const namespaceUrn = `urn:perun:user:attribute-def:def:login-namespace:${namespace}`;
191-
if (
192-
(this.secondFormGroup.get('namespaceCtrl').value as string) !== 'Not selected' &&
193-
rules.login === 'disabled'
194-
) {
181+
if (this.secondFormGroup.value.namespaceCtrl !== 'Not selected' && rules.login === 'disabled') {
195182
this.usersManagerService
196-
.generateAccountForName(namespace, this.firstFormGroup.get('nameCtrl').value as string)
197-
.subscribe(
198-
(params) => {
183+
.generateAccountForName(namespace, this.firstFormGroup.value.nameCtrl)
184+
.subscribe({
185+
next: (params) => {
199186
this.candidate['attributes'][namespaceUrn] = params[namespaceUrn];
200187
this.createSpecificMember(sponsor);
201188
},
202-
() => (this.processing = false)
203-
);
189+
error: () => (this.processing = false),
190+
});
204191
} else {
205-
if (this.secondFormGroup.get('namespaceCtrl').value !== 'Not selected') {
206-
this.candidate['attributes'][namespaceUrn] = this.secondFormGroup.get('loginCtrl')
207-
.value as string;
192+
if (this.secondFormGroup.value.namespaceCtrl !== 'Not selected') {
193+
this.candidate['attributes'][namespaceUrn] = this.secondFormGroup.value.loginCtrl;
208194
}
209195
this.createSpecificMember(sponsor);
210196
}
@@ -218,17 +204,13 @@ export class CreateServiceMemberDialogComponent implements OnInit, AfterViewInit
218204
specificUserOwners: this.assignedMembers.map((m) => m.user),
219205
candidate: this.candidate,
220206
})
221-
.subscribe(
222-
(member) => {
223-
this.membersManagerService.validateMemberAsync(member.id).subscribe(
224-
(mem) => {
207+
.subscribe({
208+
next: (member) => {
209+
this.membersManagerService.validateMemberAsync(member.id).subscribe({
210+
next: (mem) => {
225211
this.notificator.showSuccess(this.successMessageMember);
226-
if (this.secondFormGroup.get('namespaceCtrl').value !== 'Not selected') {
227-
this.setPassword(
228-
mem,
229-
this.secondFormGroup.get('generatePasswordCtrl').value as boolean,
230-
sponsor
231-
);
212+
if (this.secondFormGroup.value.namespaceCtrl !== 'Not selected') {
213+
this.setPassword(mem, this.secondFormGroup.value.generatePasswordCtrl, sponsor);
232214
} else {
233215
this.dialogRef.close({
234216
result: true,
@@ -239,48 +221,46 @@ export class CreateServiceMemberDialogComponent implements OnInit, AfterViewInit
239221
});
240222
}
241223
},
242-
() => (this.processing = false)
243-
);
224+
error: () => (this.processing = false),
225+
});
244226
},
245-
() => (this.processing = false)
246-
);
227+
error: () => (this.processing = false),
228+
});
247229
}
248230

249231
setPassword(member: Member, generateRandom: boolean, sponsor: boolean): void {
250-
const namespace: string = (
251-
this.secondFormGroup.get('namespaceCtrl').value as string
252-
).toLowerCase();
253-
const password: string = this.secondFormGroup.get('passwordCtrl').value as string;
232+
const namespace: string = this.secondFormGroup.value.namespaceCtrl.toLowerCase();
233+
const password: string = this.secondFormGroup.value.passwordCtrl;
254234
if (generateRandom) {
255235
if (this.parsedRules.get(namespace).login === 'disabled') {
256236
this.validateMember(member.id, sponsor);
257237
return; // password already set when account was generated
258238
}
259-
this.usersManagerService.reserveRandomPassword(member.userId, namespace).subscribe(
260-
() => {
261-
this.usersManagerService.validatePasswordForUser(member.userId, namespace).subscribe(
262-
() => {
239+
this.usersManagerService.reserveRandomPassword(member.userId, namespace).subscribe({
240+
next: () => {
241+
this.usersManagerService.validatePasswordForUser(member.userId, namespace).subscribe({
242+
next: () => {
263243
this.validateMember(member.id, sponsor, false);
264244
},
265-
() => {
245+
error: () => {
266246
this.processing = false;
267-
}
268-
);
247+
},
248+
});
269249
},
270-
() => {
250+
error: () => {
271251
this.processing = false;
272-
}
273-
);
252+
},
253+
});
274254
} else {
275255
this.usersManagerService
276256
.reservePasswordForUser({ user: member.userId, namespace: namespace, password: password })
277-
.subscribe(
278-
() => {
279-
this.usersManagerService.validatePasswordForUser(member.userId, namespace).subscribe(
280-
() => {
257+
.subscribe({
258+
next: () => {
259+
this.usersManagerService.validatePasswordForUser(member.userId, namespace).subscribe({
260+
next: () => {
281261
this.validateMember(member.id, sponsor);
282262
},
283-
() => {
263+
error: () => {
284264
this.processing = false;
285265
this.dialogRef.close({
286266
result: true,
@@ -289,10 +269,10 @@ export class CreateServiceMemberDialogComponent implements OnInit, AfterViewInit
289269
findSponsorsAuth: this.findSponsorsAuth,
290270
serviceMemberId: member.id,
291271
});
292-
}
293-
);
272+
},
273+
});
294274
},
295-
() => {
275+
error: () => {
296276
this.processing = false;
297277
this.dialogRef.close({
298278
result: true,
@@ -301,14 +281,14 @@ export class CreateServiceMemberDialogComponent implements OnInit, AfterViewInit
301281
findSponsorsAuth: this.findSponsorsAuth,
302282
serviceMemberId: member.id,
303283
});
304-
}
305-
);
284+
},
285+
});
306286
}
307287
}
308288

309289
validateMember(memberId: number, sponsor: boolean, showNotification = true): void {
310-
this.membersManagerService.validateMemberAsync(memberId).subscribe(
311-
() => {
290+
this.membersManagerService.validateMemberAsync(memberId).subscribe({
291+
next: () => {
312292
if (showNotification) {
313293
this.notificator.showSuccess(this.successMessagePwd);
314294
}
@@ -320,10 +300,10 @@ export class CreateServiceMemberDialogComponent implements OnInit, AfterViewInit
320300
serviceMemberId: memberId,
321301
});
322302
},
323-
() => {
303+
error: () => {
324304
this.processing = false;
325-
}
326-
);
305+
},
306+
});
327307
}
328308

329309
onCancel(): void {
@@ -334,7 +314,7 @@ export class CreateServiceMemberDialogComponent implements OnInit, AfterViewInit
334314
this.loading = true;
335315
this.firstSearchDone = true;
336316
this.membersManagerService
337-
.findCompleteRichMembersForVo(this.data.vo.id, [''], this.searchCtrl.value as string)
317+
.findCompleteRichMembersForVo(this.data.vo.id, [''], this.searchCtrl.value)
338318
.subscribe((members) => {
339319
this.members = members.filter((m) => !m.user.specificUser);
340320
this.loading = false;

apps/admin-gui/src/app/shared/components/dialogs/create-attribute-definition-dialog/create-attribute-definition-dialog.component.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
AttributePolicyCollection,
77
AttributesManagerService,
88
} from '@perun-web-apps/perun/openapi';
9-
import { UntypedFormBuilder, Validators } from '@angular/forms';
9+
import { FormBuilder, Validators } from '@angular/forms';
1010
import { debounceTime, switchMap } from 'rxjs/operators';
1111
import { BehaviorSubject, of, zip } from 'rxjs';
1212
import { AttributeRightsService, NotificatorService } from '@perun-web-apps/perun/services';
@@ -73,7 +73,7 @@ export class CreateAttributeDefinitionDialogComponent {
7373

7474
constructor(
7575
private dialogRef: MatDialogRef<CreateAttributeDefinitionDialogComponent>,
76-
private formBuilder: UntypedFormBuilder,
76+
private formBuilder: FormBuilder,
7777
private attributeService: AttributesManagerService,
7878
private attributeRightsService: AttributeRightsService,
7979
private notificator: NotificatorService,
@@ -120,15 +120,15 @@ export class CreateAttributeDefinitionDialogComponent {
120120
)
121121
)
122122
)
123-
.subscribe(
124-
() => {
123+
.subscribe({
124+
next: () => {
125125
this.notificator.showSuccess(
126126
this.translate.instant('DIALOGS.CREATE_ATTRIBUTE_DEFINITION.SUCCESS') as string
127127
);
128128
this.dialogRef.close(true);
129129
},
130-
() => (this.loading = false)
131-
);
130+
error: () => (this.loading = false),
131+
});
132132
}
133133

134134
cancel(): void {

0 commit comments

Comments
 (0)