import { Component, OnInit } from '@angular/core'; import { AbstractControl, FormArray, FormControl, FormGroup } from '@angular/forms'; import { MatDialog, MatSnackBar } from '@angular/material'; import { ActivatedRoute, Params, Router } from '@angular/router'; import { TranslateService } from '@ngx-translate/core'; import { Observable } from 'rxjs'; import { takeUntil } from 'rxjs/operators'; import { environment } from '../../../../environments/environment'; import { ValidationErrorModel } from '../../../common/forms/validation/error-model/validation-error-model'; import { BaseComponent } from '../../../core/common/base/base.component'; import { ProjectType } from '../../../core/common/enum/project-type'; import { ProjectListingModel } from '../../../core/model/project/project-listing'; import { ProjectFileUploadService } from '../../../core/services/project/project-file-upload.service'; import { ProjectService } from '../../../core/services/project/project.service'; import { ConfirmationDialogComponent } from '../../../library/confirmation-dialog/confirmation-dialog.component'; import { SnackBarNotificationComponent } from '../../../library/notification/snack-bar/snack-bar-notification.component'; import { BreadcrumbItem } from '../../misc/breadcrumb/definition/breadcrumb-item'; import { IBreadCrumbComponent } from '../../misc/breadcrumb/definition/IBreadCrumbComponent'; import { ProjectEditorModel } from './project-editor.model'; import { SnackBarNotificationLevel, UiNotificationService } from '../../../core/services/notification/ui-notification-service'; @Component({ selector: 'app-project-editor-component', templateUrl: 'project-editor.component.html', styleUrls: ['./project-editor.component.scss'] }) export class ProjectEditorComponent extends BaseComponent implements OnInit, IBreadCrumbComponent { breadCrumbs: Observable = Observable.of([]); isNew = true; project: ProjectEditorModel; formGroup: FormGroup = null; host = environment.Server; editMode = false; constructor( private projectService: ProjectService, private route: ActivatedRoute, public snackBar: MatSnackBar, public router: Router, public language: TranslateService, private dialog: MatDialog, private projectFileUploadService: ProjectFileUploadService, private uiNotificationService: UiNotificationService ) { super(); } ngOnInit() { this.route.params .pipe(takeUntil(this._destroyed)) .subscribe((params: Params) => { const itemId = params['id']; if (itemId != null) { this.isNew = false; this.projectService.getSingle(itemId).map(data => data as ProjectListingModel) .pipe(takeUntil(this._destroyed)) .subscribe(data => { this.project = new ProjectEditorModel().fromModel(data); this.formGroup = this.project.buildForm(null, this.project.type === ProjectType.External || !this.editMode); this.breadCrumbs = Observable.of([ { parentComponentName: 'ProjectListingComponent', label: 'Projects', url: '/projects' }, ]); }); } else { this.breadCrumbs = Observable.of([ { parentComponentName: 'ProjectListingComponent', label: 'Projects', url: '/projects' }, ]); this.project = new ProjectEditorModel(); setTimeout(() => { this.formGroup = this.project.buildForm(); }); } }); } formSubmit(): void { this.touchAllFormFields(this.formGroup); if (!this.isFormValid()) { return; } this.onSubmit(); } public isFormValid() { return this.formGroup.valid; } 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(['/projects']); } onCallbackError(errorResponse: any) { this.setErrorModel(errorResponse.error.payload); this.validateAllFormFields(this.formGroup); } public setErrorModel(validationErrorModel: ValidationErrorModel) { Object.keys(validationErrorModel).forEach(item => { (this.project.validationErrorModel)[item] = (validationErrorModel)[item]; }); } public cancel(): void { this.router.navigate(['/projects']); } public delete(): void { const dialogRef = this.dialog.open(ConfirmationDialogComponent, { maxWidth: '300px', data: { message: this.language.instant('GENERAL.CONFIRMATION-DIALOG.DELETE-ITEM'), confirmButton: this.language.instant('GENERAL.CONFIRMATION-DIALOG.ACTIONS.CONFIRM'), cancelButton: this.language.instant('GENERAL.CONFIRMATION-DIALOG.ACTIONS.CANCEL') } }); dialogRef.afterClosed().pipe(takeUntil(this._destroyed)).subscribe(result => { if (result) { this.projectService.delete(this.project.id) .pipe(takeUntil(this._destroyed)) .subscribe( complete => { this.onCallbackSuccess() }, error => this.onCallbackError(error) ); } }); } 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); }); } } 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); }); } } public enableForm() { if (!this.isExternalProject()) { this.editMode = true; this.formGroup.enable(); } } public disableForm() { this.editMode = false; this.formGroup.disable(); } public goToProjectDmps() { this.router.navigate(['plans/project/' + this.project.id], { queryParams: { projectLabel: this.project.label } }); } public isExternalProject() { return this.project.type === ProjectType.External; } public previewImage(event): void { const fileList: FileList | File = event.target.files; const formdata: FormData = new FormData(); if (fileList instanceof FileList) { for (let i = 0; i < fileList.length; i++) { formdata.append('file', fileList[i]); } } else { formdata.append('file', fileList); } this.projectFileUploadService.uploadFile(formdata) .pipe(takeUntil(this._destroyed)) .subscribe(files => this.formGroup.get('files').patchValue(files)); } }