2019-09-23 10:17:03 +02:00
|
|
|
|
2018-11-27 18:33:17 +01:00
|
|
|
import { Component, OnInit } from '@angular/core';
|
2019-12-13 10:53:43 +01:00
|
|
|
import { FormGroup } from '@angular/forms';
|
2019-09-23 10:17:03 +02:00
|
|
|
import { MatDialog } from '@angular/material/dialog';
|
|
|
|
import { MatSnackBar } from '@angular/material/snack-bar';
|
2018-11-27 15:13:56 +01:00
|
|
|
import { ActivatedRoute, Params, Router } from '@angular/router';
|
2019-12-11 15:51:03 +01:00
|
|
|
import { GrantType } from '@app/core/common/enum/grant-type';
|
|
|
|
import { GrantListingModel } from '@app/core/model/grant/grant-listing';
|
|
|
|
import { GrantFileUploadService } from '@app/core/services/grant/grant-file-upload.service';
|
|
|
|
import { GrantService } from '@app/core/services/grant/grant.service';
|
|
|
|
import { SnackBarNotificationLevel, UiNotificationService } from '@app/core/services/notification/ui-notification-service';
|
|
|
|
import { GrantEditorModel } from '@app/ui/grant/editor/grant-editor.model';
|
|
|
|
import { BreadcrumbItem } from '@app/ui/misc/breadcrumb/definition/breadcrumb-item';
|
|
|
|
import { IBreadCrumbComponent } from '@app/ui/misc/breadcrumb/definition/IBreadCrumbComponent';
|
|
|
|
import { BaseComponent } from '@common/base/base.component';
|
2019-12-13 10:53:43 +01:00
|
|
|
import { FormService } from '@common/forms/form-service';
|
2019-12-11 15:51:03 +01:00
|
|
|
import { ValidationErrorModel } from '@common/forms/validation/error-model/validation-error-model';
|
2019-12-13 10:53:43 +01:00
|
|
|
import { ConfirmationDialogComponent } from '@common/modules/confirmation-dialog/confirmation-dialog.component';
|
2018-10-05 17:00:54 +02:00
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
2019-12-11 15:51:03 +01:00
|
|
|
import { environment } from 'environments/environment';
|
|
|
|
import { Observable, of as observableOf } from 'rxjs';
|
|
|
|
import { map, takeUntil } from 'rxjs/operators';
|
|
|
|
|
2017-12-14 18:13:28 +01:00
|
|
|
|
|
|
|
@Component({
|
2019-08-01 09:54:40 +02:00
|
|
|
selector: 'app-grant-editor-component',
|
|
|
|
templateUrl: 'grant-editor.component.html',
|
|
|
|
styleUrls: ['./grant-editor.component.scss']
|
2017-12-14 18:13:28 +01:00
|
|
|
})
|
2019-08-01 09:54:40 +02:00
|
|
|
export class GrantEditorComponent extends BaseComponent implements OnInit, IBreadCrumbComponent {
|
2017-12-14 18:13:28 +01:00
|
|
|
|
2019-09-23 10:17:03 +02:00
|
|
|
breadCrumbs: Observable<BreadcrumbItem[]> = observableOf([]);
|
2018-10-05 17:00:54 +02:00
|
|
|
isNew = true;
|
2019-08-01 09:54:40 +02:00
|
|
|
grant: GrantEditorModel;
|
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;
|
2019-02-12 12:48:24 +01:00
|
|
|
sizeError = false;
|
|
|
|
maxFileSize: number = 1048576;
|
2018-10-05 17:00:54 +02:00
|
|
|
constructor(
|
2019-08-01 09:54:40 +02:00
|
|
|
private grantService: GrantService,
|
2018-10-05 17:00:54 +02:00
|
|
|
private route: ActivatedRoute,
|
|
|
|
public snackBar: MatSnackBar,
|
|
|
|
public router: Router,
|
|
|
|
public language: TranslateService,
|
2019-01-18 18:03:45 +01:00
|
|
|
private dialog: MatDialog,
|
2019-08-01 09:54:40 +02:00
|
|
|
private grantFileUploadService: GrantFileUploadService,
|
2019-12-13 10:53:43 +01:00
|
|
|
private uiNotificationService: UiNotificationService,
|
|
|
|
private formService: FormService
|
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;
|
2019-09-23 10:17:03 +02:00
|
|
|
this.grantService.getSingle(itemId).pipe(map(data => data as GrantListingModel))
|
2018-11-27 18:33:17 +01:00
|
|
|
.pipe(takeUntil(this._destroyed))
|
|
|
|
.subscribe(data => {
|
2019-08-01 09:54:40 +02:00
|
|
|
this.grant = new GrantEditorModel().fromModel(data);
|
|
|
|
this.formGroup = this.grant.buildForm(null, this.grant.type === GrantType.External || !this.editMode);
|
2019-06-07 13:03:10 +02:00
|
|
|
const breadCrumbs = [];
|
|
|
|
breadCrumbs.push({
|
|
|
|
parentComponentName: null,
|
2019-08-01 09:54:40 +02:00
|
|
|
label: this.language.instant('NAV-BAR.GRANTS').toUpperCase(),
|
2019-09-16 17:46:19 +02:00
|
|
|
url: '/grants'
|
2019-06-07 13:03:10 +02:00
|
|
|
});
|
|
|
|
breadCrumbs.push({
|
2019-08-01 09:54:40 +02:00
|
|
|
parentComponentName: 'GrantListingComponent',
|
|
|
|
label: this.grant.label,
|
|
|
|
url: '/grants/edit/' + this.grant.id
|
2019-06-07 13:03:10 +02:00
|
|
|
});
|
2019-09-23 10:17:03 +02:00
|
|
|
this.breadCrumbs = observableOf(breadCrumbs);
|
2018-11-27 18:33:17 +01:00
|
|
|
});
|
|
|
|
} else {
|
2019-09-16 17:46:19 +02:00
|
|
|
this.language.get('QUICKWIZARD.CREATE-ADD.CREATE.QUICKWIZARD_CREATE.ACTIONS.CREATE-NEW-GRANT').pipe(takeUntil(this._destroyed)).subscribe(x => {
|
2019-09-23 10:17:03 +02:00
|
|
|
this.breadCrumbs = observableOf([
|
2019-09-16 17:46:19 +02:00
|
|
|
{
|
|
|
|
parentComponentName: null,
|
|
|
|
label: this.language.instant('NAV-BAR.GRANTS').toUpperCase(),
|
|
|
|
url: '/grants'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
parentComponentName: 'GrantListingComponent',
|
|
|
|
label: x,
|
|
|
|
url: '/grants/new/'
|
|
|
|
}]
|
|
|
|
);
|
2019-06-07 13:03:10 +02:00
|
|
|
});
|
|
|
|
|
2019-08-01 09:54:40 +02:00
|
|
|
this.grant = new GrantEditorModel();
|
2018-11-27 18:33:17 +01:00
|
|
|
setTimeout(() => {
|
2019-08-01 09:54:40 +02:00
|
|
|
this.formGroup = this.grant.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 {
|
2019-12-13 10:53:43 +01:00
|
|
|
this.formService.touchAllFormFields(this.formGroup);
|
2018-10-05 17:00:54 +02:00
|
|
|
if (!this.isFormValid()) { return; }
|
|
|
|
this.onSubmit();
|
|
|
|
}
|
|
|
|
|
|
|
|
public isFormValid() {
|
|
|
|
return this.formGroup.valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
onSubmit(): void {
|
2019-08-01 09:54:40 +02:00
|
|
|
this.grantService.createGrant(this.formGroup.value)
|
2018-11-27 18:33:17 +01:00
|
|
|
.pipe(takeUntil(this._destroyed))
|
|
|
|
.subscribe(
|
2019-06-07 13:03:10 +02:00
|
|
|
complete => this.onCallbackSuccess(),
|
|
|
|
error => this.onCallbackError(error)
|
2018-11-27 18:33:17 +01:00
|
|
|
);
|
2018-10-05 17:00:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
onCallbackSuccess(): void {
|
2019-01-24 11:46:29 +01:00
|
|
|
this.uiNotificationService.snackBarNotification(this.isNew ? this.language.instant('GENERAL.SNACK-BAR.SUCCESSFUL-CREATION') : this.language.instant('GENERAL.SNACK-BAR.SUCCESSFUL-UPDATE'), SnackBarNotificationLevel.Success);
|
2019-08-01 09:54:40 +02:00
|
|
|
this.router.navigate(['/grants']);
|
2018-10-05 17:00:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
onCallbackError(errorResponse: any) {
|
|
|
|
this.setErrorModel(errorResponse.error.payload);
|
2019-12-13 10:53:43 +01:00
|
|
|
this.formService.validateAllFormFields(this.formGroup);
|
2018-10-05 17:00:54 +02:00
|
|
|
}
|
|
|
|
|
2019-01-21 12:14:20 +01:00
|
|
|
public setErrorModel(validationErrorModel: ValidationErrorModel) {
|
|
|
|
Object.keys(validationErrorModel).forEach(item => {
|
2019-08-01 09:54:40 +02:00
|
|
|
(<any>this.grant.validationErrorModel)[item] = (<any>validationErrorModel)[item];
|
2018-10-05 17:00:54 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public cancel(): void {
|
2019-08-01 09:54:40 +02:00
|
|
|
this.router.navigate(['/grants']);
|
2018-10-05 17:00:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public delete(): void {
|
2019-01-18 18:03:45 +01:00
|
|
|
const dialogRef = this.dialog.open(ConfirmationDialogComponent, {
|
|
|
|
maxWidth: '300px',
|
2019-09-27 10:09:29 +02:00
|
|
|
restoreFocus: false,
|
2019-01-18 18:03:45 +01:00
|
|
|
data: {
|
|
|
|
message: this.language.instant('GENERAL.CONFIRMATION-DIALOG.DELETE-ITEM'),
|
|
|
|
confirmButton: this.language.instant('GENERAL.CONFIRMATION-DIALOG.ACTIONS.CONFIRM'),
|
2019-06-26 11:24:06 +02:00
|
|
|
cancelButton: this.language.instant('GENERAL.CONFIRMATION-DIALOG.ACTIONS.CANCEL'),
|
|
|
|
isDeleteConfirmation: true
|
2019-01-18 18:03:45 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
dialogRef.afterClosed().pipe(takeUntil(this._destroyed)).subscribe(result => {
|
|
|
|
if (result) {
|
2019-08-01 09:54:40 +02:00
|
|
|
this.grantService.delete(this.grant.id)
|
2018-11-27 18:33:17 +01:00
|
|
|
.pipe(takeUntil(this._destroyed))
|
2019-01-18 18:03:45 +01:00
|
|
|
.subscribe(
|
2019-06-07 13:03:10 +02:00
|
|
|
complete => { this.onCallbackSuccess() },
|
|
|
|
error => this.onCallbackError(error)
|
2019-01-18 18:03:45 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
2018-10-05 17:00:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public enableForm() {
|
2019-08-01 09:54:40 +02:00
|
|
|
if (!this.isExternalGrant()) {
|
2018-10-05 17:00:54 +02:00
|
|
|
this.editMode = true;
|
|
|
|
this.formGroup.enable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public disableForm() {
|
|
|
|
this.editMode = false;
|
|
|
|
this.formGroup.disable();
|
|
|
|
}
|
|
|
|
|
2019-05-23 11:40:24 +02:00
|
|
|
public imgEnable(): boolean {
|
|
|
|
if (this.isNew || this.editMode) {
|
2019-03-06 15:59:22 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-08-01 09:54:40 +02:00
|
|
|
public goToGrantDmps() {
|
|
|
|
this.router.navigate(['plans/grant/' + this.grant.id], { queryParams: { grantLabel: this.grant.label } });
|
2018-10-05 17:00:54 +02:00
|
|
|
}
|
|
|
|
|
2019-08-01 09:54:40 +02:00
|
|
|
public isExternalGrant() {
|
|
|
|
return this.grant.type === GrantType.External;
|
2018-10-05 17:00:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public previewImage(event): void {
|
|
|
|
const fileList: FileList | File = event.target.files;
|
2019-02-12 12:48:24 +01:00
|
|
|
const size: number = event.target.files[0].size; // Get file size.
|
|
|
|
this.sizeError = size > this.maxFileSize; // Checks if file size is valid.
|
2018-10-05 17:00:54 +02:00
|
|
|
const formdata: FormData = new FormData();
|
2019-05-23 11:40:24 +02:00
|
|
|
if (!this.sizeError) {
|
2019-02-12 12:48:24 +01:00
|
|
|
if (fileList instanceof FileList) {
|
|
|
|
for (let i = 0; i < fileList.length; i++) {
|
|
|
|
formdata.append('file', fileList[i]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
formdata.append('file', fileList);
|
2018-10-05 17:00:54 +02:00
|
|
|
}
|
2019-08-01 09:54:40 +02:00
|
|
|
this.grantFileUploadService.uploadFile(formdata)
|
2019-05-23 11:40:24 +02:00
|
|
|
.pipe(takeUntil(this._destroyed))
|
|
|
|
.subscribe(files => this.formGroup.get('files').patchValue(files));
|
2019-02-12 12:48:24 +01:00
|
|
|
}
|
2018-10-05 17:00:54 +02:00
|
|
|
}
|
2018-05-28 11:50:42 +02:00
|
|
|
}
|