import { HttpErrorResponse } from '@angular/common/http'; import { Component, OnInit, ViewChild } from '@angular/core'; import { FormArray, FormControl, FormGroup } from '@angular/forms'; import { MatHorizontalStepper } from '@angular/material'; import { ActivatedRoute, ParamMap, Router } from '@angular/router'; import { TranslateService } from '@ngx-translate/core'; import { map, takeUntil } from 'rxjs/operators'; import { BaseComponent } from '../../../../core/common/base/base.component'; import { DatasetProfile } from '../../../../core/model/admin/dataset-profile/dataset-profile'; import { DatasetWizardModel } from '../../../../core/model/dataset/dataset-wizard'; import { DatasetProfileService } from '../../../../core/services/dataset-profile/dataset-profile.service'; import { LoggingService } from '../../../../core/services/logging/logging-service'; import { SnackBarNotificationLevel, UiNotificationService } from '../../../../core/services/notification/ui-notification-service'; import { PageEditorModel } from '../admin/page-editor-model'; import { SectionEditorModel } from '../admin/section-editor-model'; import { DatasetProfileEditorModel } from './dataset-profile-editor-model'; @Component({ selector: 'app-dataset-profile-editor-component', templateUrl: './dataset-profile-editor.component.html', styleUrls: ['./dataset-profile-editor.component.scss'] }) export class DatasetProfileEditorComponent extends BaseComponent implements OnInit { isNew = true; isDeleted = false; dataModel: DatasetProfileEditorModel; form: FormGroup; previewerFormGroup: FormGroup; private datasetProfileId: string; dataWizardModel: DatasetWizardModel; @ViewChild('stepper') stepper: MatHorizontalStepper; constructor( private datasetProfileService: DatasetProfileService, private route: ActivatedRoute, private router: Router, private logger: LoggingService, private uiNotificationService: UiNotificationService, private language: TranslateService ) { super(); // this.profileID = route.snapshot.params['id']; // this.cloneId = route.snapshot.params['cloneid']; } ngOnInit() { this.route.paramMap.pipe(takeUntil(this._destroyed)).subscribe((paramMap: ParamMap) => { this.datasetProfileId = paramMap.get('id'); const cloneId = paramMap.get('cloneid'); if (this.datasetProfileId != null) { this.isNew = false; this.datasetProfileService.getDatasetProfileById(this.datasetProfileId) .pipe(map(data => data as DatasetProfile), takeUntil(this._destroyed)) .subscribe( data => { try { this.dataModel = new DatasetProfileEditorModel().fromModel(data); // this.isDeleted = this.masterItem.isActive === IsActive.Inactive; this.form = this.dataModel.buildForm(); this.prepareForm(); } catch { this.logger.error('Could not parse MasterItem: ' + data); this.uiNotificationService.snackBarNotification(this.language.instant('NOTIFICATIONS.DEFAULT.ERROR'), SnackBarNotificationLevel.Error); } }, error => this.onCallbackError(error) ); } else if (cloneId != null) { this.datasetProfileService.clone(cloneId) .pipe(map(data => data as DatasetProfile), takeUntil(this._destroyed)) .subscribe( data => { try { this.dataModel = new DatasetProfileEditorModel().fromModel(data); // this.isDeleted = this.masterItem.isActive === IsActive.Inactive; this.form = this.dataModel.buildForm(); this.prepareForm(); } catch { this.logger.error('Could not parse MasterItem: ' + data); this.uiNotificationService.snackBarNotification(this.language.instant('NOTIFICATIONS.DEFAULT.ERROR'), SnackBarNotificationLevel.Error); } }, error => this.onCallbackError(error) ); } else { this.dataModel = new DatasetProfileEditorModel(); this.form = this.dataModel.buildForm(); this.addSection(); this.addPage(); } }); } prepareForm() { this.form.valueChanges .pipe(takeUntil(this._destroyed)) .subscribe(change => { // this.datasetProfileService.preview(this.form.value) // .pipe(takeUntil(this._destroyed)) // .subscribe(dataset => { // const datasetModel = new DatasetWizardModel(); // datasetModel.datasetProfileDefinition = JsonSerializer.fromJSONObject(dataset, DatasetProfileDefinitionModel); // this.dataWizardModel = datasetModel; // this.previewerFormGroup = this.dataWizardModel.buildForm().get('datasetProfileDefinition'); // }); }); this.form.updateValueAndValidity(); } onIsMultiplicityEnabledChange(isMultiplicityEnabled: boolean) { if (!isMultiplicityEnabled) { (this.form.get('multiplicity').get('min')).setValue(0); (this.form.get('multiplicity').get('max')).setValue(0); } } addSection() { const section: SectionEditorModel = new SectionEditorModel(); this.dataModel.sections.push(section); (this.form.get('sections')).push(section.buildForm()); } addPage() { const page: PageEditorModel = new PageEditorModel(this.dataModel.pages.length); this.dataModel.pages.push(page); (this.form.get('pages')).push(page.buildForm()); } DeleteSection(index) { this.dataModel.sections.splice(index, 1); (this.form.get('sections')).removeAt(index); } onSubmit() { const data = this.form.value; if (this.datasetProfileId) { this.datasetProfileService.updateForm(this.datasetProfileId, data) .pipe(takeUntil(this._destroyed)) .subscribe(() => { this.router.navigate(['/dataset-profiles']); }); } else { this.datasetProfileService.createForm(data) .pipe(takeUntil(this._destroyed)) .subscribe(() => { this.router.navigate(['/dataset-profiles']); }); } } isStepActive(step: number) { return this.stepper && this.stepper.selectedIndex === step; } 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(['/master-items']); } onCallbackError(errorResponse: HttpErrorResponse) { // const error: HttpError = this.httpErrorHandlingService.getError(errorResponse); // if (error.statusCode === 400) { // this.masterItem.validationErrorModel.fromJSONObject(errorResponse.error); // this.formService.validateAllFormFields(this.formGroup); // } else { this.uiNotificationService.snackBarNotification(errorResponse.message, SnackBarNotificationLevel.Warning); // } } }