2019-09-23 10:17:03 +02:00
|
|
|
|
2019-12-11 15:51:03 +01:00
|
|
|
import { HttpErrorResponse } from '@angular/common/http';
|
2019-08-30 15:02:02 +02:00
|
|
|
import { Component, OnInit } from '@angular/core';
|
|
|
|
import { FormGroup } from '@angular/forms';
|
|
|
|
import { ActivatedRoute, Params, Router } from '@angular/router';
|
2019-12-11 15:51:03 +01:00
|
|
|
import { DmpModel } from '@app/core/model/dmp/dmp';
|
|
|
|
import { DmpService } from '@app/core/services/dmp/dmp.service';
|
|
|
|
import { SnackBarNotificationLevel, UiNotificationService } from '@app/core/services/notification/ui-notification-service';
|
|
|
|
import { FunderFormModel } from '@app/ui/dmp/editor/grant-tab/funder-form-model';
|
|
|
|
import { GrantTabModel } from '@app/ui/dmp/editor/grant-tab/grant-tab-model';
|
|
|
|
import { ProjectFormModel } from '@app/ui/dmp/editor/grant-tab/project-form-model';
|
|
|
|
import { DmpWizardEditorModel } from '@app/ui/dmp/wizard/dmp-wizard-editor.model';
|
|
|
|
import { BreadcrumbItem } from '@app/ui/misc/breadcrumb/definition/breadcrumb-item';
|
|
|
|
import { BaseComponent } from '@common/base/base.component';
|
|
|
|
import { ValidationErrorModel } from '@common/forms/validation/error-model/validation-error-model';
|
2019-08-30 15:02:02 +02:00
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
2019-12-11 15:51:03 +01:00
|
|
|
import { Observable, of as observableOf } from 'rxjs';
|
|
|
|
import { map, takeUntil } from 'rxjs/operators';
|
2019-12-13 12:15:12 +01:00
|
|
|
import { DmpStatus } from '@app/core/common/enum/dmp-status';
|
2020-06-26 17:57:04 +02:00
|
|
|
import { ExtraPropertiesFormModel } from '../editor/general-tab/extra-properties-form.model';
|
2019-12-11 15:51:03 +01:00
|
|
|
|
2019-08-30 15:02:02 +02:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-dmp-clone',
|
|
|
|
templateUrl: './dmp-clone.component.html',
|
|
|
|
styleUrls: ['./dmp-clone.component.scss']
|
|
|
|
})
|
|
|
|
export class DmpCloneComponent extends BaseComponent implements OnInit {
|
|
|
|
|
|
|
|
breadCrumbs: Observable<BreadcrumbItem[]>;
|
|
|
|
dmp: DmpWizardEditorModel;
|
|
|
|
formGroup: FormGroup;
|
|
|
|
itemId: string;
|
|
|
|
isFinalized: false;
|
|
|
|
isPublic: false;
|
|
|
|
selectedTab = 0;
|
|
|
|
parentDmpLabel: string;
|
2019-09-02 10:19:16 +02:00
|
|
|
isNewVersion: boolean = false;
|
2020-01-07 17:17:21 +01:00
|
|
|
isClone: boolean = true;
|
|
|
|
isNew: boolean = false;
|
2019-08-30 15:02:02 +02:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
private route: ActivatedRoute,
|
|
|
|
private router: Router,
|
|
|
|
private language: TranslateService,
|
|
|
|
private dmpService: DmpService,
|
|
|
|
private uiNotificationService: UiNotificationService
|
|
|
|
) {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
this.route.params
|
|
|
|
.pipe(takeUntil(this._destroyed))
|
|
|
|
.subscribe((params: Params) => {
|
|
|
|
this.itemId = params['id'];
|
2019-09-23 10:17:03 +02:00
|
|
|
this.dmpService.getSingle(this.itemId).pipe(map(data => data as DmpModel))
|
2019-08-30 15:02:02 +02:00
|
|
|
.pipe(takeUntil(this._destroyed))
|
|
|
|
.subscribe(data => {
|
|
|
|
this.dmp = new DmpWizardEditorModel();
|
|
|
|
this.dmp.grant = new GrantTabModel();
|
|
|
|
this.dmp.project = new ProjectFormModel();
|
|
|
|
this.dmp.funder = new FunderFormModel();
|
2020-06-26 17:57:04 +02:00
|
|
|
this.dmp.extraProperties = new ExtraPropertiesFormModel();
|
2019-08-30 15:02:02 +02:00
|
|
|
this.dmp.fromModel(data);
|
2019-12-13 12:15:12 +01:00
|
|
|
this.dmp.status = DmpStatus.Draft;
|
2019-08-30 15:02:02 +02:00
|
|
|
this.formGroup = this.dmp.buildForm();
|
|
|
|
|
|
|
|
this.parentDmpLabel = this.formGroup.get('label').value;
|
2019-09-02 10:19:16 +02:00
|
|
|
if (this.route.routeConfig.path.startsWith('new_version/')) {
|
|
|
|
this.formGroup.get('version').setValue(this.formGroup.get('version').value + 1);
|
|
|
|
this.formGroup.controls['label'].disable();
|
|
|
|
this.formGroup.controls['grant'].disable();
|
|
|
|
this.isNewVersion = true;
|
|
|
|
} else if (this.route.routeConfig.path.startsWith('clone/')) {
|
|
|
|
this.formGroup.get('label').setValue(this.dmp.label + " New");
|
|
|
|
this.isNewVersion = false;
|
|
|
|
}
|
|
|
|
|
2019-08-30 15:02:02 +02:00
|
|
|
const breadCrumbs = [];
|
|
|
|
breadCrumbs.push({ parentComponentName: null, label: this.language.instant('NAV-BAR.MY-DMPS'), url: "/plans" });
|
|
|
|
breadCrumbs.push({ parentComponentName: 'DmpListingComponent', label: this.dmp.label, url: '/plans/clone/' + this.dmp.id });
|
2019-09-23 10:17:03 +02:00
|
|
|
this.breadCrumbs = observableOf(breadCrumbs);
|
2019-08-30 15:02:02 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public cancel(id: String): void {
|
|
|
|
if (id != null) {
|
|
|
|
this.router.navigate(['/plans/overview/' + id]);
|
|
|
|
} else {
|
|
|
|
this.router.navigate(['/plans']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public isFormValid(): boolean {
|
|
|
|
return this.formGroup.valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
formSubmit(): void {
|
|
|
|
if (!this.isFormValid()) { return; }
|
2019-09-02 11:50:42 +02:00
|
|
|
if (this.isNewVersion) {
|
|
|
|
this.dmpService.newVersion(this.formGroup.getRawValue(), this.itemId)
|
|
|
|
.pipe(takeUntil(this._destroyed))
|
|
|
|
.subscribe(
|
|
|
|
complete => this.onCallbackSuccess(),
|
|
|
|
error => this.onCallbackErrorNewVersion(error)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.dmpService.clone(this.formGroup.getRawValue(), this.itemId)
|
|
|
|
.pipe(takeUntil(this._destroyed))
|
|
|
|
.subscribe(
|
|
|
|
complete => this.onCallbackSuccess(),
|
|
|
|
error => this.onCallbackError(error)
|
|
|
|
);
|
|
|
|
}
|
2019-08-30 15:02:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
onCallbackSuccess(): void {
|
|
|
|
this.uiNotificationService.snackBarNotification(this.language.instant('GENERAL.SNACK-BAR.SUCCESSFUL-UPDATE'), SnackBarNotificationLevel.Success);
|
|
|
|
this.router.navigate(['/plans']);
|
|
|
|
}
|
|
|
|
|
|
|
|
onCallbackError(error: any) {
|
|
|
|
this.setErrorModel(error.error);
|
|
|
|
}
|
|
|
|
|
2019-09-02 11:50:42 +02:00
|
|
|
onCallbackErrorNewVersion(errorResponse: HttpErrorResponse) {
|
|
|
|
this.uiNotificationService.snackBarNotification(errorResponse.error.message, SnackBarNotificationLevel.Error);
|
|
|
|
}
|
|
|
|
|
2019-08-30 15:02:02 +02:00
|
|
|
public setErrorModel(validationErrorModel: ValidationErrorModel) {
|
|
|
|
Object.keys(validationErrorModel).forEach(item => {
|
|
|
|
(<any>this.dmp.validationErrorModel)[item] = (<any>validationErrorModel)[item];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|