import { Component, OnInit } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; import { HttpResponse, HttpErrorResponse } from '@angular/common/http'; import { Observable } from 'rxjs/Observable'; import * as moment from 'moment'; import { DATE_TIME_FORMAT } from 'app/shared/constants/input.constants'; import { JhiAlertService } from 'ng-jhipster'; import { IComanda } from 'app/shared/model/comanda.model'; import { ComandaService } from './comanda.service'; import { IMesa } from 'app/shared/model/mesa.model'; import { MesaService } from 'app/entities/mesa'; @Component({ selector: 'jhi-comanda-update', templateUrl: './comanda-update.component.html' }) export class ComandaUpdateComponent implements OnInit { private _comanda: IComanda; isSaving: boolean; mesas: IMesa[]; datePedido: string; dateServido: string; constructor( private jhiAlertService: JhiAlertService, private comandaService: ComandaService, private mesaService: MesaService, private route: ActivatedRoute ) {} ngOnInit() { this.isSaving = false; this.route.data.subscribe(({ comanda }) => { this.comanda = comanda.body ? comanda.body : comanda; }); this.mesaService.query({ filter: 'comanda-is-null' }).subscribe( (res: HttpResponse) => { if (!this.comanda.mesaId) { this.mesas = res.body; } else { this.mesaService.find(this.comanda.mesaId).subscribe( (subRes: HttpResponse) => { this.mesas = [subRes.body].concat(res.body); }, (subRes: HttpErrorResponse) => this.onError(subRes.message) ); } }, (res: HttpErrorResponse) => this.onError(res.message) ); } previousState() { window.history.back(); } save() { this.isSaving = true; this.comanda.datePedido = moment(this.datePedido, DATE_TIME_FORMAT); this.comanda.dateServido = moment(this.dateServido, DATE_TIME_FORMAT); if (this.comanda.id !== undefined) { this.subscribeToSaveResponse(this.comandaService.update(this.comanda)); } else { this.subscribeToSaveResponse(this.comandaService.create(this.comanda)); } } private subscribeToSaveResponse(result: Observable>) { result.subscribe((res: HttpResponse) => this.onSaveSuccess(res.body), (res: HttpErrorResponse) => this.onSaveError()); } private onSaveSuccess(result: IComanda) { this.isSaving = false; this.previousState(); } private onSaveError() { this.isSaving = false; } private onError(errorMessage: string) { this.jhiAlertService.error(errorMessage, null, null); } trackMesaById(index: number, item: IMesa) { return item.id; } get comanda() { return this._comanda; } set comanda(comanda: IComanda) { this._comanda = comanda; this.datePedido = moment(comanda.datePedido).format(); this.dateServido = moment(comanda.dateServido).format(); } }