100 lines
3.1 KiB
TypeScript
100 lines
3.1 KiB
TypeScript
import { Component, ViewChild, OnInit, AfterViewInit, ViewEncapsulation } from "@angular/core";
|
|
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";
|
|
import { FormGroup } from "@angular/forms";
|
|
import { SnackBarNotificationComponent } from "../../shared/components/notificaiton/snack-bar-notification.component";
|
|
import { BaseErrorModel } from "../../models/error/BaseErrorModel";
|
|
|
|
|
|
|
|
@Component({
|
|
selector: 'app-project-editor-component',
|
|
templateUrl: 'project-editor.component.html',
|
|
styleUrls: ['./project-editor.component.scss'],
|
|
providers: [ProjectService],
|
|
encapsulation: ViewEncapsulation.None
|
|
})
|
|
export class ProjectEditorComponent implements AfterViewInit {
|
|
|
|
isNew = true;
|
|
project: ProjectModel;
|
|
formGroup: FormGroup = null;
|
|
|
|
constructor(
|
|
private projectService: ProjectService,
|
|
private route: ActivatedRoute,
|
|
public snackBar: MatSnackBar,
|
|
public router: Router,
|
|
public language: TranslateService
|
|
) {
|
|
|
|
}
|
|
|
|
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 => {
|
|
this.project = new JsonSerializer<ProjectModel>().fromJSONObject(data, ProjectModel);
|
|
this.formGroup = this.project.buildForm();
|
|
});
|
|
} else {
|
|
this.project = new ProjectModel();
|
|
setTimeout(() => {
|
|
this.formGroup = this.project.buildForm();
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
formSubmit(): void {
|
|
//this.touchAllFormFields(this.formGroup);
|
|
if (!this.isFormValid()) { return; }
|
|
this.onSubmit();
|
|
}
|
|
|
|
public isFormValid() {
|
|
return this.formGroup.valid;
|
|
}
|
|
|
|
onSubmit(): void {
|
|
this.projectService.createProject(this.formGroup.value).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,
|
|
extraClasses: ['snackbar-success']
|
|
})
|
|
this.router.navigate(['/projects']);
|
|
}
|
|
|
|
onCallbackError(error: any) {
|
|
this.setErrorModel(error.error);
|
|
//this.validateAllFormFields(this.formGroup);
|
|
}
|
|
|
|
public setErrorModel(errorModel: BaseErrorModel) {
|
|
Object.keys(errorModel).forEach(item => {
|
|
(<any>this.project.errorModel)[item] = (<any>errorModel)[item];
|
|
})
|
|
}
|
|
|
|
public cancel(): void {
|
|
this.router.navigate(['/projects']);
|
|
}
|
|
} |