argos/dmp-frontend/src/app/dmps/editor/dmp-editor.component.ts

143 lines
4.8 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 { 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";
import { DataManagementPlanService } from "../../services/data-management-plan/data-management-plan.service";
import { DataManagementPlanModel } from "../../models/data-managemnt-plans/DataManagementPlanModel";
import { ExternalSourcesService } from "../../services/external-sources/external-sources.service";
import { ExternalSourcesItemModel } from "../../models/external-sources/ExternalSourcesItemModel";
@Component({
selector: 'app-dmp-editor-component',
templateUrl: 'dmp-editor.component.html',
styleUrls: ['./dmp-editor.component.scss'],
providers: [DataManagementPlanService, ExternalSourcesService],
encapsulation: ViewEncapsulation.None
})
export class DataManagementPlanEditorComponent implements AfterViewInit {
isNew = true;
dataManagementPlan: DataManagementPlanModel;
formGroup: FormGroup = null;
filteringOrganisationsAsync: boolean = false;
filteringResearchersAsync: boolean = false;
filteredOrganisations: ExternalSourcesItemModel[];
filteredResearchers: ExternalSourcesItemModel[];
constructor(
private dataManagementPlanService: DataManagementPlanService,
private externalSourcesService: ExternalSourcesService,
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.dataManagementPlanService.getSingle(itemId).map(data => data as DataManagementPlanModel)
.subscribe(data => {
this.dataManagementPlan = new JsonSerializer<DataManagementPlanModel>().fromJSONObject(data, DataManagementPlanModel);
this.formGroup = this.dataManagementPlan.buildForm();
});
} else {
this.dataManagementPlan = new DataManagementPlanModel();
setTimeout(() => {
this.formGroup = this.dataManagementPlan.buildForm();
});
}
});
}
formSubmit(): void {
//this.touchAllFormFields(this.formGroup);
if (!this.isFormValid()) { return; }
this.onSubmit();
}
public isFormValid() {
return this.formGroup.valid;
}
onSubmit(): void {
this.dataManagementPlanService.createDataManagementPlan(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(['/dmps']);
}
onCallbackError(error: any) {
this.setErrorModel(error.error);
//this.validateAllFormFields(this.formGroup);
}
public setErrorModel(errorModel: BaseErrorModel) {
Object.keys(errorModel).forEach(item => {
(<any>this.dataManagementPlan.errorModel)[item] = (<any>errorModel)[item];
})
}
public cancel(): void {
this.router.navigate(['/dmps']);
}
filterOrganisations(value: string): void {
this.filteredOrganisations = undefined;
if (value) {
this.filteringOrganisationsAsync = true;
this.externalSourcesService.searchDMPOrganizations(value).subscribe(items => {
this.filteredOrganisations = items;
this.filteringOrganisationsAsync = false;
// this.filteredOrganisations = items.filter((filteredObj: any) => {
// return this.objectsModel ? this.objectsModel.indexOf(filteredObj) < 0 : true;
// });
});
}
}
filterResearchers(value: string): void {
this.filteredResearchers = undefined;
if (value) {
this.filteringResearchersAsync = true;
this.externalSourcesService.searchDMPResearchers(value).subscribe(items => {
this.filteredResearchers = items;
this.filteringResearchersAsync = false;
// this.filteredOrganisations = items.filter((filteredObj: any) => {
// return this.objectsModel ? this.objectsModel.indexOf(filteredObj) < 0 : true;
// });
});
}
}
}