Commit 81bae83d2f0b106eaa7f4aaf52601d0c47daff99

Authored by Marcelo Puebla
1 parent 2fbf7bb9ee
Exists in master

Pongo imagen generica si el producto no tiene imagen.

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 queMostrar: string = 'todo'; 20 private queMostrar: string = 'todo';
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.queMostrar = this.productoService.mostrar; 31 this.queMostrar = this.productoService.mostrar;
32 32
33 this.productoService.getCategorias() 33 this.productoService.getCategorias()
34 .subscribe((categorias: Categoria[]) => { 34 .subscribe((categorias: Categoria[]) => {
35 35
36 switch (this.queMostrar) { 36 switch (this.queMostrar) {
37 case 'todos': 37 case 'todos':
38 this.categorias = categorias; 38 this.categorias = categorias;
39 this.categoriaActive = 0; 39 this.categoriaActive = 0;
40 break; 40 break;
41 case 'promociones': 41 case 'promociones':
42 this.categorias = categorias; 42 this.categorias = categorias;
43 this.categoriaActive = 1; 43 this.categoriaActive = 1;
44 break; 44 break;
45 case 'ordenar': 45 case 'ordenar':
46 46
47 this.categorias = categorias.filter((categoria: Categoria) => { 47 this.categorias = categorias.filter((categoria: Categoria) => {
48 return categoria.ES_PEDIDO; 48 return categoria.ES_PEDIDO;
49 }); 49 });
50 50
51 this.categoriaActive = 0; 51 this.categoriaActive = 0;
52 52
53 break; 53 break;
54 default: 54 default:
55 break; 55 break;
56 } 56 }
57 57
58 }); 58 });
59 59
60 this.productoService.productoAcargar = undefined; 60 this.productoService.productoAcargar = undefined;
61 this.productoService.getAll() 61 this.productoService.getAll()
62 .subscribe((data: Producto[]) => { 62 .subscribe((data: Producto[]) => {
63 63
64 this.setProductosSinImagen(data);
65
64 if (this.queMostrar == 'ordenar') { 66 if (this.queMostrar == 'ordenar') {
65 67
66 this.categorias.forEach((categoria: Categoria) => { 68 this.categorias.forEach((categoria: Categoria) => {
67 69
68 let tempProductos = data.filter((producto: Producto) => { 70 let tempProductos = data.filter((producto: Producto) => {
69 return producto.categoria_selfservice == categoria.id; 71 return producto.categoria_selfservice == categoria.id;
70 }); 72 });
71 73
72 this.productos = this.productos.concat(tempProductos); 74 this.productos = this.productos.concat(tempProductos);
73 75
74 }); 76 });
75 } else { 77 } else {
76 this.productos = data; 78 this.productos = data;
77 } 79 }
78 this.filterItems(); 80 this.filterItems();
79 }, (error) => { 81 }, (error) => {
80 this.showSpinner = false; 82 this.showSpinner = false;
81 console.error(error); 83 console.error(error);
82 }); 84 });
83 } 85 }
84 86
85 filterItems() { 87 filterItems() {
86 88
87 this.auxProductos = this.productos.filter(x => { 89 this.auxProductos = this.productos.filter(x => {
88 if (this.categoriaActive === 0) { 90 if (this.categoriaActive === 0) {
89 return x.DET_LAR.toLowerCase().includes(this.searchTerm.toLowerCase()); 91 return x.DET_LAR.toLowerCase().includes(this.searchTerm.toLowerCase());
90 } 92 }
91 else { 93 else {
92 return x.DET_LAR.toLowerCase().includes(this.searchTerm.toLowerCase()) && 94 return x.DET_LAR.toLowerCase().includes(this.searchTerm.toLowerCase()) &&
93 x.categoria_selfservice === this.categoriaActive; 95 x.categoria_selfservice === this.categoriaActive;
94 } 96 }
95 }); 97 });
96 98
97 } 99 }
98 100
99 agregarAlCarrito(producto: Producto) { 101 agregarAlCarrito(producto: Producto) {
100 102
101 producto.cantidad = 1; 103 producto.cantidad = 1;
102 this.productoService.productos.push(producto); 104 this.productoService.productos.push(producto);
103 } 105 }
104 106
105 lostBlur() { 107 lostBlur() {
106 this.blurFocus.emit(); 108 this.blurFocus.emit();
107 } 109 }
108 110
109 private elegirProducto(producto: Producto) { 111 private elegirProducto(producto: Producto) {
110 112
111 if (producto.PRO) { 113 if (producto.PRO) {
112 114
113 let imagenes = producto.imagenes; 115 let imagenes = producto.imagenes;
114 this.productoService.getPromocionByCodigos(producto.CodSec, producto.CodArt) 116 this.productoService.getPromocionByCodigos(producto.CodSec, producto.CodArt)
115 .subscribe(res => { 117 .subscribe(res => {
116 118
117 this.productoService.productoAcargar = res[0]; 119 this.productoService.productoAcargar = res[0];
118 this.productoService.productoAcargar.imagenes = imagenes; 120 this.productoService.productoAcargar.imagenes = imagenes;
119 this.router.navigate(['inicio']); 121 this.router.navigate(['inicio']);
120 }, 122 },
121 error => { console.error(error); } 123 error => { console.error(error); }
122 ); 124 );
123 } else { 125 } else {
124 126
125 this.productoService.productoAcargar = producto; 127 this.productoService.productoAcargar = producto;
126 this.router.navigate(['inicio']); 128 this.router.navigate(['inicio']);
127 } 129 }
128 130
129 } 131 }
132
133 private setProductosSinImagen(productos: Producto[]) {
134
135 productos.forEach((producto: Producto) => {
136 producto.imagenes = producto.imagenes.length == 0 ?
137 [{ imagen: 'noImage.jpg' }] : producto.imagenes;
138 })
139 }
130 } 140 }
131 141