Commit 8c203fc8f180e8474ef02e79100557ca1abe10eb

Authored by Marcelo Puebla
1 parent b96a90a4b3
Exists in master

Cambio para limpiar el carrito.

src/app/components/sidebar/sidebar.component.ts
1 import { Component, OnInit } from '@angular/core'; 1 import { Component, OnInit } from '@angular/core';
2 import { Producto } from 'src/app/wrappers/producto'; 2 import { Producto } from 'src/app/wrappers/producto';
3 import { appSettings } from 'src/etc/AppSettings'; 3 import { appSettings } from 'src/etc/AppSettings';
4 import { ProductoService } from 'src/app/services/producto.service'; 4 import { ProductoService } from 'src/app/services/producto.service';
5 5
6 6
7 @Component({ 7 @Component({
8 selector: 'app-sidebar', 8 selector: 'app-sidebar',
9 templateUrl: './sidebar.component.html', 9 templateUrl: './sidebar.component.html',
10 styleUrls: ['./sidebar.component.scss'], 10 styleUrls: ['./sidebar.component.scss'],
11 }) 11 })
12 export class SidebarComponent implements OnInit { 12 export class SidebarComponent implements OnInit {
13 13
14 private cont: number = 1; 14 private cont: number = 1;
15 private min: number = 1; 15 private min: number = 1;
16 private max: number = 50; 16 private max: number = 50;
17 private total: number = 0; 17 private total: number = 0;
18 private apiUrl: string = appSettings.apiUrl; 18 private apiUrl: string = appSettings.apiUrl;
19 19
20 public productosCarrito: Producto[] = []; 20 public productosCarrito: Producto[] = [];
21 21
22 constructor(private productoService: ProductoService) { } 22 constructor(private productoService: ProductoService) { }
23 23
24 ngOnInit() { 24 ngOnInit() {
25 25
26 this.productoService.productosEvent 26 this.productoService.productosEvent
27 .subscribe((data: Producto[]) => { 27 .subscribe((data: Producto[]) => {
28 this.productosCarrito = data; 28 this.productosCarrito = data;
29 }, (error) => { console.error(error); }) 29 }, (error) => { console.error(error); })
30 } 30 }
31 31
32 getCantidadProductos() { 32 getCantidadProductos() {
33 33
34 for (let i = 0; i < this.productosCarrito.length; i++) { 34 for (let i = 0; i < this.productosCarrito.length; i++) {
35 this.productosCarrito[i].cantidad = 1 35 this.productosCarrito[i].cantidad = 1
36 this.cont++; 36 this.cont++;
37 } 37 }
38 return this.cont; 38 return this.cont;
39 } 39 }
40 40
41 getTotal() { 41 getTotal() {
42 42
43 let subTotal = 0; 43 let subTotal = 0;
44 for (let i = 0; i < this.productosCarrito.length; i++) { 44 for (let i = 0; i < this.productosCarrito.length; i++) {
45 subTotal = subTotal + (this.productosCarrito[i].PreVen * this.productosCarrito[i].cantidad); 45 subTotal = subTotal + (this.productosCarrito[i].PreVen * this.productosCarrito[i].cantidad);
46 } 46 }
47 return this.total = subTotal; 47 return this.total = subTotal;
48 } 48 }
49 49
50 public aumentarContador(index) { 50 public aumentarContador(index) {
51 51
52 this.cont++; 52 this.cont++;
53 for (let i = 0; i < this.productosCarrito.length; i++) { 53 for (let i = 0; i < this.productosCarrito.length; i++) {
54 if (i === index) { 54 if (i === index) {
55 this.total = this.total + this.productosCarrito[i].PreVen; 55 this.total = this.total + this.productosCarrito[i].PreVen;
56 return (this.productosCarrito[i].cantidad === this.max) ? 56 return (this.productosCarrito[i].cantidad === this.max) ?
57 this.productosCarrito[i].cantidad : this.productosCarrito[i].cantidad++; 57 this.productosCarrito[i].cantidad : this.productosCarrito[i].cantidad++;
58 58
59 } 59 }
60 } 60 }
61 } 61 }
62 62
63 decrementarContador(index) { 63 decrementarContador(index) {
64 64
65 for (let i = 0; i < this.productosCarrito.length; i++) { 65 for (let i = 0; i < this.productosCarrito.length; i++) {
66 if (i === index && this.productosCarrito[i].cantidad > this.min) { 66 if (i === index && this.productosCarrito[i].cantidad > this.min) {
67 this.productosCarrito[i].cantidad--; 67 this.productosCarrito[i].cantidad--;
68 this.cont--; 68 this.cont--;
69 break; 69 break;
70 } 70 }
71 } 71 }
72 72
73 this.getTotal() 73 this.getTotal()
74 } 74 }
75 75
76 setCantidadItems() { 76 setCantidadItems() {
77 77
78 this.cont = 0; 78 this.cont = 0;
79 for (let i = 0; i < this.productosCarrito.length; i++) { 79 for (let i = 0; i < this.productosCarrito.length; i++) {
80 this.cont += this.productosCarrito[i].cantidad; 80 this.cont += this.productosCarrito[i].cantidad;
81 } 81 }
82 } 82 }
83 83
84 deleteProducto(index: number) { 84 deleteProducto(index: number) {
85 85
86 for (let i = 0; i < this.productosCarrito.length; i++) { 86 for (let i = 0; i < this.productosCarrito.length; i++) {
87 if (i === index) { 87 if (i === index) {
88 this.cont -= this.productosCarrito[i].cantidad; 88 this.cont -= this.productosCarrito[i].cantidad;
89 //Elimina del total el PreVen de todo el item 89 //Elimina del total el PreVen de todo el item
90 this.total = this.total - (this.productosCarrito[i].PreVen * this.productosCarrito[i].cantidad); 90 this.total = this.total - (this.productosCarrito[i].PreVen * this.productosCarrito[i].cantidad);
91 this.productosCarrito.splice(i, 1); 91 this.productosCarrito.splice(i, 1);
92 return; 92 return;
93 } 93 }
94 } 94 }
95 } 95 }
96 96
97 cleanCarrito() { 97 cleanCarrito() {
98 98
99 this.productosCarrito = []; 99 this.productoService.productos = this.productosCarrito = [];
100 this.total = 0; 100 this.total = 0;
101 this.cont = 0; 101 this.cont = 0;
102 } 102 }
103 } 103 }
104 104