2018-11-27 18:33:17 +01:00
|
|
|
import { Component, Inject, OnInit, ViewChild } from '@angular/core';
|
|
|
|
import { AbstractControl, FormArray, FormBuilder, FormControl, FormGroup } from '@angular/forms';
|
|
|
|
import { MatDialogRef, MatSelectionList, MAT_DIALOG_DATA } from '@angular/material';
|
|
|
|
import { Router } from '@angular/router';
|
|
|
|
import { takeUntil } from 'rxjs/operators';
|
|
|
|
import { BaseComponent } from '../../../core/common/base/base.component';
|
|
|
|
import { DatasetCriteria } from '../../../models/criteria/dataset/DatasetCriteria';
|
2018-10-11 13:22:03 +02:00
|
|
|
import { DataManagementPlanModel } from '../../../models/data-managemnt-plans/DataManagementPlanModel';
|
|
|
|
import { DataTableRequest } from '../../../models/data-table/DataTableRequest';
|
2018-11-27 18:33:17 +01:00
|
|
|
import { DatasetListingModel } from '../../../models/datasets/DatasetListingModel';
|
2018-10-11 13:22:03 +02:00
|
|
|
import { DatasetStatus } from '../../../models/datasets/DatasetWizardModel';
|
2018-11-27 18:33:17 +01:00
|
|
|
import { DatasetService } from '../../../services/dataset/dataset.service';
|
2018-10-11 13:22:03 +02:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-dmp-finalise-dialog-component',
|
|
|
|
templateUrl: 'dmp-finalise-dialog.component.html',
|
|
|
|
|
|
|
|
})
|
2018-11-27 18:33:17 +01:00
|
|
|
export class DMPFinaliseDialogComponent extends BaseComponent implements OnInit {
|
2018-10-11 13:22:03 +02:00
|
|
|
|
|
|
|
@ViewChild('datasetsDraftSelectionList') selectionList: MatSelectionList;
|
|
|
|
public formGroup: FormGroup;
|
|
|
|
public submitFunction: (items: DatasetListingModel[]) => any;
|
|
|
|
public dmp: DataManagementPlanModel;
|
2018-10-12 10:22:23 +02:00
|
|
|
public datasetsFinalised: DatasetListingModel[];
|
|
|
|
public datasetsDraft: DatasetListingModel[];
|
2018-10-11 13:22:03 +02:00
|
|
|
constructor(
|
|
|
|
public router: Router,
|
|
|
|
public dialogRef: MatDialogRef<DMPFinaliseDialogComponent>,
|
|
|
|
public dmpService: DatasetService,
|
|
|
|
@Inject(MAT_DIALOG_DATA) public data: any
|
|
|
|
) {
|
2018-11-27 18:33:17 +01:00
|
|
|
super();
|
2018-10-11 13:22:03 +02:00
|
|
|
this.submitFunction = data['submitFunction'];
|
|
|
|
this.dmp = data['dmp'];
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit(): void {
|
2018-10-12 10:22:23 +02:00
|
|
|
this.initialiseDatasetFinalisedRequest();
|
|
|
|
this.initialiseDMPFinalisedRequest();
|
2018-10-11 13:22:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
initialiseDatasetFinalisedRequest() {
|
|
|
|
const request = new DataTableRequest<DatasetCriteria>(null, null, null);
|
|
|
|
request.criteria = new DatasetCriteria();
|
|
|
|
request.criteria.dmpIds = [this.dmp.id];
|
|
|
|
request.criteria.status = DatasetStatus.Finalised;
|
2018-11-27 18:33:17 +01:00
|
|
|
this.dmpService.getPaged(request).map(x => x.data)
|
|
|
|
.pipe(takeUntil(this._destroyed))
|
|
|
|
.subscribe(result => {
|
|
|
|
this.datasetsFinalised = result;
|
|
|
|
this.createFormGroup();
|
|
|
|
});
|
2018-10-11 13:22:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
initialiseDMPFinalisedRequest() {
|
|
|
|
const request = new DataTableRequest<DatasetCriteria>(null, null, null);
|
|
|
|
request.criteria = new DatasetCriteria();
|
|
|
|
request.criteria.dmpIds = [this.dmp.id];
|
|
|
|
request.criteria.status = DatasetStatus.Draft;
|
2018-11-27 18:33:17 +01:00
|
|
|
this.dmpService.getPaged(request).map(x => x.data)
|
|
|
|
.pipe(takeUntil(this._destroyed))
|
|
|
|
.subscribe(result => {
|
|
|
|
this.datasetsDraft = result;
|
|
|
|
});
|
2018-10-12 10:22:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
createFormGroup() {
|
|
|
|
this.formGroup = new FormBuilder().group({
|
|
|
|
datasets: [this.datasetsFinalised, this.minLengthArray(1)]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onSubmit() {
|
|
|
|
//this.formGroup.get('datasets').patchValue(this.formGroup.get('datasets').value.push(this.selectionList.selectedOptions.selected.map(x => x.value)));
|
|
|
|
this.touchAllFormFields(this.formGroup);
|
|
|
|
if (!this.formGroup.valid) { return; }
|
|
|
|
this.submitFunction(this.formGroup.get('datasets').value);
|
|
|
|
}
|
|
|
|
|
|
|
|
minLengthArray(min: number) {
|
|
|
|
return (c: AbstractControl): { [key: string]: any } => {
|
|
|
|
if (c.value.length >= min) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return { 'minLengthArray': { valid: false } };
|
|
|
|
};
|
2018-10-11 13:22:03 +02:00
|
|
|
}
|
|
|
|
|
2018-10-12 10:22:23 +02: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);
|
|
|
|
});
|
|
|
|
}
|
2018-10-11 13:22:03 +02:00
|
|
|
}
|
|
|
|
}
|