argos/dmp-frontend/src/app/datasets/editor/dataset-editor.component.ts

164 lines
5.2 KiB
TypeScript
Raw Normal View History

2017-12-21 10:26:11 +01:00
import { AfterViewInit, Component, Input, OnInit, ViewChild, ViewEncapsulation } from '@angular/core';
2017-12-18 15:28:08 +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 { 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 { DatasetService } from "../../services/dataset/dataset.service";
import { DatasetModel } from "../../models/datasets/DatasetModel";
import { ExternalSourcesService } from "../../services/external-sources/external-sources.service";
import { ExternalSourcesItemModel } from "../../models/external-sources/ExternalSourcesItemModel";
@Component({
selector: 'app-dataset-editor-component',
templateUrl: 'dataset-editor.component.html',
2017-12-19 13:47:08 +01:00
styleUrls: ['./dataset-editor.component.css'],
2017-12-18 15:28:08 +01:00
providers: [DatasetService, ExternalSourcesService],
encapsulation: ViewEncapsulation.None
})
export class DatasetEditorComponent implements AfterViewInit {
isNew = true;
2017-12-19 13:47:08 +01:00
dataset: DatasetModel;
2017-12-21 10:26:11 +01:00
@Input() formGroup: FormGroup = null;
2017-12-18 15:28:08 +01:00
2017-12-19 16:38:28 +01:00
filtereddataRepositoriesAsync: boolean = false;
filteredRegistriesAsync: boolean = false;
2017-12-21 10:26:11 +01:00
filteredServicesAsync: boolean = false;
2017-12-19 16:38:28 +01:00
filtereddataRepositories: ExternalSourcesItemModel[];
filteredRegistries: ExternalSourcesItemModel[];
2017-12-21 10:26:11 +01:00
filteredServices: ExternalSourcesItemModel[];
2017-12-18 15:28:08 +01:00
constructor(
private datasetService: DatasetService,
private externalSourcesService: ExternalSourcesService,
private route: ActivatedRoute,
public snackBar: MatSnackBar,
public router: Router,
public language: TranslateService,
) {
}
ngAfterViewInit() {
2017-12-21 10:26:11 +01:00
/* this.route.params.subscribe((params: Params) => {
2017-12-18 15:28:08 +01:00
const itemId = params['id'];
if (itemId != null) {
this.isNew = false;
this.datasetService.getSingle(itemId).map(data => data as DatasetModel)
.subscribe(data => {
2017-12-19 13:47:08 +01:00
this.dataset = new JsonSerializer<DatasetModel>().fromJSONObject(data, DatasetModel);
this.formGroup = this.dataset.buildForm();
2017-12-18 15:28:08 +01:00
});
} else {
2017-12-19 13:47:08 +01:00
this.dataset = new DatasetModel();
2017-12-18 15:28:08 +01:00
setTimeout(() => {
2017-12-19 13:47:08 +01:00
this.formGroup = this.dataset.buildForm();
2017-12-18 15:28:08 +01:00
});
}
2017-12-21 10:26:11 +01:00
}); */
2017-12-18 15:28:08 +01:00
}
formSubmit(): void {
//this.touchAllFormFields(this.formGroup);
if (!this.isFormValid()) { return; }
//this.onSubmit();
}
public isFormValid() {
return this.formGroup.valid;
}
2017-12-18 16:10:34 +01:00
onSubmit(): void {
this.datasetService.createDataset(this.formGroup.value).subscribe(
complete => this.onCallbackSuccess(),
error => this.onCallbackError(error)
);
}
2017-12-18 15:28:08 +01: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,
extraClasses: ['snackbar-success']
})
2017-12-19 13:47:08 +01:00
this.router.navigate(['/datasets']);
2017-12-18 15:28:08 +01:00
}
onCallbackError(error: any) {
this.setErrorModel(error.error);
//this.validateAllFormFields(this.formGroup);
}
public setErrorModel(errorModel: BaseErrorModel) {
Object.keys(errorModel).forEach(item => {
2017-12-19 13:47:08 +01:00
(<any>this.dataset.errorModel)[item] = (<any>errorModel)[item];
2017-12-18 15:28:08 +01:00
})
}
public cancel(): void {
2017-12-19 13:47:08 +01:00
this.router.navigate(['/datasets']);
2017-12-18 15:28:08 +01:00
}
2017-12-19 16:38:28 +01:00
filterdataRepositories(value: string): void {
2017-12-18 15:28:08 +01:00
2017-12-19 16:38:28 +01:00
this.filtereddataRepositories = undefined;
2017-12-18 15:28:08 +01:00
if (value) {
2017-12-19 16:38:28 +01:00
this.filtereddataRepositoriesAsync = true;
2017-12-18 15:28:08 +01:00
this.externalSourcesService.searchDMPOrganizations(value).subscribe(items => {
2017-12-19 16:38:28 +01:00
this.filtereddataRepositories = items;
this.filtereddataRepositoriesAsync = false;
2017-12-18 15:28:08 +01:00
// this.filteredOrganisations = items.filter((filteredObj: any) => {
// return this.objectsModel ? this.objectsModel.indexOf(filteredObj) < 0 : true;
// });
});
}
}
2017-12-19 16:38:28 +01:00
filterRegistries(value: string): void {
2017-12-18 15:28:08 +01:00
2017-12-19 16:38:28 +01:00
this.filteredRegistries = undefined;
2017-12-18 15:28:08 +01:00
if (value) {
2017-12-19 16:38:28 +01:00
this.filteredRegistriesAsync = true;
2017-12-18 15:28:08 +01:00
this.externalSourcesService.searchDMPResearchers(value).subscribe(items => {
2017-12-19 16:38:28 +01:00
this.filteredRegistries = items;
this.filteredRegistriesAsync = false;
2017-12-18 15:28:08 +01:00
// this.filteredOrganisations = items.filter((filteredObj: any) => {
// return this.objectsModel ? this.objectsModel.indexOf(filteredObj) < 0 : true;
// });
});
}
}
2017-12-21 10:26:11 +01:00
filterServices(value: string): void {
this.filteredServices = undefined;
if (value) {
this.filteredServicesAsync = true;
this.externalSourcesService.searchDMPResearchers(value).subscribe(items => {
this.filteredServices = items;
this.filteredServicesAsync = false;
// this.filteredOrganisations = items.filter((filteredObj: any) => {
// return this.objectsModel ? this.objectsModel.indexOf(filteredObj) < 0 : true;
// });
});
}
}
2017-12-18 15:28:08 +01:00
}