import { Component, OnInit } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; import { HttpResponse, HttpErrorResponse } from '@angular/common/http'; import { Observable } from 'rxjs/Observable'; import { JhiAlertService } from 'ng-jhipster'; import { IArticulo } from 'app/shared/model/articulo.model'; import { ArticuloService } from './articulo.service'; import { IBar } from 'app/shared/model/bar.model'; import { BarService } from 'app/entities/bar'; @Component({ selector: 'jhi-articulo-update', templateUrl: './articulo-update.component.html' }) export class ArticuloUpdateComponent implements OnInit { private _articulo: IArticulo; isSaving: boolean; bars: IBar[]; constructor( private jhiAlertService: JhiAlertService, private articuloService: ArticuloService, private barService: BarService, private route: ActivatedRoute ) {} ngOnInit() { this.isSaving = false; this.route.data.subscribe(({ articulo }) => { this.articulo = articulo.body ? articulo.body : articulo; }); this.barService.query().subscribe( (res: HttpResponse) => { this.bars = res.body; }, (res: HttpErrorResponse) => this.onError(res.message) ); } previousState() { window.history.back(); } save() { this.isSaving = true; if (this.articulo.id !== undefined) { this.subscribeToSaveResponse(this.articuloService.update(this.articulo)); } else { this.subscribeToSaveResponse(this.articuloService.create(this.articulo)); } } private subscribeToSaveResponse(result: Observable>) { result.subscribe((res: HttpResponse) => this.onSaveSuccess(res.body), (res: HttpErrorResponse) => this.onSaveError()); } private onSaveSuccess(result: IArticulo) { this.isSaving = false; this.previousState(); } private onSaveError() { this.isSaving = false; } private onError(errorMessage: string) { this.jhiAlertService.error(errorMessage, null, null); } trackBarById(index: number, item: IBar) { return item.id; } get articulo() { return this._articulo; } set articulo(articulo: IArticulo) { this._articulo = articulo; } }