2019-03-01 16:16:21 +01:00
|
|
|
import { Component, OnInit, ViewChild } from '@angular/core';
|
|
|
|
import { AbstractControl, FormArray, FormControl, FormGroup } from '@angular/forms';
|
|
|
|
import { MatSnackBar, MatStepper } from '@angular/material';
|
|
|
|
import { ActivatedRoute, Router } from '@angular/router';
|
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
|
|
|
import { Observable } from 'rxjs';
|
|
|
|
import { takeUntil } from 'rxjs/operators';
|
|
|
|
import { ValidationErrorModel } from '../../../common/forms/validation/error-model/validation-error-model';
|
|
|
|
import { BaseComponent } from "../../../core/common/base/base.component";
|
|
|
|
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-03-01 16:16:21 +01:00
|
|
|
import { BreadcrumbItem } from '../../misc/breadcrumb/definition/breadcrumb-item';
|
|
|
|
import { IBreadCrumbComponent } from '../../misc/breadcrumb/definition/IBreadCrumbComponent';
|
|
|
|
import { ProjectEditorWizardModel } from '../project-editor/project-editor-wizard-model';
|
|
|
|
import { QuickWizardEditorWizardModel } from './quick-wizard-editor.model';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-quick-wizard-editor-component',
|
|
|
|
templateUrl: 'quick-wizard-editor.component.html',
|
|
|
|
styleUrls: ['./quick-wizard-editor.component.scss']
|
|
|
|
})
|
|
|
|
export class QuickWizardEditorComponent extends BaseComponent implements OnInit, IBreadCrumbComponent {
|
|
|
|
|
|
|
|
breadCrumbs: Observable<BreadcrumbItem[]> = Observable.of([]);
|
|
|
|
|
|
|
|
@ViewChild('stepper') stepper: MatStepper;
|
|
|
|
isNew = true;
|
|
|
|
|
|
|
|
quickWizard: QuickWizardEditorWizardModel
|
|
|
|
formGroup: FormGroup = null;
|
|
|
|
firstStepFormGroup: FormGroup;
|
|
|
|
secondFormGroup: FormGroup;
|
|
|
|
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
public snackBar: MatSnackBar,
|
|
|
|
private route: ActivatedRoute,
|
|
|
|
public router: Router,
|
|
|
|
public language: TranslateService,
|
2019-03-05 12:21:04 +01:00
|
|
|
public quickWizardService: QuickWizardService,
|
|
|
|
private uiNotificationService: UiNotificationService
|
2019-03-01 16:16:21 +01:00
|
|
|
) {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit(): void {
|
|
|
|
this.quickWizard = new QuickWizardEditorWizardModel();
|
2019-03-05 13:04:08 +01:00
|
|
|
this.quickWizard.project = new ProjectEditorWizardModel();
|
2019-03-01 16:16:21 +01:00
|
|
|
this.formGroup = this.quickWizard.buildForm();
|
|
|
|
this.breadCrumbs = Observable.of([
|
|
|
|
{
|
|
|
|
parentComponentName: 'Dashboard',
|
2019-03-06 15:41:24 +01:00
|
|
|
label: 'Quick Create DMP',
|
2019-03-01 16:16:21 +01:00
|
|
|
url: '/quick-wizard'
|
|
|
|
}]
|
|
|
|
);
|
2019-03-06 15:41:24 +01:00
|
|
|
|
2019-03-01 16:16:21 +01:00
|
|
|
}
|
|
|
|
|
2019-03-05 13:04:08 +01:00
|
|
|
isActive(step: string): boolean {
|
2019-03-01 17:18:17 +01:00
|
|
|
switch (step) {
|
|
|
|
case 'step1':
|
2019-03-05 13:04:08 +01:00
|
|
|
return this.stepper.selectedIndex == 0;
|
2019-03-01 17:18:17 +01:00
|
|
|
case 'step2':
|
2019-03-05 13:04:08 +01:00
|
|
|
return this.stepper.selectedIndex == 1;
|
2019-03-01 17:18:17 +01:00
|
|
|
case 'step3':
|
2019-03-05 13:04:08 +01:00
|
|
|
return this.stepper.selectedIndex == 2;
|
2019-03-01 17:18:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-01 16:16:21 +01:00
|
|
|
formSubmit(): void {
|
|
|
|
this.touchAllFormFields(this.formGroup);
|
|
|
|
if (!this.isFormValid()) { return; }
|
2019-03-05 13:04:08 +01:00
|
|
|
if (this.formGroup.get('datasets') && this.formGroup.get('datasets').get('datasetsList') && (this.formGroup.get('datasets').get('datasetsList') as FormArray).length > 0) {
|
|
|
|
this.onSubmit();
|
|
|
|
} else {
|
2019-03-05 12:21:04 +01:00
|
|
|
return;
|
|
|
|
}
|
2019-03-01 16:16:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public isFormValid() {
|
|
|
|
return this.formGroup.valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onSubmit(): void {
|
|
|
|
if (!this.isFormValid()) { return; }
|
2019-03-05 12:21:04 +01:00
|
|
|
this.quickWizardService.createQuickWizard(this.formGroup.getRawValue())
|
2019-03-01 16:16:21 +01:00
|
|
|
.pipe(takeUntil(this._destroyed))
|
|
|
|
.subscribe(
|
|
|
|
complete => this.onCallbackSuccess(),
|
|
|
|
error => this.onCallbackError(error)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
onCallbackSuccess(): void {
|
|
|
|
this.uiNotificationService.snackBarNotification(this.isNew ? this.language.instant('GENERAL.SNACK-BAR.SUCCESSFUL-CREATION') : this.language.instant('GENERAL.SNACK-BAR.SUCCESSFUL-UPDATE'), SnackBarNotificationLevel.Success);
|
2019-03-06 15:41:24 +01:00
|
|
|
this.router.navigate(['/home']);
|
2019-03-01 16:16:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
onCallbackError(errorResponse: any) {
|
2019-03-05 12:21:04 +01:00
|
|
|
this.setErrorModel(errorResponse.error.payload);
|
2019-03-01 16:16:21 +01:00
|
|
|
this.validateAllFormFields(this.formGroup);
|
|
|
|
}
|
|
|
|
|
2019-03-05 12:21:04 +01:00
|
|
|
public setErrorModel(validationErrorModel: ValidationErrorModel) {
|
|
|
|
Object.keys(validationErrorModel).forEach(item => {
|
|
|
|
(<any>this.quickWizard.validationErrorModel)[item] = (<any>validationErrorModel)[item];
|
|
|
|
});
|
|
|
|
}
|
2019-03-01 16:16:21 +01:00
|
|
|
|
|
|
|
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-03-05 13:04:08 +01:00
|
|
|
getProjectLabel(): string {
|
|
|
|
if (this.formGroup.get('project').get('existProject').value) {
|
2019-03-05 12:21:04 +01:00
|
|
|
return this.formGroup.get('project').get('existProject').value['label'];
|
2019-03-05 13:04:08 +01:00
|
|
|
} else {
|
|
|
|
return this.formGroup.get('project').get('label').value;
|
2019-03-05 12:21:04 +01:00
|
|
|
}
|
2019-03-01 16:16:21 +01:00
|
|
|
}
|
|
|
|
|
2019-03-05 13:04:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-03-06 15:41:24 +01:00
|
|
|
}
|