diff --git a/alfa-client/libs/admin/user/src/lib/user-form/user-form-roles/user-form-roles.component.spec.ts b/alfa-client/libs/admin/user/src/lib/user-form/user-form-roles/user-form-roles.component.spec.ts index d657690baab3ba8e17c0e36085a698633ae61674..703a699d979c9c0912d08d2b51f5091159c08c0a 100644 --- a/alfa-client/libs/admin/user/src/lib/user-form/user-form-roles/user-form-roles.component.spec.ts +++ b/alfa-client/libs/admin/user/src/lib/user-form/user-form-roles/user-form-roles.component.spec.ts @@ -98,21 +98,21 @@ describe('UserFormRolesComponent', () => { }); describe('handle alfa role change', () => { - it('should call form service updateAlfaCheckboxes', () => { + it('should call form service changeAlfaRole', () => { const formControlName: string = 'dummy'; const value: boolean = true; component.handleAlfaRoleChange(formControlName, value); - expect(formService.updateAlfaCheckboxes).toHaveBeenCalledWith(formControlName, value); + expect(formService.changeAlfaRole).toHaveBeenCalledWith(formControlName, value); }); }); describe('handle admin role change', () => { - it('should call form service removeCheckboxError', () => { + it('should call form service removeClientRolesValidationErrors', () => { component.handleAdminRoleChange(); - expect(formService.removeCheckboxError).toHaveBeenCalled(); + expect(formService.removeClientRolesValidationErrors).toHaveBeenCalled(); }); }); }); diff --git a/alfa-client/libs/admin/user/src/lib/user-form/user-form-roles/user-form-roles.component.ts b/alfa-client/libs/admin/user/src/lib/user-form/user-form-roles/user-form-roles.component.ts index 9d3288e4c0225f22f78a72c948e32ec61c744551..04bdc68434341f2d226954e0b0340dc56ad4a4ab 100644 --- a/alfa-client/libs/admin/user/src/lib/user-form/user-form-roles/user-form-roles.component.ts +++ b/alfa-client/libs/admin/user/src/lib/user-form/user-form-roles/user-form-roles.component.ts @@ -41,10 +41,10 @@ export class UserFormRolesComponent implements OnInit { } public handleAlfaRoleChange(formControlName: string, value: boolean) { - this.formService.updateAlfaCheckboxes(formControlName, value); + this.formService.changeAlfaRole(formControlName, value); } public handleAdminRoleChange() { - this.formService.removeCheckboxError(); + this.formService.removeClientRolesValidationErrors(); } } diff --git a/alfa-client/libs/admin/user/src/lib/user-form/user-form-save-button/user-form-save-button.component.html b/alfa-client/libs/admin/user/src/lib/user-form/user-form-save-button/user-form-save-button.component.html index c38b3871bcc6324394b118055c18a363c9fb56b9..40841d6b9be5727178fd85bef19eb653e0ec2dcd 100644 --- a/alfa-client/libs/admin/user/src/lib/user-form/user-form-save-button/user-form-save-button.component.html +++ b/alfa-client/libs/admin/user/src/lib/user-form/user-form-save-button/user-form-save-button.component.html @@ -2,4 +2,5 @@ [stateResource]="submitStateResource$ | async" text="Speichern" dataTestId="save-button" + type="submit" /> diff --git a/alfa-client/libs/admin/user/src/lib/user-form/user.formservice.spec.ts b/alfa-client/libs/admin/user/src/lib/user-form/user.formservice.spec.ts index f4eece3f17ff3899627903e43df570d9dd6cab57..71ea6569bdcc5da1257ec1078b124e8259c0b61f 100644 --- a/alfa-client/libs/admin/user/src/lib/user-form/user.formservice.spec.ts +++ b/alfa-client/libs/admin/user/src/lib/user-form/user.formservice.spec.ts @@ -136,7 +136,7 @@ describe('UserFormService', () => { beforeEach(() => { userService.getUserById.mockReturnValue(singleCold(loadedUser)); - service._updateAlfaCheckboxStates = jest.fn(); + service._updateAlfaRoleStates = jest.fn(); }); it('should call service to get user by id', () => { @@ -151,20 +151,20 @@ describe('UserFormService', () => { expect(response).toBeObservable(singleCold(loadedUser)); }); - it('should update alfa roles after user is loaded', () => { + it('should update alfa role states after user is loaded', () => { userService.getUserById.mockReturnValue(of(loadedUser)); service._load(id).subscribe(); - expect(service._updateAlfaCheckboxStates).toHaveBeenCalled(); + expect(service._updateAlfaRoleStates).toHaveBeenCalled(); }); - it('should not update alfa roles if user is not loaded', () => { + it('should not update alfa role states if user is not loaded', () => { userService.getUserById.mockReturnValue(of(createEmptyStateResource())); service._load(id).subscribe(); - expect(service._updateAlfaCheckboxStates).not.toHaveBeenCalled(); + expect(service._updateAlfaRoleStates).not.toHaveBeenCalled(); }); }); @@ -218,115 +218,104 @@ describe('UserFormService', () => { }); }); - describe('updateAlfaCheckboxStates', () => { - it('should call disableUncheckedCheckboxes if any checkbox is checked', () => { - service._isAnyAlfaCheckboxChecked = jest.fn().mockReturnValue(true); - service._disableUncheckedAlfaCheckboxes = jest.fn(); + describe('updateAlfaRoleStates', () => { + it('should disable alfa roles if no role is assigned', () => { + service._isAnyAlfaRoleAssigned = jest.fn().mockReturnValue(true); + service._disableAlfaRoles = jest.fn(); - service._updateAlfaCheckboxStates(); + service._updateAlfaRoleStates(); - expect(service._disableUncheckedAlfaCheckboxes).toHaveBeenCalled(); + expect(service._disableAlfaRoles).toHaveBeenCalled(); }); - it('should call enableAllAlfaCheckboxes if any checkbox is checked', () => { - service._isAnyAlfaCheckboxChecked = jest.fn().mockReturnValue(false); - service._enableAllAlfaCheckboxes = jest.fn(); + it('should enable alfa roles if any role is assigned', () => { + service._isAnyAlfaRoleAssigned = jest.fn().mockReturnValue(false); + service._enableAlfaRoles = jest.fn(); - service._updateAlfaCheckboxStates(); + service._updateAlfaRoleStates(); - expect(service._enableAllAlfaCheckboxes).toHaveBeenCalled(); + expect(service._enableAlfaRoles).toHaveBeenCalled(); }); }); - describe('isAnyAlfaCheckboxChecked', () => { - it('should return false if no checkbox is checked', () => { - const result = service._isAnyAlfaCheckboxChecked(); + describe('isAnyAlfaRoleAssigned', () => { + it('should return false if no role is assigned', () => { + const result = service._isAnyAlfaRoleAssigned(); expect(result).toBe(false); }); - it('should return true if any checkbox is checked', () => { + it('should return true if any role is assigned', () => { alfaGroup.get(UserFormService.LOESCHEN).setValue(true); - const result = service._isAnyAlfaCheckboxChecked(); + const result = service._isAnyAlfaRoleAssigned(); expect(result).toBe(true); }); }); - describe('disableUncheckedAlfaCheckboxes', () => { - it('if control value is false then control should be disabled', () => { + describe('disableAlfaRoles', () => { + it('if role is not assigned it should be disabled', () => { const control: AbstractControl = alfaGroup.get(UserFormService.LOESCHEN); control.setValue(false); - service._disableUncheckedAlfaCheckboxes(); + service._disableAlfaRoles(); expect(control.disabled).toBe(true); }); - it('if control value is true then control should NOT be disabled', () => { + it('if role is assigned then it should NOT be disabled', () => { const control: AbstractControl = alfaGroup.get(UserFormService.LOESCHEN); control.setValue(true); - service._disableUncheckedAlfaCheckboxes(); + service._disableAlfaRoles(); expect(control.disabled).toBe(false); }); }); - describe('updateCheckboxStates', () => { - it('if control value is false then control should be disabled', () => { - const control: AbstractControl = alfaGroup.get(UserFormService.LOESCHEN); - control.setValue(false); - - service._disableUncheckedAlfaCheckboxes(); - - expect(control.disabled).toBe(true); - }); - }); - - describe('updateAlfaCheckboxes', () => { + describe('changeAlfaRole', () => { it('should set control value', () => { const control: AbstractControl = alfaGroup.get(UserFormService.LOESCHEN); - service.updateAlfaCheckboxes(UserFormService.LOESCHEN, true); + service.changeAlfaRole(UserFormService.LOESCHEN, true); expect(control.value).toBe(true); }); - it('should call removeCheckboxError', () => { - service.removeCheckboxError = jest.fn(); + it('should call removeClientRolesValidationErrors', () => { + service.removeClientRolesValidationErrors = jest.fn(); - service.updateAlfaCheckboxes(UserFormService.LOESCHEN, true); + service.changeAlfaRole(UserFormService.LOESCHEN, true); - expect(service.removeCheckboxError).toHaveBeenCalled(); + expect(service.removeClientRolesValidationErrors).toHaveBeenCalled(); }); - it('should call disableAlfaCheckboxes', () => { - service._updateAlfaCheckboxStates = jest.fn(); + it('should call updateAlfaRoleStates', () => { + service._updateAlfaRoleStates = jest.fn(); - service.updateAlfaCheckboxes(UserFormService.LOESCHEN, true); + service.changeAlfaRole(UserFormService.LOESCHEN, true); - expect(service._updateAlfaCheckboxStates).toHaveBeenCalled(); + expect(service._updateAlfaRoleStates).toHaveBeenCalled(); }); }); - describe('removeCheckboxError', () => { + describe('removeClientRolesValidationErrors', () => { it('should remove error on clientRoles group', () => { roleGroup.setErrors({ error: 'Client Roles Error' }); - service.removeCheckboxError(); + service.removeClientRolesValidationErrors(); expect(roleGroup.errors).toBeNull(); }); }); - describe('enableAllAlfaCheckboxes', () => { + describe('enableAlfaRoles', () => { it('if control value is true then control should be enabled', () => { const control: AbstractControl = alfaGroup.get(UserFormService.LOESCHEN); const enableSpy: jest.SpyInstance = jest.spyOn(control, 'enable'); - service._enableAllAlfaCheckboxes(); + service._enableAlfaRoles(); expect(enableSpy).toHaveBeenCalled(); }); diff --git a/alfa-client/libs/admin/user/src/lib/user-form/user.formservice.ts b/alfa-client/libs/admin/user/src/lib/user-form/user.formservice.ts index e083adaeac2e317d0f2ab37ee470ee0dcf3a4126..2e717f691285c8079a9fcc0434a824a17cd5399c 100644 --- a/alfa-client/libs/admin/user/src/lib/user-form/user.formservice.ts +++ b/alfa-client/libs/admin/user/src/lib/user-form/user.formservice.ts @@ -97,7 +97,7 @@ export class UserFormService extends KeycloakFormService<User> { _load(id: string): Observable<StateResource<User>> { return this.userService.getUserById(id).pipe( filter(isLoaded), - tap(() => this._updateAlfaCheckboxStates()), + tap(() => this._updateAlfaRoleStates()), ); } @@ -162,52 +162,52 @@ export class UserFormService extends KeycloakFormService<User> { }); } - updateAlfaCheckboxes(formControlName: string, value: boolean) { - this.setControlValueInAlfa(formControlName, value); - this.removeCheckboxError(); - this._updateAlfaCheckboxStates(); + public changeAlfaRole(formControlName: string, value: boolean) { + this.setAlfaRole(formControlName, value); + this.removeClientRolesValidationErrors(); + this._updateAlfaRoleStates(); } - private setControlValueInAlfa(formControlName: string, value: boolean): void { + private setAlfaRole(formControlName: string, value: boolean): void { this.getRoleGroup(UserFormService.ALFA_GROUP).get(formControlName).setValue(value, { emitEvent: false }); } - removeCheckboxError() { + public removeClientRolesValidationErrors() { this.form.get(UserFormService.CLIENT_ROLES).setErrors(null); } - _updateAlfaCheckboxStates(): void { - if (this._isAnyAlfaCheckboxChecked()) { - this._disableUncheckedAlfaCheckboxes(); + _updateAlfaRoleStates(): void { + if (this._isAnyAlfaRoleAssigned()) { + this._disableAlfaRoles(); } else { - this._enableAllAlfaCheckboxes(); + this._enableAlfaRoles(); } } - _isAnyAlfaCheckboxChecked(): boolean { + _isAnyAlfaRoleAssigned(): boolean { const alfaGroup: UntypedFormGroup = this.getRoleGroup(UserFormService.ALFA_GROUP); return Object.keys(alfaGroup.controls).some((key) => alfaGroup.controls[key].value); } - _disableUncheckedAlfaCheckboxes(): void { + _disableAlfaRoles(): void { for (const control of Object.values<AbstractControl>(this.getRoleGroup(UserFormService.ALFA_GROUP).controls)) { - if (!control.value) this.disableCheckbox(control); + if (!control.value) this.disableControl(control); } } - _enableAllAlfaCheckboxes() { + _enableAlfaRoles() { const alfaGroup: UntypedFormGroup = this.getRoleGroup(UserFormService.ALFA_GROUP); for (const control of Object.values<AbstractControl>(alfaGroup.controls)) { - this.enableCheckbox(control); + this.enableControl(control); } } - private enableCheckbox(control: AbstractControl): void { - control.enable({ emitEvent: false }); + private enableControl(control: AbstractControl): void { + control.enable({ onlySelf: true }); } - private disableCheckbox(control: AbstractControl): void { - if (!control.value) control.disable({ emitEvent: false }); + private disableControl(control: AbstractControl): void { + if (!control.value) control.disable({ onlySelf: true }); } _doSubmit(): Observable<StateResource<User>> { diff --git a/alfa-client/libs/design-component/src/lib/button-with-spinner/button-with-spinner.component.ts b/alfa-client/libs/design-component/src/lib/button-with-spinner/button-with-spinner.component.ts index 79e617880c423eefeb7ce3e3e41877c32154e29f..f7a4b9f1096eefd7191bf5a064c3f7e00319e49d 100644 --- a/alfa-client/libs/design-component/src/lib/button-with-spinner/button-with-spinner.component.ts +++ b/alfa-client/libs/design-component/src/lib/button-with-spinner/button-with-spinner.component.ts @@ -42,6 +42,7 @@ type ButtonVariants = VariantProps<typeof buttonVariants>; [text]="text" [variant]="variant" [size]="size" + [type]="type" [dataTestId]="dataTestId" [isLoading]="isLoading" [disabled]="disabled" @@ -59,6 +60,7 @@ export class ButtonWithSpinnerComponent { @Input() variant: ButtonVariants['variant'] = 'primary'; @Input() size: ButtonVariants['size'] = 'medium'; @Input() disabled: boolean = false; + @Input() type: 'button' | 'submit' = 'button'; @Output() public clickEmitter: EventEmitter<MouseEvent> = new EventEmitter<MouseEvent>(); diff --git a/alfa-client/libs/design-system/src/lib/button/button.component.ts b/alfa-client/libs/design-system/src/lib/button/button.component.ts index 78bc43acf3056cc5d13782d7d413008764dfb176..68a0d753e5b89c6f542cdd2345e7a3bb5c75f6c4 100644 --- a/alfa-client/libs/design-system/src/lib/button/button.component.ts +++ b/alfa-client/libs/design-system/src/lib/button/button.component.ts @@ -105,7 +105,7 @@ export type ButtonVariants = VariantProps<typeof buttonVariants>; standalone: true, imports: [CommonModule, SpinnerIconComponent], template: ` <button - type="submit" + [type]="type" [ngClass]="buttonVariants({ size, variant, disabled: isDisabled, destructive })" [attr.aria-disabled]="isDisabled" [attr.aria-label]="text" @@ -133,6 +133,7 @@ export class ButtonComponent { @Input() variant: ButtonVariants['variant']; @Input() size: ButtonVariants['size']; @Input() spinnerSize: IconVariants['size'] = 'medium'; + @Input() type: 'button' | 'submit' = 'button'; @Output() public clickEmitter: EventEmitter<MouseEvent> = new EventEmitter<MouseEvent>();