Commit 6a61f21a9e968b695c97162a2e3c233c761dfc11

Authored by Marcelo Puebla
1 parent 264e5848c8
Exists in master and in 1 other branch validar_pve

arregle de suscribe de undefined

src/app/components/confirmacion-carrito/confirmacion-carrito.component.ts
1 import { Component, OnInit, OnDestroy } from '@angular/core'; 1 import { Component, OnInit, OnDestroy } from '@angular/core';
2 import { appSettings } from 'src/etc/AppSettings'; 2 import { appSettings } from 'src/etc/AppSettings';
3 import { Location } from '@angular/common'; 3 import { Location } from '@angular/common';
4 import { ProductoService } from 'src/app/services/producto.service'; 4 import { ProductoService } from 'src/app/services/producto.service';
5 import { Producto } from 'src/app/wrappers/producto'; 5 import { Producto } from 'src/app/wrappers/producto';
6 import { Router } from '@angular/router'; 6 import { Router } from '@angular/router';
7 import { Subscription } from 'rxjs'; 7 import { Subscription } from 'rxjs';
8 import { BsModalService } from 'ngx-bootstrap'; 8 import { BsModalService } from 'ngx-bootstrap';
9 import { PagoConTarjetaComponent } from '../pago-con-tarjeta/pago-con-tarjeta.component'; 9 import { PagoConTarjetaComponent } from '../pago-con-tarjeta/pago-con-tarjeta.component';
10 10
11 @Component({ 11 @Component({
12 selector: 'app-confirmacion-carrito', 12 selector: 'app-confirmacion-carrito',
13 templateUrl: './confirmacion-carrito.component.html', 13 templateUrl: './confirmacion-carrito.component.html',
14 styleUrls: ['./confirmacion-carrito.component.scss'] 14 styleUrls: ['./confirmacion-carrito.component.scss']
15 }) 15 })
16 export class ConfirmacionCarritoComponent implements OnInit, OnDestroy { 16 export class ConfirmacionCarritoComponent implements OnInit, OnDestroy {
17 17
18 private productos: Producto[] = []; 18 private productos: Producto[] = [];
19 private total: number = 0; 19 private total: number = 0;
20 private apiImagenes: string = appSettings.apiImagenes; 20 private apiImagenes: string = appSettings.apiImagenes;
21 private timerReposo: any; 21 private timerReposo: any;
22 private compraConEfectivofinalizada: boolean = false; 22 private compraConEfectivofinalizada: boolean = false;
23 private compraConQRfinalizada: boolean = false; 23 private compraConQRfinalizada: boolean = false;
24 private verQR: boolean = false; 24 private verQR: boolean = false;
25 private subscribePago: Subscription; 25 private subscribePago: Subscription;
26 26
27 constructor( 27 constructor(
28 private location: Location, 28 private location: Location,
29 private productoService: ProductoService, 29 private productoService: ProductoService,
30 private router: Router, 30 private router: Router,
31 private modalService: BsModalService, 31 private modalService: BsModalService,
32 ) { } 32 ) { }
33 33
34 ngOnInit() { 34 ngOnInit() {
35 35
36 this.timerReposo = setTimeout(() => { 36 this.timerReposo = setTimeout(() => {
37 37
38 this.router.navigate(['cancelar-compra']); 38 this.router.navigate(['cancelar-compra']);
39 }, 90000) 39 }, 90000)
40 this.productos = this.productoService.productos; 40 this.productos = this.productoService.productos;
41 } 41 }
42 42
43 ngOnDestroy() { 43 ngOnDestroy() {
44 44
45 this.subscribePago.unsubscribe(); 45 if (this.subscribePago !== undefined) {
46 this.subscribePago.unsubscribe();
47 }
46 clearTimeout(this.timerReposo); 48 clearTimeout(this.timerReposo);
47 } 49 }
48 50
49 volverPreviousPage() { 51 volverPreviousPage() {
50 52
51 this.subscribePago.unsubscribe(); 53 if (this.subscribePago !== undefined) {
54 this.subscribePago.unsubscribe();
55 }
52 56
53 if (this.verQR) { 57 if (this.verQR) {
54 this.verQR = !this.verQR; 58 this.verQR = !this.verQR;
55 return; 59 return;
56 } 60 }
57 61
58 this.location.back(); 62 this.location.back();
59 } 63 }
60 64
61 getTotal() { 65 getTotal() {
62 66
63 var subTotal = 0; 67 var subTotal = 0;
64 this.productos.forEach(producto => { 68 this.productos.forEach(producto => {
65 69
66 subTotal = subTotal + (producto.PreVen * producto.cantidad); 70 subTotal = subTotal + (producto.PreVen * producto.cantidad);
67 }); 71 });
68 return this.total = subTotal; 72 return this.total = subTotal;
69 } 73 }
70 74
71 reiniciarTimer() { 75 reiniciarTimer() {
72 76
73 clearTimeout(this.timerReposo); 77 clearTimeout(this.timerReposo);
74 this.timerReposo = setTimeout(() => { 78 this.timerReposo = setTimeout(() => {
75 79
76 this.router.navigate(['cancelar-compra']); 80 this.router.navigate(['cancelar-compra']);
77 }, 90000) 81 }, 90000)
78 } 82 }
79 83
80 //#region METODOS PARA LA FORMA DE PAGO 84 //#region METODOS PARA LA FORMA DE PAGO
81 pagar(medioPago: number) { 85 pagar(medioPago: number) {
82 86
83 if (medioPago === 9) { 87 if (medioPago === 9) {
84 88
85 this.verQR = true; 89 this.verQR = true;
86 } 90 }
87 91
88 this.subscribePago = this.productoService.pagar(medioPago) 92 this.subscribePago = this.productoService.pagar(medioPago)
89 .subscribe(() => { 93 .subscribe(() => {
90 94
91 clearTimeout(this.timerReposo); 95 clearTimeout(this.timerReposo);
92 96
93 if (medioPago === 1) { 97 if (medioPago === 1) {
94 98
95 this.compraConEfectivofinalizada = true; 99 this.compraConEfectivofinalizada = true;
96 } else if (medioPago === 9) { 100 } else if (medioPago === 9) {
97 101
98 this.compraConQRfinalizada = true; 102 this.compraConQRfinalizada = true;
99 } 103 }
100 104
101 setTimeout(() => { 105 setTimeout(() => {
102 106
103 this.router.navigate(['mensaje-final']); 107 this.router.navigate(['mensaje-final']);
104 }, 10000); 108 }, 10000);
105 }, err => { 109 }, err => {
106 console.log(err); 110 console.log(err);
107 alert('algo salió mal'); 111 alert('algo salió mal');
108 }) 112 })
109 } 113 }
110 //#endregion 114 //#endregion
111 115
112 abrirPagoConTarjeta() { 116 abrirPagoConTarjeta() {
113 117
114 this.modalService.show(PagoConTarjetaComponent, { 118 this.modalService.show(PagoConTarjetaComponent, {
115 class: 'modal-lg', 119 class: 'modal-lg',
116 ignoreBackdropClick: true, 120 ignoreBackdropClick: true,
117 initialState: { importeTotal: this.total }, 121 initialState: { importeTotal: this.total },
118 }); 122 });
119 } 123 }
120 } 124 }
121 125