inicio.component.ts
3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { Component, OnInit, ViewChild } from '@angular/core';
import { PopoverDirective } from 'ngx-bootstrap';
import { appSettings } from 'src/etc/AppSettings';
import { Producto } from 'src/app/wrappers/producto';
import { ProductoService } from 'src/app/services/producto.service';
import { Router } from '@angular/router';
import { Promocion } from 'src/app/wrappers/promocion';
import { Sinonimo } from 'src/app/wrappers/sinonimo';
@Component({
selector: 'app-inicio',
templateUrl: './inicio.component.html',
styleUrls: ['./inicio.component.scss']
})
export class InicioComponent implements OnInit {
@ViewChild('pop', { static: false }) popoverDirective: PopoverDirective;
private productoAcargar: Producto;
private promoAcargar: Promocion;
private sinonimoAcargar: Sinonimo;
promociones: Promocion[] = [];
sinonimos: Sinonimo[] = [];
apiUrl: string = appSettings.apiUrl
constructor(
private router: Router,
private productoService: ProductoService) { }
ngOnInit() {
this.productoAcargar = this.productoService.productoAcargar;
this.getPromociones();
}
getPromociones() {
if (this.productoAcargar) {
var sector = this.productoAcargar.CodSec;
var codigo = this.productoAcargar.CodArt;
this.productoService.getPromocion(sector, codigo)
.subscribe((res: Promocion[]) => {
if (res.length === 0) {
//Si no tiene promociones la cargará al carrito despues de un tiempo
setTimeout(() => {
this.productoService.productos.push(this.productoAcargar);
this.productoAcargar = undefined;
}, 2000)
} else {
this.promociones = res;
this.popoverDirective.show();
}
}, error => { console.error(error); })
}
}
showPopover() {
this.popoverDirective.show();
}
private goPage(pageUrl) {
this.router.navigate([pageUrl]);
}
deshacerCarga() {
if (this.sinonimoAcargar || this.sinonimos.length > 0) {
this.sinonimos = [];
this.sinonimoAcargar = undefined;
this.popoverDirective.hide();
}
if (this.promoAcargar) {
this.promoAcargar = undefined;
this.popoverDirective.show();
} else {
this.productoAcargar = undefined;
this.promociones = [];
this.popoverDirective.hide();
}
}
promoSeleccionada($event: Promocion) {
this.promoAcargar = $event;
this.popoverDirective.hide();
if (this.promoAcargar.sinonimos) {
var sector = this.promoAcargar.sector;
var codigo = this.promoAcargar.codigo;
this.productoService.getPromocionSinonimos(sector, codigo)
.subscribe((res: Sinonimo[]) => {
res.forEach(resSinonimo => {
resSinonimo.productos.forEach(productoSinonimo => {
this.promoAcargar.productos.forEach(productoPromo => {
if (productoPromo.id === productoSinonimo.id) {
productoSinonimo.esPadre = true;
}
});
})
})
this.sinonimos = res;
this.showPopover();
})
}
}
sinonimoSeleccionado($event: Sinonimo) {
console.log($event);
this.sinonimoAcargar = $event;
}
}