argos/dmp-frontend/src/app/projects/editor/project-editor.component.ts

155 lines
5.2 KiB
TypeScript
Raw Normal View History

2017-12-18 11:01:22 +01:00
import { Component, ViewChild, OnInit, AfterViewInit, ViewEncapsulation } from "@angular/core";
2017-12-14 18:13:28 +01:00
import { MatPaginator, MatSort, MatSnackBar } from "@angular/material";
import { Router, ActivatedRoute, Params } from "@angular/router";
import { TranslateService } from "@ngx-translate/core";
import { DataSource } from "@angular/cdk/table";
import { Observable } from "rxjs/Observable";
import { DataManagementPlanService } from "../../services/data-management-plan/data-management-plan.service";
import { ProjectModel } from "../../models/projects/ProjectModel";
import { ProjectService } from "../../services/project/project.service";
import { JsonSerializer } from "../../utilities/JsonSerializer";
2018-01-03 17:36:31 +01:00
import { FormGroup, AbstractControl, FormControl, FormArray } from "@angular/forms";
2017-12-18 11:01:22 +01:00
import { SnackBarNotificationComponent } from "../../shared/components/notificaiton/snack-bar-notification.component";
import { BaseErrorModel } from "../../models/error/BaseErrorModel";
2017-12-18 16:53:17 +01:00
import { TdDialogService } from "@covalent/core";
2018-03-21 14:15:06 +01:00
import { ProjectFileUploaderService } from "@app/services/files/project-file-uploader.service";
import { HostConfiguration } from "@app/app.constants";
2017-12-14 18:13:28 +01:00
@Component({
selector: 'app-project-editor-component',
templateUrl: 'project-editor.component.html',
2017-12-18 11:01:22 +01:00
styleUrls: ['./project-editor.component.scss'],
2018-03-21 14:15:06 +01:00
providers: [ProjectService, ProjectFileUploaderService],
2017-12-18 11:01:22 +01:00
encapsulation: ViewEncapsulation.None
2017-12-14 18:13:28 +01:00
})
export class ProjectEditorComponent implements AfterViewInit {
isNew = true;
project: ProjectModel;
formGroup: FormGroup = null;
2018-03-21 14:15:06 +01:00
host = HostConfiguration.Server;
2017-12-14 18:13:28 +01:00
constructor(
private projectService: ProjectService,
private route: ActivatedRoute,
2017-12-18 11:01:22 +01:00
public snackBar: MatSnackBar,
public router: Router,
2017-12-18 16:53:17 +01:00
public language: TranslateService,
2018-03-21 14:15:06 +01:00
private dialogService: TdDialogService,
private uploaderService: ProjectFileUploaderService
2017-12-14 18:13:28 +01:00
) {
}
ngAfterViewInit() {
this.route.params.subscribe((params: Params) => {
const itemId = params['id'];
if (itemId != null) {
this.isNew = false;
this.projectService.getSingle(itemId).map(data => data as ProjectModel)
.subscribe(data => {
2018-01-03 17:36:31 +01:00
this.project = JsonSerializer.fromJSONObject(data, ProjectModel);
2017-12-14 18:13:28 +01:00
this.formGroup = this.project.buildForm();
});
} else {
this.project = new ProjectModel();
2017-12-18 11:01:22 +01:00
setTimeout(() => {
this.formGroup = this.project.buildForm();
});
2017-12-14 18:13:28 +01:00
}
});
}
2017-12-18 11:01:22 +01:00
formSubmit(): void {
2018-01-03 17:36:31 +01:00
this.touchAllFormFields(this.formGroup);
2017-12-18 16:53:17 +01:00
if (!this.isFormValid()) { return; }
this.onSubmit();
2017-12-18 11:01:22 +01:00
}
public isFormValid() {
2017-12-18 16:53:17 +01:00
return this.formGroup.valid;
}
2017-12-18 11:01:22 +01:00
onSubmit(): void {
2017-12-18 16:53:17 +01:00
this.projectService.createProject(this.formGroup.value).subscribe(
complete => this.onCallbackSuccess(),
error => this.onCallbackError(error)
);
2017-12-18 11:01:22 +01:00
}
2017-12-18 16:53:17 +01:00
2017-12-18 11:01:22 +01:00
onCallbackSuccess(): void {
2017-12-18 16:53:17 +01:00
this.snackBar.openFromComponent(SnackBarNotificationComponent, {
data: { message: this.isNew ? 'GENERAL.SNACK-BAR.SUCCESSFUL-CREATION' : 'GENERAL.SNACK-BAR.SUCCESSFUL-UPDATE', language: this.language },
duration: 3000,
extraClasses: ['snackbar-success']
})
this.router.navigate(['/projects']);
2017-12-18 11:01:22 +01:00
}
2017-12-18 16:53:17 +01:00
2018-01-03 17:36:31 +01:00
onCallbackError(errorResponse: any) {
this.setErrorModel(errorResponse.error);
this.validateAllFormFields(this.formGroup);
2017-12-18 16:53:17 +01:00
}
2017-12-18 11:01:22 +01:00
public setErrorModel(errorModel: BaseErrorModel) {
2017-12-18 16:53:17 +01:00
Object.keys(errorModel).forEach(item => {
(<any>this.project.errorModel)[item] = (<any>errorModel)[item];
})
2017-12-18 11:01:22 +01:00
}
2017-12-18 16:53:17 +01:00
2017-12-18 11:01:22 +01:00
public cancel(): void {
2017-12-18 16:53:17 +01:00
this.router.navigate(['/projects']);
}
public delete(): void {
this.language.get('GENERAL.DELETE-CONFIRMATION').subscribe((messages: any) => {
this.dialogService.openConfirm({
message: messages.MESSAGE,
title: messages.TITLE,
cancelButton: messages.NEGATIVE,
acceptButton: messages.POSITIVE
}).afterClosed().subscribe((accept: boolean) => {
if (accept) {
this.projectService.inactivate(this.project.id).subscribe(
complete => { this.router.navigate(['/projects']); },
error => this.onCallbackError(error)
);
}
});
});
}
2018-01-03 17:36:31 +01:00
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) {
2018-03-21 14:15:06 +01:00
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);
})
}
}
2017-12-14 18:13:28 +01:00
}