2019-01-18 18:03:45 +01:00
|
|
|
import { AfterViewInit, Component } from '@angular/core';
|
2018-11-27 15:13:56 +01:00
|
|
|
import { AbstractControl, FormArray, FormControl, FormGroup } from '@angular/forms';
|
|
|
|
import { MatSnackBar } from '@angular/material';
|
|
|
|
import { ActivatedRoute, Params, Router } from '@angular/router';
|
2018-10-05 17:00:54 +02:00
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
2018-11-27 18:33:17 +01:00
|
|
|
import { takeUntil } from 'rxjs/operators';
|
2019-01-18 18:03:45 +01:00
|
|
|
import { environment } from '../../../../../environments/environment';
|
2019-01-21 12:14:20 +01:00
|
|
|
import { ValidationErrorModel } from '../../../../common/forms/validation/error-model/validation-error-model';
|
2019-01-18 18:03:45 +01:00
|
|
|
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';
|
2018-03-28 15:24:47 +02:00
|
|
|
|
|
|
|
@Component({
|
2018-10-05 17:00:54 +02:00
|
|
|
selector: 'app-dmp-profile-editor-component',
|
|
|
|
templateUrl: 'dmp-profile-editor.component.html',
|
2019-01-18 18:03:45 +01:00
|
|
|
styleUrls: ['./dmp-profile-editor.component.scss']
|
2018-03-28 15:24:47 +02:00
|
|
|
})
|
2019-01-18 18:03:45 +01:00
|
|
|
export class DmpProfileEditorComponent extends BaseComponent implements AfterViewInit {
|
2018-03-28 15:24:47 +02:00
|
|
|
|
2018-10-05 17:00:54 +02:00
|
|
|
isNew = true;
|
2019-01-18 18:03:45 +01:00
|
|
|
dmpProfileModel: DmpProfileEditorModel;
|
2018-10-05 17:00:54 +02:00
|
|
|
formGroup: FormGroup = null;
|
2018-11-27 15:13:56 +01:00
|
|
|
host = environment.Server;
|
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 dmpProfileService: DmpProfileService,
|
2018-10-05 17:00:54 +02:00
|
|
|
private route: ActivatedRoute,
|
2019-01-18 18:03:45 +01:00
|
|
|
private snackBar: MatSnackBar,
|
|
|
|
private router: Router,
|
|
|
|
private language: TranslateService,
|
|
|
|
private enumUtils: EnumUtils
|
2018-10-05 17:00:54 +02:00
|
|
|
) {
|
2018-11-27 18:33:17 +01:00
|
|
|
super();
|
2018-10-05 17:00:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ngAfterViewInit() {
|
2018-11-27 18:33:17 +01:00
|
|
|
this.route.params
|
|
|
|
.pipe(takeUntil(this._destroyed))
|
|
|
|
.subscribe((params: Params) => {
|
|
|
|
const itemId = params['id'];
|
|
|
|
|
|
|
|
if (itemId != null) {
|
|
|
|
this.isNew = false;
|
2019-01-18 18:03:45 +01:00
|
|
|
this.dmpProfileService.getSingle(itemId).map(data => data as DmpProfile)
|
2018-11-27 18:33:17 +01:00
|
|
|
.pipe(takeUntil(this._destroyed))
|
|
|
|
.subscribe(data => {
|
2019-01-18 18:03:45 +01:00
|
|
|
this.dmpProfileModel = new DmpProfileEditorModel().fromModel(data);
|
|
|
|
this.formGroup = this.dmpProfileModel.buildForm();
|
2018-11-27 18:33:17 +01:00
|
|
|
});
|
|
|
|
} else {
|
2019-01-18 18:03:45 +01:00
|
|
|
this.dmpProfileModel = new DmpProfileEditorModel();
|
2018-11-27 18:33:17 +01:00
|
|
|
setTimeout(() => {
|
2019-01-18 18:03:45 +01:00
|
|
|
this.formGroup = this.dmpProfileModel.buildForm();
|
2018-10-05 17:00:54 +02:00
|
|
|
});
|
2018-11-27 18:33:17 +01:00
|
|
|
}
|
|
|
|
});
|
2018-10-05 17:00:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
formSubmit(): void {
|
|
|
|
this.touchAllFormFields(this.formGroup);
|
|
|
|
if (!this.isFormValid()) { return; }
|
|
|
|
this.onSubmit();
|
|
|
|
}
|
|
|
|
|
|
|
|
public isFormValid() {
|
|
|
|
return this.formGroup.valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
onSubmit(): void {
|
2019-01-18 18:03:45 +01:00
|
|
|
this.dmpProfileService.createDmp(this.formGroup.value)
|
2018-11-27 18:33:17 +01:00
|
|
|
.pipe(takeUntil(this._destroyed))
|
|
|
|
.subscribe(
|
|
|
|
complete => this.onCallbackSuccess(),
|
|
|
|
error => this.onCallbackError(error)
|
|
|
|
);
|
2018-10-05 17:00:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-01-21 12:14:20 +01:00
|
|
|
public setErrorModel(validationErrorModel: ValidationErrorModel) {
|
|
|
|
Object.keys(validationErrorModel).forEach(item => {
|
|
|
|
(<any>this.dmpProfileModel.validationErrorModel)[item] = (<any>validationErrorModel)[item];
|
|
|
|
});
|
2018-10-05 17:00:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2019-01-18 18:03:45 +01:00
|
|
|
(<FormArray>this.formGroup.get('definition').get('fields')).push(new DmpProfileFieldEditorModel().buildForm());
|
|
|
|
}
|
|
|
|
|
|
|
|
removeField(index: number) {
|
|
|
|
(<FormArray>this.formGroup.get('definition').get('fields')).controls.splice(index, 1);
|
2018-10-05 17:00:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
getDMPProfileFieldDataTypeValues(): Number[] {
|
2019-01-18 18:03:45 +01:00
|
|
|
let keys: string[] = Object.keys(DmpProfileFieldDataType);
|
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
|
|
|
getDMPProfileFieldDataTypeWithLanguage(fieldType: DmpProfileFieldDataType): string {
|
2018-10-05 17:00:54 +02:00
|
|
|
let result = '';
|
2019-01-18 18:03:45 +01:00
|
|
|
this.language.get(this.enumUtils.toDmpProfileFieldDataTypeString(fieldType))
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
getDMPProfileFieldTypeValues(): Number[] {
|
2019-01-18 18:03:45 +01:00
|
|
|
let keys: string[] = Object.keys(DmpProfileType);
|
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
|
|
|
getDMPProfileFieldTypeWithLanguage(profileType: DmpProfileType): string {
|
2018-10-05 17:00:54 +02:00
|
|
|
let result = '';
|
2019-01-18 18:03:45 +01:00
|
|
|
this.language.get(this.enumUtils.toDmpProfileTypeString(profileType))
|
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;
|
|
|
|
}
|
2019-01-18 18:03:45 +01:00
|
|
|
|
|
|
|
delete() {
|
|
|
|
//TODO
|
|
|
|
}
|
2018-05-28 11:50:42 +02:00
|
|
|
}
|