Commit 7f8d72ac14c07037bf1b6021082c594ba7cfdfff

Authored by Marcelo Puebla
1 parent d684bd32d2
Exists in master

Cambio de tipo en objeto sinonimo,

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