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 { ICategoria } from 'app/shared/model/categoria.model'; import { CategoriaService } from './categoria.service'; import { IBar } from 'app/shared/model/bar.model'; import { BarService } from 'app/entities/bar'; @Component({ selector: 'jhi-categoria-update', templateUrl: './categoria-update.component.html' }) export class CategoriaUpdateComponent implements OnInit { private _categoria: ICategoria; isSaving: boolean; bars: IBar[]; constructor( private jhiAlertService: JhiAlertService, private categoriaService: CategoriaService, private barService: BarService, private route: ActivatedRoute ) {} ngOnInit() { this.isSaving = false; this.route.data.subscribe(({ categoria }) => { this.categoria = categoria.body ? categoria.body : categoria; }); 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.categoria.id !== undefined) { this.subscribeToSaveResponse(this.categoriaService.update(this.categoria)); } else { this.subscribeToSaveResponse(this.categoriaService.create(this.categoria)); } } private subscribeToSaveResponse(result: Observable>) { result.subscribe((res: HttpResponse) => this.onSaveSuccess(res.body), (res: HttpErrorResponse) => this.onSaveError()); } private onSaveSuccess(result: ICategoria) { 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 categoria() { return this._categoria; } set categoria(categoria: ICategoria) { this._categoria = categoria; } }