import { AfterViewInit, Component } from '@angular/core'; import { AbstractControl, FormArray, FormControl, FormGroup } from '@angular/forms'; import { MatSnackBar } from '@angular/material'; import { ActivatedRoute, Params, Router } from '@angular/router'; import { TranslateService } from '@ngx-translate/core'; import { takeUntil } from 'rxjs/operators'; import { environment } from '../../../../../environments/environment'; import { ValidationErrorModel } from '../../../../common/forms/validation/error-model/validation-error-model'; import { BaseComponent } from '../../../../core/common/base/base.component'; import { DmpProfileFieldDataType } from '../../../../core/common/enum/dmp-profile-field-type'; import { DmpProfileType } from '../../../../core/common/enum/dmp-profile-type'; import { DmpProfile } from '../../../../core/model/dmp-profile/dmp-profile'; import { DmpProfileService } from '../../../../core/services/dmp/dmp-profile.service'; import { EnumUtils } from '../../../../core/services/utilities/enum-utils.service'; import { SnackBarNotificationComponent } from '../../../../library/notification/snack-bar/snack-bar-notification.component'; import { DmpProfileEditorModel, DmpProfileFieldEditorModel } from './dmp-profile-editor.model'; @Component({ selector: 'app-dmp-profile-editor-component', templateUrl: 'dmp-profile-editor.component.html', styleUrls: ['./dmp-profile-editor.component.scss'] }) export class DmpProfileEditorComponent extends BaseComponent implements AfterViewInit { isNew = true; dmpProfileModel: DmpProfileEditorModel; formGroup: FormGroup = null; host = environment.Server; constructor( private dmpProfileService: DmpProfileService, private route: ActivatedRoute, private snackBar: MatSnackBar, private router: Router, private language: TranslateService, private enumUtils: EnumUtils ) { super(); } ngAfterViewInit() { this.route.params .pipe(takeUntil(this._destroyed)) .subscribe((params: Params) => { const itemId = params['id']; if (itemId != null) { this.isNew = false; this.dmpProfileService.getSingle(itemId).map(data => data as DmpProfile) .pipe(takeUntil(this._destroyed)) .subscribe(data => { this.dmpProfileModel = new DmpProfileEditorModel().fromModel(data); this.formGroup = this.dmpProfileModel.buildForm(); }); } else { this.dmpProfileModel = new DmpProfileEditorModel(); setTimeout(() => { this.formGroup = this.dmpProfileModel.buildForm(); }); } }); } formSubmit(): void { this.touchAllFormFields(this.formGroup); if (!this.isFormValid()) { return; } this.onSubmit(); } public isFormValid() { return this.formGroup.valid; } onSubmit(): void { this.dmpProfileService.createDmp(this.formGroup.value) .pipe(takeUntil(this._destroyed)) .subscribe( complete => this.onCallbackSuccess(), error => this.onCallbackError(error) ); } onCallbackSuccess(): void { this.snackBar.openFromComponent(SnackBarNotificationComponent, { data: { message: this.isNew ? 'GENERAL.SNACK-BAR.SUCCESSFUL-CREATION' : 'GENERAL.SNACK-BAR.SUCCESSFUL-UPDATE', language: this.language }, duration: 3000, }); this.router.navigate(['/dmp-profiles']); } onCallbackError(errorResponse: any) { this.setErrorModel(errorResponse.error); this.validateAllFormFields(this.formGroup); } public setErrorModel(validationErrorModel: ValidationErrorModel) { Object.keys(validationErrorModel).forEach(item => { (this.dmpProfileModel.validationErrorModel)[item] = (validationErrorModel)[item]; }); } public cancel(): void { this.router.navigate(['/dmp-profiles']); } public 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); }); } } public 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); }); } } addField() { (this.formGroup.get('definition').get('fields')).push(new DmpProfileFieldEditorModel().buildForm()); } removeField(index: number) { (this.formGroup.get('definition').get('fields')).controls.splice(index, 1); } getDMPProfileFieldDataTypeValues(): Number[] { let keys: string[] = Object.keys(DmpProfileFieldDataType); keys = keys.slice(0, keys.length / 2); const values: Number[] = keys.map(Number); return values; } getDMPProfileFieldDataTypeWithLanguage(fieldType: DmpProfileFieldDataType): string { let result = ''; this.language.get(this.enumUtils.toDmpProfileFieldDataTypeString(fieldType)) .pipe(takeUntil(this._destroyed)) .subscribe((value: string) => { result = value; }); return result; } getDMPProfileFieldTypeValues(): Number[] { let keys: string[] = Object.keys(DmpProfileType); keys = keys.slice(0, keys.length / 2); const values: Number[] = keys.map(Number); return values; } getDMPProfileFieldTypeWithLanguage(profileType: DmpProfileType): string { let result = ''; this.language.get(this.enumUtils.toDmpProfileTypeString(profileType)) .pipe(takeUntil(this._destroyed)) .subscribe((value: string) => { result = value; }); return result; } delete() { //TODO } }