import { UserReferenceService } from '../../../core/services/user-reference-data.service'; import { TranslateService } from '@ngx-translate/core'; import { Component, OnInit, Input } from '@angular/core'; import { FormControl, FormGroup, NgForm, FormArray, AbstractControl, FormBuilder } from '@angular/forms'; import { ValidationContext, Validation } from '../../../utilities/validators/ValidationContext'; import { Principal } from '../../../models/login/Principal'; import { MatSnackBar } from '@angular/material'; import { UserListingModel } from 'app/shared/components/criteria/models/errormodel/users/userListingModel'; import { UserErrorModel } from 'app/shared/components/criteria/models/errormodel/users/userErrorModel'; import { SnackBarNotificationComponent } from 'app/shared/notification/snack-bar-notification.component'; @Component({ selector: 'user-role-editor-component', templateUrl: './user-role-editor.component.html', styleUrls: ['./user-role-editor.component.scss'], providers: [ UserReferenceService ] }) export class UserRoleEditorComponent implements OnInit { @Input() public item: UserListingModel; private formGroup: FormGroup = null; private nowEditing = false; constructor( private language: TranslateService, private userService: UserReferenceService, private errorModel: UserErrorModel, private formBuilder: FormBuilder, private snackBar: MatSnackBar ) { } ngOnInit() { if (this.errorModel == null) { this.errorModel = new UserErrorModel(); } if (this.formGroup == null) { this.formGroup = this.buildForm(); } } buildForm(): FormGroup { const context: ValidationContext = this.createValidationContext(); return this.formBuilder.group({ roles: new FormControl({ value: this.item.roles, disabled: true }, context.getValidation('roles').validators) }); } createValidationContext(): ValidationContext { const validationContext: ValidationContext = new ValidationContext(); const validationArray: Validation[] = new Array(); validationArray.push({ key: 'roles' }); validationContext.validation = validationArray; return validationContext; } formSubmit(): void { this.clearErrorModel(); const modifiedItem = new UserListingModel().fromJSONObject(this.item); modifiedItem.roles = this.getFormControl('roles').value; if (!this.isFormValid()) { return; } this.userService.updateRoles(modifiedItem).subscribe( (res) => this.onCallbackSuccess(), (error) => this.onCallbackError(error) ); } 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); }) } } setErrorModel(errorModel: UserErrorModel) { Object.keys(errorModel).forEach(item => { (this.errorModel)[item] = (errorModel)[item]; }) } clearErrorModel() { Object.keys(this.errorModel).forEach(item => { (this.errorModel)[item] = ''; }) } onCallbackSuccess() { this.nowEditing = false; this.formGroup.disable(); this.snackBar.openFromComponent(SnackBarNotificationComponent, { data: { message: 'GENERAL.SNACK-BAR.SUCCESSFUL-UPDATE', language: this.language }, duration: 3000, extraClasses: ['snackbar-success'] }); } onCallbackError(error: any) { this.setErrorModel(error.error); this.validateAllFormFields(this.formGroup); this.snackBar.openFromComponent(SnackBarNotificationComponent, { data: { message: 'GENERAL.SNACK-BAR.UNSUCCESSFUL-UPDATE', language: this.language }, duration: 3000, extraClasses: ['snackbar-warning'] }); } getPrincipalAppRoleValues(): Number[] { let keys: string[] = Object.keys(Principal.AppRole); keys = keys.slice(0, keys.length / 2); const values: Number[] = keys.map(Number); return values; } getPrincipalAppRoleWithLanguage(role: Principal.AppRole): string { let result = ''; /* this.language.get(new Utilities().convertFromPrincipalAppRole(role)).subscribe((value: string) => { result = value; }); */ return result; } }