94 lines
3.4 KiB
TypeScript
94 lines
3.4 KiB
TypeScript
import { Component, OnInit } from '@angular/core';
|
|
import { FormGroup } from '@angular/forms';
|
|
import { MatSnackBar } from '@angular/material';
|
|
import { ActivatedRoute, Params, Router } from '@angular/router';
|
|
import { TranslateService } from '@ngx-translate/core';
|
|
import { Observable } from 'rxjs';
|
|
import { takeUntil } from 'rxjs/operators';
|
|
import { BaseComponent } from '../../core/common/base/base.component';
|
|
import { DataManagementPlanModel } from '../../models/data-managemnt-plans/DataManagementPlanModel';
|
|
import { BaseErrorModel } from '../../models/error/BaseErrorModel';
|
|
import { DataManagementPlanService } from '../../services/data-management-plan/data-management-plan.service';
|
|
import { BreadcrumbItem } from '../../shared/components/breadcrumb/definition/breadcrumb-item';
|
|
import { IBreadCrumbComponent } from '../../shared/components/breadcrumb/definition/IBreadCrumbComponent';
|
|
import { SnackBarNotificationComponent } from '../../shared/components/notificaiton/snack-bar-notification.component';
|
|
import { JsonSerializer } from '../../utilities/JsonSerializer';
|
|
|
|
@Component({
|
|
selector: 'app-dmp-wizard-component',
|
|
templateUrl: 'dmp-wizard.component.html',
|
|
styleUrls: ['./dmp-wizard.component.scss'],
|
|
})
|
|
export class DataManagementPlanWizardComponent extends BaseComponent implements OnInit, IBreadCrumbComponent {
|
|
breadCrumbs: Observable<BreadcrumbItem[]>;
|
|
constructor(
|
|
private dataManagementPlanService: DataManagementPlanService,
|
|
private language: TranslateService,
|
|
public snackBar: MatSnackBar,
|
|
public route: ActivatedRoute,
|
|
private router: Router
|
|
) { super(); }
|
|
|
|
public itemId: string;
|
|
public dataManagementPlan: DataManagementPlanModel;
|
|
public formGroup: FormGroup;
|
|
public isClone: boolean;
|
|
|
|
ngOnInit(): void {
|
|
|
|
this.route.params
|
|
.pipe(takeUntil(this._destroyed))
|
|
.subscribe((params: Params) => {
|
|
this.itemId = params['id'];
|
|
this.dataManagementPlanService.getSingle(this.itemId).map(data => data as DataManagementPlanModel)
|
|
.pipe(takeUntil(this._destroyed))
|
|
.subscribe(data => {
|
|
this.dataManagementPlan = JsonSerializer.fromJSONObject(data, DataManagementPlanModel);
|
|
this.isClone = this.route.snapshot.data.clone;
|
|
if (this.isClone === false) { this.dataManagementPlan.version = this.dataManagementPlan.version + 1; }
|
|
this.formGroup = this.dataManagementPlan.buildForm();
|
|
});
|
|
});
|
|
}
|
|
|
|
|
|
submit() {
|
|
if (this.isClone) {
|
|
this.dataManagementPlanService.clone(this.formGroup.getRawValue(), this.itemId)
|
|
.pipe(takeUntil(this._destroyed))
|
|
.subscribe(
|
|
complete => this.onCallbackSuccess(),
|
|
error => this.onCallbackError(error)
|
|
);
|
|
} else {
|
|
this.dataManagementPlanService.newVersion(this.formGroup.getRawValue(), this.itemId)
|
|
.pipe(takeUntil(this._destroyed))
|
|
.subscribe(
|
|
complete => this.onCallbackSuccess(),
|
|
error => this.onCallbackError(error)
|
|
);
|
|
}
|
|
}
|
|
|
|
onCallbackSuccess(): void {
|
|
this.snackBar.openFromComponent(SnackBarNotificationComponent, {
|
|
data: { message: 'GENERAL.SNACK-BAR.SUCCESSFUL-UPDATE', language: this.language },
|
|
duration: 3000,
|
|
});
|
|
this.router.navigate(['/dmps']);
|
|
}
|
|
|
|
onCallbackError(error: any) {
|
|
this.setErrorModel(error.error);
|
|
//this.validateAllFormFields(this.formGroup);
|
|
}
|
|
|
|
public setErrorModel(errorModel: BaseErrorModel) {
|
|
Object.keys(errorModel).forEach(item => {
|
|
// (<any>this.dataManagementPlan.errorModel)[item] = (<any>errorModel)[item];
|
|
});
|
|
}
|
|
|
|
|
|
}
|