Commit 8bae28733d49e5f798349e6258124d47c9b5b8b2
1 parent
4062317f9f
Exists in
master
and in
1 other branch
categoria por defecto 0
Showing
1 changed file
with
1 additions
and
1 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 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 = this.categorias[0].id; | 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 | if (this.queMostrar == 'ordenar') { | 64 | if (this.queMostrar == 'ordenar') { |
65 | 65 | ||
66 | this.categorias.forEach((categoria: Categoria) => { | 66 | this.categorias.forEach((categoria: Categoria) => { |
67 | 67 | ||
68 | let tempProductos = data.filter((producto: Producto) => { | 68 | let tempProductos = data.filter((producto: Producto) => { |
69 | return producto.categoria_selfservice == categoria.id; | 69 | return producto.categoria_selfservice == categoria.id; |
70 | }); | 70 | }); |
71 | 71 | ||
72 | this.productos = this.productos.concat(tempProductos); | 72 | this.productos = this.productos.concat(tempProductos); |
73 | 73 | ||
74 | }); | 74 | }); |
75 | } else { | 75 | } else { |
76 | this.productos = data; | 76 | this.productos = data; |
77 | } | 77 | } |
78 | this.filterItems(); | 78 | this.filterItems(); |
79 | }, (error) => { | 79 | }, (error) => { |
80 | this.showSpinner = false; | 80 | this.showSpinner = false; |
81 | console.error(error); | 81 | console.error(error); |
82 | }); | 82 | }); |
83 | } | 83 | } |
84 | 84 | ||
85 | filterItems() { | 85 | filterItems() { |
86 | 86 | ||
87 | this.auxProductos = this.productos.filter(x => { | 87 | this.auxProductos = this.productos.filter(x => { |
88 | if (this.categoriaActive === 0) { | 88 | if (this.categoriaActive === 0) { |
89 | return x.DetArt.toLowerCase().includes(this.searchTerm.toLowerCase()); | 89 | return x.DetArt.toLowerCase().includes(this.searchTerm.toLowerCase()); |
90 | } | 90 | } |
91 | else { | 91 | else { |
92 | return x.DetArt.toLowerCase().includes(this.searchTerm.toLowerCase()) && | 92 | return x.DetArt.toLowerCase().includes(this.searchTerm.toLowerCase()) && |
93 | x.categoria_selfservice === this.categoriaActive; | 93 | x.categoria_selfservice === this.categoriaActive; |
94 | } | 94 | } |
95 | }); | 95 | }); |
96 | 96 | ||
97 | } | 97 | } |
98 | 98 | ||
99 | agregarAlCarrito(producto: Producto) { | 99 | agregarAlCarrito(producto: Producto) { |
100 | 100 | ||
101 | producto.cantidad = 1; | 101 | producto.cantidad = 1; |
102 | this.productoService.productos.push(producto); | 102 | this.productoService.productos.push(producto); |
103 | } | 103 | } |
104 | 104 | ||
105 | lostBlur() { | 105 | lostBlur() { |
106 | this.blurFocus.emit(); | 106 | this.blurFocus.emit(); |
107 | } | 107 | } |
108 | 108 | ||
109 | private elegirProducto(producto: Producto) { | 109 | private elegirProducto(producto: Producto) { |
110 | 110 | ||
111 | if (producto.PRO) { | 111 | if (producto.PRO) { |
112 | 112 | ||
113 | let imagenes = producto.imagenes; | 113 | let imagenes = producto.imagenes; |
114 | this.productoService.getPromocionByCodigos(producto.CodSec, producto.CodArt) | 114 | this.productoService.getPromocionByCodigos(producto.CodSec, producto.CodArt) |
115 | .subscribe(res => { | 115 | .subscribe(res => { |
116 | 116 | ||
117 | this.productoService.productoAcargar = res[0]; | 117 | this.productoService.productoAcargar = res[0]; |
118 | this.productoService.productoAcargar.imagenes = imagenes; | 118 | this.productoService.productoAcargar.imagenes = imagenes; |
119 | this.router.navigate(['inicio']); | 119 | this.router.navigate(['inicio']); |
120 | }, | 120 | }, |
121 | error => { console.error(error); } | 121 | error => { console.error(error); } |
122 | ); | 122 | ); |
123 | } else { | 123 | } else { |
124 | 124 | ||
125 | this.productoService.productoAcargar = producto; | 125 | this.productoService.productoAcargar = producto; |
126 | this.router.navigate(['inicio']); | 126 | this.router.navigate(['inicio']); |
127 | } | 127 | } |
128 | 128 | ||
129 | } | 129 | } |
130 | } | 130 | } |
131 | 131 |