argos/dmp-frontend/src/app/ui/project/editor/project-editor.component.ts

204 lines
6.9 KiB
TypeScript
Raw Normal View History

2018-11-27 18:33:17 +01:00
import { Component, OnInit } from '@angular/core';
2018-11-27 15:13:56 +01:00
import { AbstractControl, FormArray, FormControl, FormGroup } from '@angular/forms';
2019-01-18 18:03:45 +01:00
import { MatDialog, MatSnackBar } from '@angular/material';
2018-11-27 15:13:56 +01:00
import { ActivatedRoute, Params, Router } from '@angular/router';
2018-10-05 17:00:54 +02:00
import { TranslateService } from '@ngx-translate/core';
import { Observable } from 'rxjs';
2018-11-27 18:33:17 +01:00
import { takeUntil } from 'rxjs/operators';
2019-01-18 18:03:45 +01:00
import { environment } from '../../../../environments/environment';
import { ValidationErrorModel } from '../../../common/forms/validation/error-model/validation-error-model';
2019-01-18 18:03:45 +01:00
import { BaseComponent } from '../../../core/common/base/base.component';
import { ProjectType } from '../../../core/common/enum/project-type';
import { ProjectListingModel } from '../../../core/model/project/project-listing';
2019-01-18 18:03:45 +01:00
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';
2017-12-14 18:13:28 +01:00
@Component({
2018-10-05 17:00:54 +02:00
selector: 'app-project-editor-component',
templateUrl: 'project-editor.component.html',
2018-11-27 18:33:17 +01:00
styleUrls: ['./project-editor.component.scss']
2017-12-14 18:13:28 +01:00
})
2018-11-27 18:33:17 +01:00
export class ProjectEditorComponent extends BaseComponent implements OnInit, IBreadCrumbComponent {
2017-12-14 18:13:28 +01:00
2018-10-05 17:00:54 +02:00
breadCrumbs: Observable<BreadcrumbItem[]> = Observable.of([]);
isNew = true;
2019-01-18 18:03:45 +01:00
project: ProjectEditorModel;
2018-10-05 17:00:54 +02:00
formGroup: FormGroup = null;
2018-11-27 15:13:56 +01:00
host = environment.Server;
2018-10-05 17:00:54 +02:00
editMode = false;
constructor(
private projectService: ProjectService,
private route: ActivatedRoute,
public snackBar: MatSnackBar,
public router: Router,
public language: TranslateService,
2019-01-18 18:03:45 +01:00
private dialog: MatDialog,
private projectFileUploadService: ProjectFileUploadService,
private uiNotificationService: UiNotificationService
2018-10-05 17:00:54 +02:00
) {
2018-11-27 18:33:17 +01:00
super();
2018-10-05 17:00:54 +02:00
}
ngOnInit() {
2018-11-27 18:33:17 +01:00
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)
2018-11-27 18:33:17 +01:00
.pipe(takeUntil(this._destroyed))
.subscribe(data => {
2019-01-18 18:03:45 +01:00
this.project = new ProjectEditorModel().fromModel(data);
2018-11-27 18:33:17 +01:00
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' },
]);
2019-01-18 18:03:45 +01:00
this.project = new ProjectEditorModel();
2018-11-27 18:33:17 +01:00
setTimeout(() => {
this.formGroup = this.project.buildForm();
2018-10-05 17:00:54 +02:00
});
2018-11-27 18:33:17 +01:00
}
});
2018-10-05 17:00:54 +02:00
}
formSubmit(): void {
this.touchAllFormFields(this.formGroup);
if (!this.isFormValid()) { return; }
this.onSubmit();
}
public isFormValid() {
return this.formGroup.valid;
}
onSubmit(): void {
2018-11-27 18:33:17 +01:00
this.projectService.createProject(this.formGroup.value)
.pipe(takeUntil(this._destroyed))
.subscribe(
complete => this.onCallbackSuccess(),
error => this.onCallbackError(error)
);
2018-10-05 17:00:54 +02:00
}
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);
2018-10-05 17:00:54 +02:00
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 => {
(<any>this.project.validationErrorModel)[item] = (<any>validationErrorModel)[item];
2018-10-05 17:00:54 +02:00
});
}
public cancel(): void {
this.router.navigate(['/projects']);
}
public delete(): void {
2019-01-18 18:03:45 +01:00
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)
2018-11-27 18:33:17 +01:00
.pipe(takeUntil(this._destroyed))
2019-01-18 18:03:45 +01:00
.subscribe(
complete => { this.onCallbackSuccess() },
error => this.onCallbackError(error)
);
}
});
2018-10-05 17:00:54 +02: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);
});
}
}
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() {
2019-01-18 18:03:45 +01:00
this.router.navigate(['plans/project/' + this.project.id], { queryParams: { projectLabel: this.project.label } });
2018-10-05 17:00:54 +02:00
}
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);
}
2019-01-18 18:03:45 +01:00
this.projectFileUploadService.uploadFile(formdata)
2018-11-27 18:33:17 +01:00
.pipe(takeUntil(this._destroyed))
.subscribe(files => this.formGroup.get('files').patchValue(files));
2018-10-05 17:00:54 +02:00
}
2018-05-28 11:50:42 +02:00
}