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 { IProducto } from 'app/shared/model/producto.model'; import { ProductoService } from './producto.service'; import { ICategoria } from 'app/shared/model/categoria.model'; import { CategoriaService } from 'app/entities/categoria'; import { IBar } from 'app/shared/model/bar.model'; import { BarService } from 'app/entities/bar'; @Component({ selector: 'jhi-producto-update', templateUrl: './producto-update.component.html' }) export class ProductoUpdateComponent implements OnInit { private _producto: IProducto; isSaving: boolean; categorias: ICategoria[]; bars: IBar[]; constructor( private jhiAlertService: JhiAlertService, private productoService: ProductoService, private categoriaService: CategoriaService, private barService: BarService, private route: ActivatedRoute ) {} ngOnInit() { this.isSaving = false; this.route.data.subscribe(({ producto }) => { this.producto = producto.body ? producto.body : producto; }); this.categoriaService.query().subscribe( (res: HttpResponse) => { this.categorias = res.body; }, (res: HttpErrorResponse) => this.onError(res.message) ); 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.producto.id !== undefined) { this.subscribeToSaveResponse(this.productoService.update(this.producto)); } else { this.subscribeToSaveResponse(this.productoService.create(this.producto)); } } private subscribeToSaveResponse(result: Observable>) { result.subscribe((res: HttpResponse) => this.onSaveSuccess(res.body), (res: HttpErrorResponse) => this.onSaveError()); } private onSaveSuccess(result: IProducto) { this.isSaving = false; this.previousState(); } private onSaveError() { this.isSaving = false; } private onError(errorMessage: string) { this.jhiAlertService.error(errorMessage, null, null); } trackCategoriaById(index: number, item: ICategoria) { return item.id; } trackBarById(index: number, item: IBar) { return item.id; } get producto() { return this._producto; } set producto(producto: IProducto) { this._producto = producto; } }