import { Component, OnInit, Input } from '@angular/core'; import { BaseComponent } from "../../../core/common/base/base.component"; import { BreadcrumbItem } from '../../misc/breadcrumb/definition/breadcrumb-item'; import { Observable } from 'rxjs'; import { IBreadCrumbComponent } from '../../misc/breadcrumb/definition/IBreadCrumbComponent'; import { MatSnackBar } from '@angular/material'; import { Router, ActivatedRoute } from '@angular/router'; import { TranslateService } from '@ngx-translate/core'; import { FormGroup, AbstractControl, FormControl, FormArray } from '@angular/forms'; import { DmpEditorWizardModel } from './dmp-editor-wizard-model'; import { UiNotificationService, SnackBarNotificationLevel } from '../../../core/services/notification/ui-notification-service'; import { ValidationErrorModel } from '../../../common/forms/validation/error-model/validation-error-model'; import { SingleAutoCompleteConfiguration } from '../../../library/auto-complete/single/single-auto-complete-configuration'; import { DatasetProfileModel } from '../../../core/model/dataset/dataset-profile'; import { RequestItem } from '../../../core/query/request-item'; import { DatasetProfileCriteria } from '../../../core/query/dataset-profile/dataset-profile-criteria'; import { DmpService } from '../../../core/services/dmp/dmp.service'; @Component({ selector: 'app-quick-wizard-dmp-editor-component', templateUrl: 'dmp-editor-wizard.component.html', styleUrls: ['./dmp-editor-wizard.component.scss'] }) export class DmpEditorWizardComponent extends BaseComponent implements OnInit, IBreadCrumbComponent { breadCrumbs: Observable = Observable.of([]); isNew = true; dmp: DmpEditorWizardModel; @Input() formGroup: FormGroup; @Input() dmpLabel: FormGroup; //formGroup: FormGroup = null; private uiNotificationService: UiNotificationService profilesAutoCompleteConfiguration:SingleAutoCompleteConfiguration; filteredProfiles: DatasetProfileModel[]; filteredProfilesAsync = false; constructor( public snackBar: MatSnackBar, public router: Router, private route: ActivatedRoute, private _service: DmpService, public language: TranslateService, ) { super(); } ngOnInit(): void { this.profilesAutoCompleteConfiguration = { filterFn: this.filterProfiles.bind(this), initialItems: (extraData) => this.filterProfiles(''), displayFn: (item) => item['label'], titleFn: (item) => item['label'] }; if (this.formGroup == null) { this.dmp = new DmpEditorWizardModel(); this.formGroup = this.dmp.buildForm(); } this.formGroup.get('label').setValue("Dmp From Project : "+ this.dmpLabel.value); this.breadCrumbs = Observable.of([ { parentComponentName: 'project', label: 'Dmp', url: '/quick-wizard/dmp' }] ); } formSubmit(): void { this.touchAllFormFields(this.formGroup); if (!this.isFormValid()) { return; } this.onSubmit(); } 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 { // this.projectService.createProject(this.formGroup.value) // .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); this.router.navigate(['/dmp']); } onCallbackError(errorResponse: any) { this.setErrorModel(errorResponse.error.payload); this.validateAllFormFields(this.formGroup); } public setErrorModel(validationErrorModel: ValidationErrorModel) { Object.keys(validationErrorModel).forEach(item => { (this.dmp.validationErrorModel)[item] = (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); }); } } filterProfiles(value: string): Observable { this.filteredProfiles = undefined; this.filteredProfilesAsync = true; const request = new RequestItem(); const criteria = new DatasetProfileCriteria(); criteria.like = value; request.criteria = criteria; return this._service.searchDMPProfiles(request); } }