Commit 455398b6ee266e0d35d97add242f8e527b9bc358
1 parent
500dafd929
Exists in
master
and in
1 other branch
Codigo identado
Showing
1 changed file
with
3 additions
and
3 deletions
Show diff stats
src/app/components/busqueda-productos/busqueda-productos.component.ts
| 1 | import { Component, OnInit, EventEmitter } from '@angular/core'; | 1 | import { Component, OnInit, EventEmitter } from '@angular/core'; |
| 2 | import { ProductoService } from 'src/app/services/producto.service'; | 2 | import { ProductoService } from 'src/app/services/producto.service'; |
| 3 | import { Producto } from 'src/app/wrappers/producto'; | 3 | import { Producto } from 'src/app/wrappers/producto'; |
| 4 | import { Categoria } from 'src/app/wrappers/categoria'; | 4 | import { Categoria } from 'src/app/wrappers/categoria'; |
| 5 | import { appSettings } from 'src/etc/AppSettings'; | 5 | import { appSettings } from 'src/etc/AppSettings'; |
| 6 | import { Router } from '@angular/router'; | 6 | import { Router } from '@angular/router'; |
| 7 | 7 | ||
| 8 | @Component({ | 8 | @Component({ |
| 9 | selector: 'app-busqueda-productos', | 9 | selector: 'app-busqueda-productos', |
| 10 | templateUrl: './busqueda-productos.component.html', | 10 | templateUrl: './busqueda-productos.component.html', |
| 11 | styleUrls: ['./busqueda-productos.component.scss'] | 11 | styleUrls: ['./busqueda-productos.component.scss'] |
| 12 | }) | 12 | }) |
| 13 | export class BusquedaProductosComponent implements OnInit { | 13 | export class BusquedaProductosComponent implements OnInit { |
| 14 | 14 | ||
| 15 | private productos: Producto[] = []; | 15 | private productos: Producto[] = []; |
| 16 | private auxProductos: Producto[] = []; | 16 | private auxProductos: Producto[] = []; |
| 17 | private searchTerm: string = ''; | 17 | private searchTerm: string = ''; |
| 18 | private categoriaActive: number = null; | 18 | private categoriaActive: number = null; |
| 19 | private showSpinner: boolean = true; | 19 | private showSpinner: boolean = true; |
| 20 | private verCategorias: boolean = true; | 20 | private verCategorias: boolean = true; |
| 21 | private apiUrl: string = appSettings.apiUrl; | 21 | private apiUrl: string = appSettings.apiUrl; |
| 22 | private categorias: Categoria[] = []; | 22 | private categorias: Categoria[] = []; |
| 23 | private blurFocus = new EventEmitter(); | 23 | private blurFocus = new EventEmitter(); |
| 24 | 24 | ||
| 25 | constructor( | 25 | constructor( |
| 26 | private productoService: ProductoService, | 26 | private productoService: ProductoService, |
| 27 | private router: Router) { } | 27 | private router: Router) { } |
| 28 | 28 | ||
| 29 | ngOnInit() { | 29 | ngOnInit() { |
| 30 | 30 | ||
| 31 | this.verCategorias = this.productoService.verCategoriasProductos; | 31 | this.verCategorias = this.productoService.verCategoriasProductos; |
| 32 | 32 | ||
| 33 | this.productoService.getCategorias() | 33 | this.productoService.getCategorias() |
| 34 | .subscribe((categorias: Categoria[]) => { | 34 | .subscribe((categorias: Categoria[]) => { |
| 35 | this.categorias = categorias; | 35 | this.categorias = categorias; |
| 36 | this.categoriaActive = this.verCategorias ? 0 : categorias[0].id; | 36 | this.categoriaActive = this.verCategorias ? 0 : categorias[0].id; |
| 37 | }); | 37 | }); |
| 38 | 38 | ||
| 39 | this.productoService.productoAcargar = undefined; | 39 | this.productoService.productoAcargar = undefined; |
| 40 | this.productoService.getAll() | 40 | this.productoService.getAll() |
| 41 | .subscribe((data: Producto[]) => { | 41 | .subscribe((data: Producto[]) => { |
| 42 | 42 | ||
| 43 | this.productos = data; | 43 | this.productos = data; |
| 44 | this.filterItems(); | 44 | this.filterItems(); |
| 45 | }, (error) => { | 45 | }, (error) => { |
| 46 | this.showSpinner = false; | 46 | this.showSpinner = false; |
| 47 | console.error(error); | 47 | console.error(error); |
| 48 | }); | 48 | }); |
| 49 | } | 49 | } |
| 50 | 50 | ||
| 51 | filterItems() { | 51 | filterItems() { |
| 52 | 52 | ||
| 53 | this.auxProductos = this.productos.filter(x => { | 53 | this.auxProductos = this.productos.filter(x => { |
| 54 | if (this.categoriaActive === 0){ | 54 | if (this.categoriaActive === 0) { |
| 55 | return x.DetArt.toLowerCase().includes(this.searchTerm.toLowerCase()) | 55 | return x.DetArt.toLowerCase().includes(this.searchTerm.toLowerCase()) |
| 56 | } | 56 | } |
| 57 | else{ | 57 | else { |
| 58 | return x.DetArt.toLowerCase().includes(this.searchTerm.toLowerCase()) && | 58 | return x.DetArt.toLowerCase().includes(this.searchTerm.toLowerCase()) && |
| 59 | x.categoria_selfservice === this.categoriaActive; | 59 | x.categoria_selfservice === this.categoriaActive; |
| 60 | } | 60 | } |
| 61 | }); | 61 | }); |
| 62 | } | 62 | } |
| 63 | 63 | ||
| 64 | agregarAlCarrito(producto: Producto) { | 64 | agregarAlCarrito(producto: Producto) { |
| 65 | 65 | ||
| 66 | producto.cantidad = 1; | 66 | producto.cantidad = 1; |
| 67 | this.productoService.productos.push(producto); | 67 | this.productoService.productos.push(producto); |
| 68 | } | 68 | } |
| 69 | 69 | ||
| 70 | lostBlur() { | 70 | lostBlur() { |
| 71 | this.blurFocus.emit(); | 71 | this.blurFocus.emit(); |
| 72 | } | 72 | } |
| 73 | 73 | ||
| 74 | private elegirProducto(producto: Producto) { | 74 | private elegirProducto(producto: Producto) { |
| 75 | 75 | ||
| 76 | if (producto.PRO) { | 76 | if (producto.PRO) { |
| 77 | 77 | ||
| 78 | let imagenes = producto.imagenes; | 78 | let imagenes = producto.imagenes; |
| 79 | this.productoService.getPromocionByCodigos(producto.CodSec, producto.CodArt) | 79 | this.productoService.getPromocionByCodigos(producto.CodSec, producto.CodArt) |
| 80 | .subscribe(res => { | 80 | .subscribe(res => { |
| 81 | 81 | ||
| 82 | this.productoService.productoAcargar = res[0]; | 82 | this.productoService.productoAcargar = res[0]; |
| 83 | this.productoService.productoAcargar.imagenes = imagenes; | 83 | this.productoService.productoAcargar.imagenes = imagenes; |
| 84 | this.router.navigate(['inicio']); | 84 | this.router.navigate(['inicio']); |
| 85 | }, | 85 | }, |
| 86 | error => { console.error(error); } | 86 | error => { console.error(error); } |
| 87 | ); | 87 | ); |
| 88 | } else { | 88 | } else { |
| 89 | 89 | ||
| 90 | this.productoService.productoAcargar = producto; | 90 | this.productoService.productoAcargar = producto; |
| 91 | this.router.navigate(['inicio']); | 91 | this.router.navigate(['inicio']); |
| 92 | } | 92 | } |
| 93 | 93 | ||
| 94 | } | 94 | } |
| 95 | } | 95 | } |
| 96 | 96 |