argos/dmp-frontend/src/app/dmp-profiles/dmp-profile-editor/dmp-profile-editor.componen...

183 lines
6.1 KiB
TypeScript
Raw Normal View History

2018-11-27 15:13:56 +01:00
import { AfterViewInit, Component, ViewEncapsulation } from '@angular/core';
import { AbstractControl, FormArray, FormControl, FormGroup } from '@angular/forms';
import { MatSnackBar } from '@angular/material';
import { ActivatedRoute, Params, Router } from '@angular/router';
import { TdDialogService } from '@covalent/core';
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';
2018-11-27 15:13:56 +01:00
import { environment } from '../../../environments/environment';
2018-11-27 18:33:17 +01:00
import { BaseComponent } from '../../core/common/base/base.component';
2018-11-27 15:13:56 +01:00
import { DataManagementPlanProfileModel } from '../../models/data-management-plan-profile/DataManagementPlanProfileModel';
import { DataManagementProfileField, DMPProfileFieldDataType, DMPProfileType } from '../../models/data-management-plan-profile/DataManagementProfileField';
2018-10-05 17:00:54 +02:00
import { BaseErrorModel } from '../../models/error/BaseErrorModel';
import { DataManagementPlanProfileService } from '../../services/data-management-plan-profile/datamanagement-profile.service';
2018-11-27 15:13:56 +01:00
import { SnackBarNotificationComponent } from '../../shared/components/notificaiton/snack-bar-notification.component';
import { JsonSerializer } from '../../utilities/JsonSerializer';
2018-10-05 17:00:54 +02:00
import { Utilities } from '../../utilities/utilities';
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',
styleUrls: ['./dmp-profile-editor.component.scss'],
providers: [DataManagementPlanProfileService, Utilities],
encapsulation: ViewEncapsulation.None
2018-03-28 15:24:47 +02:00
})
2018-11-27 18:33:17 +01:00
export class DataManagementPlanProfileEditorComponent extends BaseComponent implements AfterViewInit {
2018-03-28 15:24:47 +02:00
2018-10-05 17:00:54 +02:00
isNew = true;
dataManagementPlanProfileModel: DataManagementPlanProfileModel;
formGroup: FormGroup = null;
2018-11-27 15:13:56 +01:00
host = environment.Server;
2018-10-05 17:00:54 +02:00
baseErrorModel: BaseErrorModel;
constructor(
private dmpProfileService: DataManagementPlanProfileService,
private route: ActivatedRoute,
public snackBar: MatSnackBar,
public router: Router,
public language: TranslateService,
private dialogService: TdDialogService,
private utilities: Utilities
) {
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;
this.dmpProfileService.getSingle(itemId).map(data => data as DataManagementPlanProfileModel)
.pipe(takeUntil(this._destroyed))
.subscribe(data => {
this.dataManagementPlanProfileModel = JsonSerializer.fromJSONObject(data, DataManagementPlanProfileModel);
this.baseErrorModel = this.dataManagementPlanProfileModel.errorModel;
this.formGroup = this.dataManagementPlanProfileModel.buildForm();
});
} else {
this.dataManagementPlanProfileModel = new DataManagementPlanProfileModel();
this.baseErrorModel = this.dataManagementPlanProfileModel.errorModel;
setTimeout(() => {
2018-10-05 17:00:54 +02:00
this.formGroup = this.dataManagementPlanProfileModel.buildForm();
});
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 {
2018-11-27 18:33:17 +01:00
this.dmpProfileService.createDataManagementPlan(this.formGroup.value)
.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);
}
public setErrorModel(errorModel: BaseErrorModel) {
Object.keys(errorModel).forEach(item => {
(<any>this.dataManagementPlanProfileModel.errorModel)[item] = (<any>errorModel)[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() {
(<FormArray>this.formGroup.get('definition').get('fields')).push(new DataManagementProfileField().buildForm());
}
getDMPProfileFieldDataTypeValues(): Number[] {
let keys: string[] = Object.keys(DMPProfileFieldDataType);
keys = keys.slice(0, keys.length / 2);
const values: Number[] = keys.map(Number);
return values;
}
getDMPProfileFieldDataTypeWithLanguage(role: DMPProfileFieldDataType): string {
let result = '';
2018-11-27 18:33:17 +01:00
this.language.get(this.utilities.convertFromDMPProfileDataType(role))
.pipe(takeUntil(this._destroyed))
.subscribe((value: string) => {
result = value;
});
2018-10-05 17:00:54 +02:00
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(role: DMPProfileType): string {
let result = '';
2018-11-27 18:33:17 +01:00
this.language.get(this.utilities.convertFromDMPProfileType(role))
.pipe(takeUntil(this._destroyed))
.subscribe((value: string) => {
result = value;
});
2018-10-05 17:00:54 +02:00
return result;
}
2018-05-28 11:50:42 +02:00
}