carrito.component.ts 2.41 KB
import { Component, OnInit, OnDestroy, HostListener } from '@angular/core';
import { Location } from '@angular/common';
import { ArticuloService } from 'src/app/services/articulo/articulo.service';
import { APP_SETTINGS } from 'src/etc/AppSettings';
import { Router } from '@angular/router';
import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal';
import { InactiveScreenService } from 'src/app/services/inactive-screen/inactive-screen.service';
import { ANIMATIONS } from 'src/app/utils/animations';
import { ErrorFormaPagoComponent } from 'src/app/shared/error-forma-pago/error-forma-pago.component';
import { ErrorStockComponent } from 'src/app/shared/error-stock/error-stock.component';

@Component({
  selector: 'app-carrito',
  templateUrl: './carrito.component.html',
  styleUrls: ['./carrito.component.scss'],
  animations: [ANIMATIONS.EnterLeaveX]
})
export class CarritoComponent implements OnInit, OnDestroy {
  urlImagenes = `${APP_SETTINGS.apiImagenes}/imagenes/`;
  maxCantidad = 50;
  modalRef: BsModalRef;

  constructor(
    public articuloService: ArticuloService,
    private location: Location,
    private router: Router,
    private inactiveScreen: InactiveScreenService,
    private modalService: BsModalService
  ) { }

  ngOnInit() {
    if (!this.articuloService.carrito.length) {
      this.router.navigate(['']);
      return;
    }
    this.mediaPantalla();
  }

  ngOnDestroy() {
    if (this.modalRef) this.modalRef.hide();
  }

  deleteArticulo(index: number) {
    this.articuloService.deleteArticulo(index);
  }

  validateCarrito() {
    const res = this.articuloService.validateCarrito()
    if (res.valid) {
      this.router.navigate(['forma-pago']);
    } else {
      this.modalService.show(
        ErrorStockComponent,
        {
          initialState: { articulo: res.articulo },
          class: 'modal-dialog-centered'
        });
    }
  }

  goBack() {
    this.location.back();
  }

  @HostListener('document:click', ['$event'])
  eventListener(event: Event) {
    clearTimeout(this.inactiveScreen.timerReposo);
    this.inactiveScreen.startTimeOutInactividad();
  }

  @HostListener('scroll', ['$event'])
  scrollEvent(event: Event) {
    clearTimeout(this.inactiveScreen.timerReposo);
    this.inactiveScreen.startTimeOutInactividad();
  }

  mediaPantalla() {
    if ($('body').hasClass('media-pantalla')) {
      $(`.carrito-content`).addClass('media-pantalla').addBack('media-pantalla');
    }
  }
}