2018-11-27 18:33:17 +01:00
|
|
|
import { Component, Input, OnInit } from '@angular/core';
|
|
|
|
import { AbstractControl, FormArray, FormBuilder, FormControl, FormGroup } from '@angular/forms';
|
2019-12-11 15:51:03 +01:00
|
|
|
import { AppRole } from '@app/core/common/enum/app-role';
|
|
|
|
import { UserListingModel } from '@app/core/model/user/user-listing';
|
|
|
|
import { SnackBarNotificationLevel, UiNotificationService } from '@app/core/services/notification/ui-notification-service';
|
|
|
|
import { UserService } from '@app/core/services/user/user.service';
|
|
|
|
import { EnumUtils } from '@app/core/services/utilities/enum-utils.service';
|
|
|
|
import { BaseComponent } from '@common/base/base.component';
|
|
|
|
import { Validation, ValidationContext } from '@common/forms/validation/validation-context';
|
2018-11-27 18:33:17 +01:00
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
|
|
|
import { takeUntil } from 'rxjs/operators';
|
2018-02-01 15:04:36 +01:00
|
|
|
|
|
|
|
@Component({
|
2018-10-05 17:00:54 +02:00
|
|
|
selector: 'app-user-role-editor-component',
|
|
|
|
templateUrl: './user-role-editor.component.html',
|
2019-01-18 18:03:45 +01:00
|
|
|
styleUrls: ['./user-role-editor.component.scss']
|
2018-02-01 15:04:36 +01:00
|
|
|
})
|
2018-11-27 18:33:17 +01:00
|
|
|
export class UserRoleEditorComponent extends BaseComponent implements OnInit {
|
2018-02-01 15:04:36 +01:00
|
|
|
|
2018-10-05 17:00:54 +02:00
|
|
|
@Input() public item: UserListingModel;
|
|
|
|
public formGroup: FormGroup = null;
|
|
|
|
public nowEditing = false;
|
2019-01-18 18:03:45 +01:00
|
|
|
|
2018-10-05 17:00:54 +02:00
|
|
|
constructor(
|
2019-01-18 18:03:45 +01:00
|
|
|
private language: TranslateService,
|
|
|
|
private userService: UserService,
|
|
|
|
private formBuilder: FormBuilder,
|
2019-01-24 11:46:29 +01:00
|
|
|
private enumUtils: EnumUtils,
|
2019-12-11 15:51:03 +01:00
|
|
|
private uiNotificationService: UiNotificationService
|
2018-11-27 18:33:17 +01:00
|
|
|
) { super(); }
|
2018-10-05 17:00:54 +02:00
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
if (this.formGroup == null) { this.formGroup = this.buildForm(); }
|
|
|
|
}
|
|
|
|
|
|
|
|
buildForm(): FormGroup {
|
|
|
|
const context: ValidationContext = this.createValidationContext();
|
|
|
|
|
|
|
|
return this.formBuilder.group({
|
|
|
|
appRoles: new FormControl({ value: this.item.appRoles, disabled: true }, context.getValidation('appRoles').validators)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
createValidationContext(): ValidationContext {
|
|
|
|
const validationContext: ValidationContext = new ValidationContext();
|
|
|
|
const validationArray: Validation[] = new Array<Validation>();
|
|
|
|
|
|
|
|
validationArray.push({ key: 'appRoles' });
|
|
|
|
|
|
|
|
validationContext.validation = validationArray;
|
|
|
|
return validationContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
formSubmit(): void {
|
|
|
|
|
2019-01-18 18:03:45 +01:00
|
|
|
const modifiedItem = this.item;
|
2018-10-05 17:00:54 +02:00
|
|
|
modifiedItem.appRoles = this.getFormControl('appRoles').value;
|
|
|
|
|
|
|
|
if (!this.isFormValid()) { return; }
|
2018-11-27 18:33:17 +01:00
|
|
|
this.userService.updateRoles(modifiedItem)
|
|
|
|
.pipe(takeUntil(this._destroyed))
|
|
|
|
.subscribe(
|
|
|
|
(res) => this.onCallbackSuccess(),
|
|
|
|
(error) => this.onCallbackError(error)
|
|
|
|
);
|
2018-10-05 17:00:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
editItem(): void {
|
|
|
|
this.formGroup.enable();
|
|
|
|
this.nowEditing = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
isFormValid(): boolean {
|
|
|
|
this.touchAllFormFields(this.formGroup);
|
|
|
|
this.validateAllFormFields(this.formGroup);
|
|
|
|
return this.formGroup.valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
getFormData(): any {
|
|
|
|
return this.formGroup.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
getFormControl(controlName: string): AbstractControl {
|
|
|
|
return this.formGroup.get(controlName);
|
|
|
|
}
|
|
|
|
|
|
|
|
validateAllFormFields(formControl: AbstractControl) {
|
|
|
|
if (formControl instanceof FormControl) {
|
|
|
|
formControl.updateValueAndValidity({ emitEvent: false });
|
|
|
|
} else if (formControl instanceof FormGroup) {
|
|
|
|
Object.keys(formControl.controls).forEach(item => {
|
|
|
|
const control = formControl.get(item);
|
|
|
|
this.validateAllFormFields(control);
|
|
|
|
});
|
|
|
|
} else if (formControl instanceof FormArray) {
|
|
|
|
formControl.controls.forEach(item => {
|
|
|
|
this.validateAllFormFields(item);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
touchAllFormFields(formControl: AbstractControl) {
|
|
|
|
if (formControl instanceof FormControl) {
|
|
|
|
formControl.markAsTouched();
|
|
|
|
} else if (formControl instanceof FormGroup) {
|
|
|
|
Object.keys(formControl.controls).forEach(item => {
|
|
|
|
const control = formControl.get(item);
|
|
|
|
this.touchAllFormFields(control);
|
|
|
|
});
|
|
|
|
} else if (formControl instanceof FormArray) {
|
|
|
|
formControl.controls.forEach(item => {
|
|
|
|
this.touchAllFormFields(item);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onCallbackSuccess() {
|
|
|
|
this.nowEditing = false;
|
|
|
|
this.formGroup.disable();
|
2019-12-11 15:51:03 +01:00
|
|
|
this.uiNotificationService.snackBarNotification(this.language.instant('GENERAL.SNACK-BAR.SUCCESSFUL-UPDATE'), SnackBarNotificationLevel.Success);
|
2018-10-05 17:00:54 +02:00
|
|
|
}
|
2019-01-24 11:46:29 +01:00
|
|
|
|
2018-10-05 17:00:54 +02:00
|
|
|
onCallbackError(error: any) {
|
|
|
|
this.validateAllFormFields(this.formGroup);
|
2019-12-11 15:51:03 +01:00
|
|
|
this.uiNotificationService.snackBarNotification(this.language.instant('GENERAL.SNACK-BAR.UNSUCCESSFUL-UPDATE'), SnackBarNotificationLevel.Error);
|
2018-10-05 17:00:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
getPrincipalAppRoleValues(): Number[] {
|
2019-01-18 18:03:45 +01:00
|
|
|
let keys: string[] = Object.keys(AppRole);
|
2018-10-05 17:00:54 +02:00
|
|
|
keys = keys.slice(0, keys.length / 2);
|
|
|
|
const values: Number[] = keys.map(Number);
|
|
|
|
return values;
|
|
|
|
}
|
|
|
|
|
2019-01-18 18:03:45 +01:00
|
|
|
getPrincipalAppRoleWithLanguage(role: AppRole): string {
|
2018-10-05 17:00:54 +02:00
|
|
|
let result = '';
|
2019-01-18 18:03:45 +01:00
|
|
|
this.language.get(this.enumUtils.convertFromPrincipalAppRole(role))
|
2018-11-27 18:33:17 +01:00
|
|
|
.pipe(takeUntil(this._destroyed))
|
|
|
|
.subscribe((value: string) => {
|
|
|
|
result = value;
|
|
|
|
});
|
2018-10-05 17:00:54 +02:00
|
|
|
return result;
|
|
|
|
}
|
2018-02-01 15:04:36 +01:00
|
|
|
}
|