Commit e612a8203033471d831e545c1cb7f3e7a39371b5

Authored by Eric Fernandez
1 parent d1da6b40b7
Exists in master

unsubscribe on back

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 8
8 @Component({ 9 @Component({
9 selector: 'app-confirmacion-carrito', 10 selector: 'app-confirmacion-carrito',
10 templateUrl: './confirmacion-carrito.component.html', 11 templateUrl: './confirmacion-carrito.component.html',
11 styleUrls: ['./confirmacion-carrito.component.scss'] 12 styleUrls: ['./confirmacion-carrito.component.scss']
12 }) 13 })
13 export class ConfirmacionCarritoComponent implements OnInit, OnDestroy { 14 export class ConfirmacionCarritoComponent implements OnInit, OnDestroy {
14 15
15 private productos: Producto[] = []; 16 private productos: Producto[] = [];
16 private total: number = 0; 17 private total: number = 0;
17 private apiImagenes: string = appSettings.apiImagenes; 18 private apiImagenes: string = appSettings.apiImagenes;
18 private timerReposo: any; 19 private timerReposo: any;
19 private compraConEfectivofinalizada: boolean = false; 20 private compraConEfectivofinalizada: boolean = false;
20 private compraConQRfinalizada: boolean = false; 21 private compraConQRfinalizada: boolean = false;
21 private verQR: boolean = false; 22 private verQR: boolean = false;
23 private subscribePago: Subscription;
22 24
23 constructor( 25 constructor(
24 private location: Location, 26 private location: Location,
25 private productoService: ProductoService, 27 private productoService: ProductoService,
26 private router: Router 28 private router: Router
27 ) { } 29 ) { }
28 30
29 ngOnInit() { 31 ngOnInit() {
30 32
31 this.timerReposo = setTimeout(() => { 33 this.timerReposo = setTimeout(() => {
32 34
33 this.router.navigate(['cancelar-compra']); 35 this.router.navigate(['cancelar-compra']);
34 }, 90000) 36 }, 90000)
35 this.productos = this.productoService.productos; 37 this.productos = this.productoService.productos;
36 } 38 }
37 39
38 ngOnDestroy() { 40 ngOnDestroy() {
39 41
40 clearTimeout(this.timerReposo); 42 clearTimeout(this.timerReposo);
41 } 43 }
42 44
43 volverPreviousPage() { 45 volverPreviousPage() {
44 46
47 this.subscribePago.unsubscribe();
48
45 if (this.verQR) { 49 if (this.verQR) {
46 this.verQR = !this.verQR; 50 this.verQR = !this.verQR;
47 return; 51 return;
48 } 52 }
53
49 this.location.back(); 54 this.location.back();
50 } 55 }
51 56
52 getTotal() { 57 getTotal() {
53 58
54 var subTotal = 0; 59 var subTotal = 0;
55 this.productos.forEach(producto => { 60 this.productos.forEach(producto => {
56 61
57 subTotal = subTotal + (producto.PreVen * producto.cantidad); 62 subTotal = subTotal + (producto.PreVen * producto.cantidad);
58 }); 63 });
59 return this.total = subTotal; 64 return this.total = subTotal;
60 } 65 }
61 66
62 reiniciarTimer() { 67 reiniciarTimer() {
63 68
64 clearTimeout(this.timerReposo); 69 clearTimeout(this.timerReposo);
65 this.timerReposo = setTimeout(() => { 70 this.timerReposo = setTimeout(() => {
66 71
67 this.router.navigate(['cancelar-compra']); 72 this.router.navigate(['cancelar-compra']);
68 }, 90000) 73 }, 90000)
69 } 74 }
70 75
71 //#region METODOS PARA LA FORMA DE PAGO 76 //#region METODOS PARA LA FORMA DE PAGO
72 pagar(medioPago: string) { 77 pagar(medioPago: string) {
73 78
74 if (medioPago == 'electronico') { 79 if (medioPago == 'electronico') {
75 80
76 this.verQR = true; 81 this.verQR = true;
77 } 82 }
78 83
79 this.productoService.pagar(medioPago) 84 this.subscribePago = this.productoService.pagar(medioPago)
80 .subscribe(() => { 85 .subscribe(() => {
81 86
82 clearTimeout(this.timerReposo); 87 clearTimeout(this.timerReposo);
83 88
84 if (medioPago == 'efectivo') { 89 if (medioPago == 'efectivo') {
85 90
86 this.compraConEfectivofinalizada = true; 91 this.compraConEfectivofinalizada = true;
87 } else if (medioPago == 'electronico') { 92 } else if (medioPago == 'electronico') {
88 93
89 this.compraConQRfinalizada = true; 94 this.compraConQRfinalizada = true;
90 } 95 }
91 96
92 setTimeout(() => { 97 setTimeout(() => {
93 98
94 this.router.navigate(['mensaje-final']); 99 this.router.navigate(['mensaje-final']);
95 }, 10000); 100 }, 10000);
96 }, err => { 101 }, err => {
97 console.log(err); 102 console.log(err);
98 alert('algo salió mal'); 103 alert('algo salió mal');
99 }) 104 })
100 } 105 }
101 //#endregion 106 //#endregion
102 107
103 } 108 }
104 109