comanda.component.ts 1.56 KB
import { Component, OnInit } from '@angular/core';
import { ComandaService } from 'src/app/services/comanda.service';
import { Comanda } from 'src/app/wrappers/comanda';
import * as _ from 'lodash';
import { Title } from '@angular/platform-browser';

@Component({
  selector: 'app-comanda',
  templateUrl: './comanda.component.html',
  styleUrls: ['./comanda.component.scss']
})
export class ComandaComponent implements OnInit {

  private comandas: Comanda[] = [];
  private cicloTime;

  constructor(
    private comandaService: ComandaService,
    private titleService: Title
  ) {

    this.titleService.setTitle('Comanda');
  }

  ngOnInit() {

    this.buscar();
    this.ciclo();
  }

  ngOnDestroy() {

    clearTimeout(this.cicloTime);
  }

  buscar() {

    this.comandaService.getAll()
      .subscribe((res: Comanda[]) => {

        if (!_.isEqual(res, this.comandas)) {

          this.comandas = res;
        }
      }, e => console.error(e))
  }

  ciclo() {

    this.cicloTime = setTimeout(() => {

      this.buscar();
      this.ciclo();
    }, 5000);
  }

  updateComanda(comanda: Comanda, estadoNuevo: number, observacionNueva: string) {

    this.comandaService.updateComanda(comanda.id, estadoNuevo, observacionNueva)
      .subscribe(res => {

        if (res.data) {
          comanda.estado = estadoNuevo;
          comanda.observacion = observacionNueva;
        }
      }, e => console.error(e))
  }

  rellenar(relleno, longitud) {
    relleno = '' + relleno;
    while (relleno.length < longitud) {
      relleno = '0' + relleno;
    }
    return relleno;
  }

}