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