argos/dmp-frontend/src/app/ui/dmp/clone/dmp-clone.component.ts

106 lines
3.6 KiB
TypeScript
Raw Normal View History

import { Component, OnInit } from '@angular/core';
import { DmpWizardEditorModel } from '../wizard/dmp-wizard-editor.model';
import { FormGroup } from '@angular/forms';
import { takeUntil } from 'rxjs/operators';
import { BaseComponent } from '../../../core/common/base/base.component';
import { ActivatedRoute, Params, Router } from '@angular/router';
import { DmpModel } from '../../../core/model/dmp/dmp';
import { TranslateService } from '@ngx-translate/core';
import { DmpService } from '../../../core/services/dmp/dmp.service';
import { Observable } from 'rxjs';
import { BreadcrumbItem } from '../../misc/breadcrumb/definition/breadcrumb-item';
import { ValidationErrorModel } from '../../../common/forms/validation/error-model/validation-error-model';
import { UiNotificationService, SnackBarNotificationLevel } from '../../../core/services/notification/ui-notification-service';
import { FunderFormModel } from '../editor/grant-tab/funder-form-model';
import { ProjectFormModel } from '../editor/grant-tab/project-form-model';
import { GrantTabModel } from '../editor/grant-tab/grant-tab-model';
@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;
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'];
this.dmpService.getSingle(this.itemId).map(data => data as DmpModel)
.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();
this.dmp.fromModel(data);
this.formGroup = this.dmp.buildForm();
this.parentDmpLabel = this.formGroup.get('label').value;
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 });
this.breadCrumbs = Observable.of(breadCrumbs);
});
});
}
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; }
this.dmpService.clone(this.formGroup.getRawValue(), this.itemId)
.pipe(takeUntil(this._destroyed))
.subscribe(
complete => this.onCallbackSuccess(),
error => this.onCallbackError(error)
);
}
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);
}
public setErrorModel(validationErrorModel: ValidationErrorModel) {
Object.keys(validationErrorModel).forEach(item => {
(<any>this.dmp.validationErrorModel)[item] = (<any>validationErrorModel)[item];
});
}
}