diff --git a/src/app/components/inicio/inicio.component.ts b/src/app/components/inicio/inicio.component.ts index eb10032..671aba6 100644 --- a/src/app/components/inicio/inicio.component.ts +++ b/src/app/components/inicio/inicio.component.ts @@ -16,11 +16,19 @@ import { HostListener } from '@angular/core'; export class InicioComponent implements OnInit { @HostListener('document:keypress', ["$event"]) catchInput(e: KeyboardEvent) { - this.busqueda += e.key; + + if (e.keyCode == 13) { + this.buscarByCodigoBarras(this.busqueda); + this.busqueda = ''; + } else { + this.busqueda += e.key; + } + }; @ViewChild('pop', { static: false }) popoverDirective: PopoverDirective; private productoAcargar: Producto; + private productos: Producto[]; private promoAcargar: Promocion; private tienePromo = false; private productoEsPromo = false; @@ -37,10 +45,9 @@ export class InicioComponent implements OnInit { ngOnInit() { - this.vaciarBusqueda(); - this.productoAcargar = this.productoService.productoAcargar; this.getPromociones(); + this.getProductos(); } getPromociones() { @@ -70,6 +77,13 @@ export class InicioComponent implements OnInit { this.popoverDirective.show(); } + getProductos() { + this.productoService.getAll() + .subscribe((productos: Producto[]) => { + this.productos = productos; + }); + } + private goPage(pageUrl) { this.router.navigate([pageUrl]); @@ -118,17 +132,27 @@ export class InicioComponent implements OnInit { } } - vaciarBusqueda() { - - setTimeout(() => { - this.busqueda = ''; - }, 2000); - } - sinonimoSeleccionado($event: Sinonimo) { console.log($event); this.sinonimoAcargar = $event; } + buscarByCodigoBarras(busqueda) { + + let producto = this.productos.filter(producto => { + return producto.codigoBarra == busqueda; + }); + + if (producto.length) { + + this.productoAcargar = producto[0]; + this.getPromociones(); + + } else { + alert('No se encuentra el producto'); + } + + } + } diff --git a/src/app/wrappers/producto.ts b/src/app/wrappers/producto.ts index 8653e8d..de4c338 100644 --- a/src/app/wrappers/producto.ts +++ b/src/app/wrappers/producto.ts @@ -91,4 +91,5 @@ export interface Producto { cantidad?: number; showCargarProducto?: boolean; esPadre?: boolean; + codigoBarra: string; }