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 { IBar } from 'app/shared/model/bar.model'; import { BarService } from './bar.service'; import { IUser, UserService } from 'app/core'; @Component({ selector: 'jhi-bar-update', templateUrl: './bar-update.component.html' }) export class BarUpdateComponent implements OnInit { private _bar: IBar; isSaving: boolean; users: IUser[]; constructor( private jhiAlertService: JhiAlertService, private barService: BarService, private userService: UserService, private route: ActivatedRoute ) {} ngOnInit() { this.isSaving = false; this.route.data.subscribe(({ bar }) => { this.bar = bar.body ? bar.body : bar; }); this.userService.query().subscribe( (res: HttpResponse) => { this.users = res.body; }, (res: HttpErrorResponse) => this.onError(res.message) ); } previousState() { window.history.back(); } save() { this.isSaving = true; if (this.bar.id !== undefined) { this.subscribeToSaveResponse(this.barService.update(this.bar)); } else { this.subscribeToSaveResponse(this.barService.create(this.bar)); } } private subscribeToSaveResponse(result: Observable>) { result.subscribe((res: HttpResponse) => this.onSaveSuccess(res.body), (res: HttpErrorResponse) => this.onSaveError()); } private onSaveSuccess(result: IBar) { this.isSaving = false; this.previousState(); } private onSaveError() { this.isSaving = false; } private onError(errorMessage: string) { this.jhiAlertService.error(errorMessage, null, null); } trackUserById(index: number, item: IUser) { return item.id; } get bar() { return this._bar; } set bar(bar: IBar) { this._bar = bar; } }