pago-tarjeta.component.ts 1.55 KB
import { Component, OnInit, TemplateRef, OnDestroy, ViewChild } from '@angular/core';
import { ArticuloService } from 'src/app/services/articulo/articulo.service';
import { Router } from '@angular/router';
import { BsModalService } from 'ngx-bootstrap/modal';

@Component({
  selector: 'app-pago-tarjeta',
  templateUrl: './pago-tarjeta.component.html',
  styleUrls: ['./pago-tarjeta.component.scss']
})
export class PagoTarjetaComponent implements OnInit, OnDestroy {
  timer: any;
  @ViewChild('template', {static: true}) public template: TemplateRef<any>;

  constructor(
    private articuloService: ArticuloService,
    private router: Router,
    private modalService: BsModalService
  ) { }

  ngOnInit() {
    if (!this.articuloService.carrito.length) {
      this.router.navigate(['']);
      return;
    }
    this.openModalEspera(this.template);
    const dataPago = {
      pedidoAnombreDe: ''
    };
    this.articuloService.pay(dataPago)
      .subscribe((res: any) => {
        this.articuloService.idComanda = res.data;
        this.router.navigate(['mensaje-final']);
      }, err => console.error(err));
    this.mediaPantalla();
  }

  ngOnDestroy() {
    if (this.timer) clearTimeout(this.timer);
  }

  mediaPantalla() {
    if ($('body').hasClass('media-pantalla')) {
      $('.pago-tarjeta').addClass('media-pantalla');
    }
  }

  openModalEspera(templateRef: TemplateRef<any>) {
    const modalRef = this.modalService.show(templateRef, { class: 'modal-lg modal-dialog-centered' });
    this.timer = setTimeout(() => {
      modalRef.hide();
    }, 5000);
  }
}