2019-09-23 10:17:03 +02:00
|
|
|
|
2019-03-05 12:21:04 +01:00
|
|
|
import { Component, Input, OnInit } from '@angular/core';
|
|
|
|
import { AbstractControl, FormArray, FormControl, FormGroup } from '@angular/forms';
|
2019-09-23 10:17:03 +02:00
|
|
|
import { MatSnackBar } from '@angular/material/snack-bar';
|
2019-12-11 15:51:03 +01:00
|
|
|
import { Router } from '@angular/router';
|
|
|
|
import { GrantCriteria } from '@app/core/query/grant/grant-criteria';
|
|
|
|
import { RequestItem } from '@app/core/query/request-item';
|
|
|
|
import { GrantService } from '@app/core/services/grant/grant.service';
|
|
|
|
import { SnackBarNotificationLevel, UiNotificationService } from '@app/core/services/notification/ui-notification-service';
|
|
|
|
import { SingleAutoCompleteConfiguration } from '@app/library/auto-complete/single/single-auto-complete-configuration';
|
|
|
|
import { BreadcrumbItem } from '@app/ui/misc/breadcrumb/definition/breadcrumb-item';
|
|
|
|
import { IBreadCrumbComponent } from '@app/ui/misc/breadcrumb/definition/IBreadCrumbComponent';
|
|
|
|
import { GrantEditorWizardModel } from '@app/ui/quick-wizard/grant-editor/grant-editor-wizard-model';
|
|
|
|
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-03-01 16:16:21 +01:00
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
2019-12-11 15:51:03 +01:00
|
|
|
import { Observable, of as observableOf } from 'rxjs';
|
2019-12-13 10:53:43 +01:00
|
|
|
import { takeUntil } from 'rxjs/operators';
|
2019-12-13 16:05:19 +01:00
|
|
|
import { strict } from 'assert';
|
2019-03-01 16:16:21 +01:00
|
|
|
|
|
|
|
@Component({
|
2019-08-01 09:54:40 +02:00
|
|
|
selector: 'app-quick-wizard-grant-editor-component',
|
|
|
|
templateUrl: 'grant-editor-wizard.component.html',
|
|
|
|
styleUrls: ['./grant-editor-wizard.component.scss']
|
2019-03-01 16:16:21 +01:00
|
|
|
})
|
2019-08-01 09:54:40 +02:00
|
|
|
export class GrantEditorWizardComponent extends BaseComponent implements OnInit, IBreadCrumbComponent {
|
2019-09-23 10:17:03 +02:00
|
|
|
breadCrumbs: Observable<BreadcrumbItem[]> = observableOf([]);
|
2019-05-23 11:40:24 +02:00
|
|
|
|
|
|
|
isNew = false;
|
2019-08-01 09:54:40 +02:00
|
|
|
grant: GrantEditorWizardModel;
|
2019-08-28 15:53:17 +02:00
|
|
|
@Input() grantformGroup: FormGroup;
|
|
|
|
@Input() funderFormGroup: FormGroup;
|
2019-05-23 11:40:24 +02:00
|
|
|
private uiNotificationService: UiNotificationService
|
|
|
|
|
2019-08-01 09:54:40 +02:00
|
|
|
grantAutoCompleteConfiguration: SingleAutoCompleteConfiguration;
|
2019-05-23 11:40:24 +02:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
public snackBar: MatSnackBar,
|
|
|
|
public router: Router,
|
|
|
|
public language: TranslateService,
|
2019-12-13 10:53:43 +01:00
|
|
|
private grantService: GrantService,
|
|
|
|
private formService: FormService
|
2019-05-23 11:40:24 +02:00
|
|
|
) {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
2019-12-13 16:05:19 +01:00
|
|
|
getGrantIdText(item) {
|
|
|
|
if (item.reference != null && typeof item.reference == 'string') {
|
|
|
|
const parts = (item.reference as String).split('::');
|
|
|
|
return parts.length > 1 ? ' (' + parts[parts.length - 1] + ')' : '';
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2019-05-23 11:40:24 +02:00
|
|
|
ngOnInit() {
|
2019-09-23 10:17:03 +02:00
|
|
|
this.breadCrumbs = observableOf([{
|
2019-05-23 11:40:24 +02:00
|
|
|
parentComponentName: 'QuickCreate',
|
2019-08-01 09:54:40 +02:00
|
|
|
label: this.language.instant('NAV-BAR.GRANT'),
|
|
|
|
url: '/quick-wizard/grant'
|
2019-05-23 11:40:24 +02:00
|
|
|
}]);
|
|
|
|
|
2019-08-01 09:54:40 +02:00
|
|
|
const grantRequestItem: RequestItem<GrantCriteria> = new RequestItem();
|
|
|
|
grantRequestItem.criteria = new GrantCriteria();
|
2019-05-23 11:40:24 +02:00
|
|
|
|
2019-08-01 09:54:40 +02:00
|
|
|
this.grantAutoCompleteConfiguration = {
|
|
|
|
filterFn: this.searchGrant.bind(this),
|
|
|
|
initialItems: (extraData) => this.searchGrant(''),
|
2019-12-13 16:05:19 +01:00
|
|
|
displayFn: (item) => item['label'] + this.getGrantIdText(item),
|
|
|
|
titleFn: (item) => item['label'] + this.getGrantIdText(item),
|
2020-01-15 16:43:41 +01:00
|
|
|
subtitleFn: (item) => item['source'] ? this.language.instant('TYPES.EXTERNAL-DATASET-TYPE.SOURCE:') + item['source'] : (item['key'] ? this.language.instant('TYPES.EXTERNAL-DATASET-TYPE.SOURCE:') + item['key'] : this.language.instant('TYPES.EXTERNAL-DATASET-TYPE.NO-SOURCE'))
|
2019-05-23 11:40:24 +02:00
|
|
|
};
|
|
|
|
|
2019-12-13 10:53:43 +01:00
|
|
|
if (this.funderFormGroup && this.funderFormGroup.valid) {
|
2019-10-04 18:11:52 +02:00
|
|
|
this.grantformGroup.get('existGrant').enable();
|
2019-11-12 11:06:37 +01:00
|
|
|
this.grantformGroup.get('label').disable();
|
|
|
|
this.grantformGroup.get('description').disable();
|
2019-10-04 18:11:52 +02:00
|
|
|
} else {
|
|
|
|
this.grantformGroup.get('existGrant').disable();
|
|
|
|
}
|
2019-05-23 11:40:24 +02:00
|
|
|
|
2020-01-15 16:45:23 +01:00
|
|
|
this.funderFormGroup.statusChanges
|
|
|
|
.pipe(takeUntil(this._destroyed))
|
|
|
|
.subscribe(x => {
|
|
|
|
this.grantformGroup.reset();
|
|
|
|
if (x == 'INVALID') {
|
|
|
|
this.grantformGroup.get('existGrant').reset();
|
2019-12-13 10:53:43 +01:00
|
|
|
this.grantformGroup.get('label').reset();
|
|
|
|
this.grantformGroup.get('description').reset();
|
2020-01-15 16:45:23 +01:00
|
|
|
this.grantformGroup.get('existGrant').disable();
|
2019-12-13 10:53:43 +01:00
|
|
|
this.grantformGroup.get('label').disable();
|
|
|
|
this.grantformGroup.get('description').disable();
|
2020-01-15 16:45:23 +01:00
|
|
|
} else if (x == 'VALID') {
|
|
|
|
if (!this.isNew) {
|
|
|
|
this.grantformGroup.get('label').reset();
|
|
|
|
this.grantformGroup.get('description').reset();
|
|
|
|
this.grantformGroup.get('label').disable();
|
|
|
|
this.grantformGroup.get('description').disable();
|
|
|
|
this.grantformGroup.get('existGrant').enable();
|
|
|
|
|
|
|
|
}
|
2019-12-13 10:53:43 +01:00
|
|
|
}
|
2020-01-15 16:45:23 +01:00
|
|
|
})
|
2019-05-23 11:40:24 +02:00
|
|
|
}
|
|
|
|
|
2019-10-04 18:11:52 +02:00
|
|
|
isFunderFormInvalid() {
|
2019-12-13 10:53:43 +01:00
|
|
|
return !this.funderFormGroup.valid;
|
2019-05-23 11:40:24 +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);
|
2019-08-01 09:54:40 +02:00
|
|
|
this.router.navigate(['/grant']);
|
2019-05-23 11:40:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
onCallbackError(errorResponse: any) {
|
|
|
|
this.setErrorModel(errorResponse.error.payload);
|
2019-12-13 10:53:43 +01:00
|
|
|
this.formService.validateAllFormFields(this.grantformGroup);
|
2019-05-23 11:40:24 +02: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];
|
2019-05-23 11:40:24 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-08-01 09:54:40 +02:00
|
|
|
searchGrant(query: string) {
|
|
|
|
const grantRequestItem: RequestItem<GrantCriteria> = new RequestItem();
|
|
|
|
grantRequestItem.criteria = new GrantCriteria();
|
|
|
|
grantRequestItem.criteria.like = query;
|
2019-08-28 15:53:17 +02:00
|
|
|
if (this.funderFormGroup.get('existFunder').value) {
|
|
|
|
grantRequestItem.criteria.funderReference = this.funderFormGroup.controls['existFunder'].value.reference;
|
|
|
|
}
|
2019-08-01 09:54:40 +02:00
|
|
|
return this.grantService.getWithExternal(grantRequestItem);
|
2019-05-23 11:40:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
create() {
|
|
|
|
this.isNew = !this.isNew;
|
|
|
|
if (this.isNew) {
|
2019-08-28 15:53:17 +02:00
|
|
|
this.grantformGroup.get('existGrant').reset();
|
2019-12-13 10:53:43 +01:00
|
|
|
this.grantformGroup.get('existGrant').disable();
|
2019-08-28 15:53:17 +02:00
|
|
|
this.grantformGroup.get('label').enable();
|
|
|
|
this.grantformGroup.get('description').enable();
|
2019-05-23 11:40:24 +02:00
|
|
|
} else {
|
2019-08-28 15:53:17 +02:00
|
|
|
this.grantformGroup.get('label').reset();
|
2019-12-13 10:53:43 +01:00
|
|
|
this.grantformGroup.get('label').disable();
|
2019-08-28 15:53:17 +02:00
|
|
|
this.grantformGroup.get('description').reset();
|
2019-12-13 10:53:43 +01:00
|
|
|
this.grantformGroup.get('description').disable();
|
|
|
|
this.grantformGroup.get('existGrant').enable();
|
2019-05-23 11:40:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|