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 { IPropietario } from 'app/shared/model/propietario.model'; import { PropietarioService } from './propietario.service'; import { IUser, UserService } from 'app/core'; @Component({ selector: 'jhi-propietario-update', templateUrl: './propietario-update.component.html' }) export class PropietarioUpdateComponent implements OnInit { private _propietario: IPropietario; isSaving: boolean; users: IUser[]; constructor( private jhiAlertService: JhiAlertService, private propietarioService: PropietarioService, private userService: UserService, private route: ActivatedRoute ) {} ngOnInit() { this.isSaving = false; this.route.data.subscribe(({ propietario }) => { this.propietario = propietario.body ? propietario.body : propietario; }); 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.propietario.id !== undefined) { this.subscribeToSaveResponse(this.propietarioService.update(this.propietario)); } else { this.subscribeToSaveResponse(this.propietarioService.create(this.propietario)); } } private subscribeToSaveResponse(result: Observable>) { result.subscribe((res: HttpResponse) => this.onSaveSuccess(res.body), (res: HttpErrorResponse) => this.onSaveError()); } private onSaveSuccess(result: IPropietario) { 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 propietario() { return this._propietario; } set propietario(propietario: IPropietario) { this._propietario = propietario; } }