2019-09-23 10:17:03 +02:00
|
|
|
|
2019-09-24 16:55:03 +02:00
|
|
|
import { of as observableOf, Observable } from 'rxjs';
|
|
|
|
import { Component, OnInit, ViewChild, HostListener } from '@angular/core';
|
2019-03-01 16:16:21 +01:00
|
|
|
import { AbstractControl, FormArray, FormControl, FormGroup } from '@angular/forms';
|
2019-09-23 10:17:03 +02:00
|
|
|
import { MatDialog } from '@angular/material/dialog';
|
|
|
|
import { MatSnackBar } from '@angular/material/snack-bar';
|
|
|
|
import { MatStepper } from '@angular/material/stepper';
|
2019-03-01 16:16:21 +01:00
|
|
|
import { ActivatedRoute, Router } from '@angular/router';
|
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
|
|
|
import { takeUntil } from 'rxjs/operators';
|
|
|
|
import { ValidationErrorModel } from '../../../common/forms/validation/error-model/validation-error-model';
|
|
|
|
import { BaseComponent } from "../../../core/common/base/base.component";
|
2019-07-01 11:35:09 +02:00
|
|
|
import { DatasetStatus } from '../../../core/common/enum/dataset-status';
|
|
|
|
import { DmpStatus } from '../../../core/common/enum/dmp-status';
|
|
|
|
import { DatasetService } from '../../../core/services/dataset/dataset.service';
|
2019-03-01 16:16:21 +01:00
|
|
|
import { SnackBarNotificationLevel, UiNotificationService } from '../../../core/services/notification/ui-notification-service';
|
2019-03-05 13:04:08 +01:00
|
|
|
import { QuickWizardService } from '../../../core/services/quick-wizard/quick-wizard.service';
|
2019-07-01 11:35:09 +02:00
|
|
|
import { ConfirmationDialogComponent } from '../../../library/confirmation-dialog/confirmation-dialog.component';
|
|
|
|
import { DmpFinalizeDialogComponent, DmpFinalizeDialogDataset, DmpFinalizeDialogInput } from '../../dmp/editor/dmp-finalize-dialog/dmp-finalize-dialog.component';
|
2019-03-01 16:16:21 +01:00
|
|
|
import { BreadcrumbItem } from '../../misc/breadcrumb/definition/breadcrumb-item';
|
|
|
|
import { IBreadCrumbComponent } from '../../misc/breadcrumb/definition/IBreadCrumbComponent';
|
2019-07-01 11:35:09 +02:00
|
|
|
import { DatasetEditorWizardComponent } from '../dataset-editor/dataset-editor-wizard.component';
|
2019-08-01 09:54:40 +02:00
|
|
|
import { GrantEditorWizardModel } from '../grant-editor/grant-editor-wizard-model';
|
2019-03-01 16:16:21 +01:00
|
|
|
import { QuickWizardEditorWizardModel } from './quick-wizard-editor.model';
|
2019-08-28 15:53:17 +02:00
|
|
|
import { FunderFormModel } from '../../dmp/editor/grant-tab/funder-form-model';
|
2019-08-28 17:34:04 +02:00
|
|
|
import { ProjectFormModel } from '../../dmp/editor/grant-tab/project-form-model';
|
2019-09-24 16:55:03 +02:00
|
|
|
import { CheckDeactivateBaseComponent } from '../../../library/deactivate/deactivate.component';
|
2019-03-01 16:16:21 +01:00
|
|
|
|
|
|
|
@Component({
|
2019-03-26 16:43:19 +01:00
|
|
|
selector: 'app-quick-wizard-editor-component',
|
|
|
|
templateUrl: 'quick-wizard-editor.component.html',
|
|
|
|
styleUrls: ['./quick-wizard-editor.component.scss']
|
2019-03-01 16:16:21 +01:00
|
|
|
})
|
2019-09-24 16:55:03 +02:00
|
|
|
export class QuickWizardEditorComponent extends CheckDeactivateBaseComponent implements OnInit, IBreadCrumbComponent {
|
2019-03-01 16:16:21 +01:00
|
|
|
|
2019-09-23 10:17:03 +02:00
|
|
|
breadCrumbs: Observable<BreadcrumbItem[]> = observableOf([]);
|
|
|
|
@ViewChild('stepper', { static: true }) stepper: MatStepper;
|
|
|
|
@ViewChild(DatasetEditorWizardComponent, { static: false }) datasetEditorWizardComponent: DatasetEditorWizardComponent;
|
2019-03-26 16:43:19 +01:00
|
|
|
isNew = true;
|
2019-09-24 16:55:03 +02:00
|
|
|
isSubmitted = false;
|
2019-07-01 11:35:09 +02:00
|
|
|
quickWizard: QuickWizardEditorWizardModel;
|
|
|
|
allDatasets: DmpFinalizeDialogDataset[] = [];
|
2019-03-26 16:43:19 +01:00
|
|
|
formGroup: FormGroup = null;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
public snackBar: MatSnackBar,
|
|
|
|
private route: ActivatedRoute,
|
|
|
|
public router: Router,
|
|
|
|
public language: TranslateService,
|
2019-07-01 11:35:09 +02:00
|
|
|
public datasetService: DatasetService,
|
2019-03-26 16:43:19 +01:00
|
|
|
public quickWizardService: QuickWizardService,
|
|
|
|
private uiNotificationService: UiNotificationService,
|
|
|
|
private dialog: MatDialog
|
|
|
|
) {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit(): void {
|
|
|
|
this.quickWizard = new QuickWizardEditorWizardModel();
|
2019-08-01 09:54:40 +02:00
|
|
|
this.quickWizard.grant = new GrantEditorWizardModel();
|
2019-08-28 15:53:17 +02:00
|
|
|
this.quickWizard.funder = new FunderFormModel();
|
2019-08-28 17:34:04 +02:00
|
|
|
this.quickWizard.project = new ProjectFormModel();
|
2019-03-26 16:43:19 +01:00
|
|
|
this.formGroup = this.quickWizard.buildForm();
|
2019-06-26 09:39:25 +02:00
|
|
|
this.language.get('NAV-BAR.DMP-WIZARD').pipe(takeUntil(this._destroyed)).subscribe(x => {
|
2019-09-23 10:17:03 +02:00
|
|
|
this.breadCrumbs = observableOf([
|
2019-06-26 09:39:25 +02:00
|
|
|
{
|
|
|
|
parentComponentName: 'Dashboard',
|
|
|
|
label: x,
|
|
|
|
url: '/quick-wizard'
|
|
|
|
}]
|
|
|
|
);
|
|
|
|
})
|
2019-03-26 16:43:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
isActive(step: string): boolean {
|
|
|
|
switch (step) {
|
|
|
|
case 'step1':
|
|
|
|
return this.stepper.selectedIndex == 0;
|
|
|
|
case 'step2':
|
|
|
|
return this.stepper.selectedIndex == 1;
|
|
|
|
case 'step3':
|
|
|
|
return this.stepper.selectedIndex == 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
formSubmit(): void {
|
|
|
|
this.touchAllFormFields(this.formGroup);
|
|
|
|
if (this.formGroup.get('datasets') && this.formGroup.get('datasets').get('datasetsList') && (this.formGroup.get('datasets').get('datasetsList') as FormArray).length > 0) {
|
|
|
|
for (let control of (this.formGroup.get('datasets').get('datasetsList') as FormArray).controls) {
|
2019-06-25 11:16:37 +02:00
|
|
|
control.get('status').setValue(DatasetStatus.Draft);
|
2019-03-14 19:00:02 +01:00
|
|
|
}
|
2019-03-26 16:43:19 +01:00
|
|
|
this.onSubmitSave();
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2019-03-14 10:50:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
saveFinalize() {
|
2019-07-01 11:35:09 +02:00
|
|
|
if (!this.isFormValid()) { return; }
|
|
|
|
|
|
|
|
const dialogInputModel: DmpFinalizeDialogInput = {
|
|
|
|
dmpLabel: this.formGroup.get('dmp').get('label').value,
|
|
|
|
dmpDescription: this.formGroup.get('dmp').get('description').value,
|
|
|
|
datasets: (this.formGroup.get('datasets').get('datasetsList') as FormArray).controls.map(x => {
|
|
|
|
return { label: x.get('datasetLabel').value, status: DatasetStatus.Finalized };
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-06-24 15:02:09 +02:00
|
|
|
const dialogRef = this.dialog.open(DmpFinalizeDialogComponent, {
|
|
|
|
maxWidth: '500px',
|
2019-09-27 10:09:29 +02:00
|
|
|
restoreFocus: false,
|
2019-06-24 15:02:09 +02:00
|
|
|
data: {
|
2019-07-01 11:35:09 +02:00
|
|
|
dialogInputModel: dialogInputModel,
|
2019-06-24 15:02:09 +02:00
|
|
|
confirmButton: this.language.instant('DMP-FINALISE-DIALOG.SUBMIT'),
|
|
|
|
cancelButton: this.language.instant('GENERAL.CONFIRMATION-DIALOG.ACTIONS.CANCEL'),
|
2019-03-26 16:43:19 +01:00
|
|
|
}
|
2019-06-24 15:02:09 +02:00
|
|
|
});
|
|
|
|
dialogRef.afterClosed().pipe(takeUntil(this._destroyed)).subscribe(result => {
|
2019-07-01 11:35:09 +02:00
|
|
|
if (result && !result.cancelled) {
|
|
|
|
if (this.formGroup.get('datasets') && this.formGroup.get('datasets').get('datasetsList') && (this.formGroup.get('datasets').get('datasetsList') as FormArray).length > 0) {
|
|
|
|
for (let control of (this.formGroup.get('datasets').get('datasetsList') as FormArray).controls) {
|
|
|
|
control.get('status').setValue(DatasetStatus.Finalized);
|
|
|
|
}
|
|
|
|
this.formGroup.get('dmp').get('status').setValue(DmpStatus.Finalized);
|
|
|
|
this.onSubmitSaveAndFinalize();
|
|
|
|
}
|
|
|
|
}
|
2019-06-24 15:02:09 +02:00
|
|
|
});
|
2019-03-26 16:43:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
hasDatasets() {
|
2019-09-25 12:40:35 +02:00
|
|
|
return this.formGroup.get('datasets').get('datasetsList').valid;
|
2019-03-26 16:43:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public isFormValid() {
|
2019-10-04 18:11:52 +02:00
|
|
|
return this.formGroup.get('grant').valid && this.formGroup.get('funder').valid;
|
2019-03-26 16:43:19 +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);
|
|
|
|
});
|
2019-03-14 19:00:02 +01:00
|
|
|
}
|
2019-03-14 10:50:06 +01:00
|
|
|
}
|
2019-03-01 16:16:21 +01:00
|
|
|
|
2019-07-01 11:35:09 +02:00
|
|
|
onSubmitSaveAndFinalize() {
|
2019-03-26 16:43:19 +01:00
|
|
|
this.quickWizardService.createQuickWizard(this.formGroup.getRawValue())
|
|
|
|
.pipe(takeUntil(this._destroyed))
|
|
|
|
.subscribe(
|
2019-06-24 15:02:09 +02:00
|
|
|
complete => this.onCallbackSuccess(),
|
|
|
|
error => this.onCallbackError(error)
|
2019-03-26 16:43:19 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
onSubmitSave(): void {
|
|
|
|
const dialogRef = this.dialog.open(ConfirmationDialogComponent, {
|
2019-09-27 10:09:29 +02:00
|
|
|
restoreFocus: false,
|
2019-03-26 16:43:19 +01:00
|
|
|
data: {
|
|
|
|
message: this.language.instant('QUICKWIZARD.SAVE-DIALOG.TITLE'),
|
|
|
|
confirmButton: this.language.instant('QUICKWIZARD.SAVE-DIALOG.ACTIONS.AFFIRMATIVE'),
|
2019-06-26 11:24:06 +02:00
|
|
|
cancelButton: this.language.instant('QUICKWIZARD.SAVE-DIALOG.ACTIONS.NEGATIVE'),
|
|
|
|
isDeleteConfirmation: false
|
2019-03-26 16:43:19 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
dialogRef.afterClosed().pipe(takeUntil(this._destroyed)).subscribe(result => {
|
|
|
|
if (result) {
|
2019-06-19 10:59:47 +02:00
|
|
|
this.datasetEditorWizardComponent.addDataset(false);
|
2019-03-26 16:43:19 +01:00
|
|
|
} else if (result === false) {
|
2019-04-02 09:53:38 +02:00
|
|
|
this.quickWizardService.createQuickWizard(this.formGroup.value)
|
2019-03-26 16:43:19 +01:00
|
|
|
.pipe(takeUntil(this._destroyed))
|
|
|
|
.subscribe(
|
2019-06-24 15:02:09 +02:00
|
|
|
complete => this.onCallbackSuccess()
|
2019-03-26 16:43:19 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onCallbackSuccess(): void {
|
2019-10-02 13:11:19 +02:00
|
|
|
this.isSubmitted = true;
|
2019-03-26 16:43:19 +01:00
|
|
|
this.uiNotificationService.snackBarNotification(this.isNew ? this.language.instant('GENERAL.SNACK-BAR.SUCCESSFUL-CREATION') : this.language.instant('GENERAL.SNACK-BAR.SUCCESSFUL-UPDATE'), SnackBarNotificationLevel.Success);
|
|
|
|
this.router.navigate(['/home']);
|
|
|
|
}
|
|
|
|
|
|
|
|
onCallbackError(errorResponse: any) {
|
|
|
|
this.setErrorModel(errorResponse.error.payload);
|
|
|
|
this.validateAllFormFields(this.formGroup);
|
|
|
|
}
|
|
|
|
|
|
|
|
public setErrorModel(validationErrorModel: ValidationErrorModel) {
|
|
|
|
Object.keys(validationErrorModel).forEach(item => {
|
|
|
|
(<any>this.quickWizard.validationErrorModel)[item] = (<any>validationErrorModel)[item];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public validateAllFormFields(formControl: AbstractControl) {
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-01 09:54:40 +02:00
|
|
|
getGrantLabel(): string {
|
|
|
|
if (this.formGroup.get('grant').get('existGrant').value) {
|
|
|
|
return this.formGroup.get('grant').get('existGrant').value['label'];
|
2019-03-26 16:43:19 +01:00
|
|
|
} else {
|
2019-08-01 09:54:40 +02:00
|
|
|
return this.formGroup.get('grant').get('label').value;
|
2019-03-26 16:43:19 +01:00
|
|
|
}
|
|
|
|
}
|
2019-09-24 16:55:03 +02:00
|
|
|
|
|
|
|
canDeactivate(): boolean {
|
|
|
|
return this.isSubmitted || !this.formGroup.dirty;
|
|
|
|
}
|
2019-03-06 15:41:24 +01:00
|
|
|
}
|