diff --git a/src/app/components/sidebar/sidebar.component.html b/src/app/components/sidebar/sidebar.component.html index d15e913..357365b 100644 --- a/src/app/components/sidebar/sidebar.component.html +++ b/src/app/components/sidebar/sidebar.component.html @@ -31,7 +31,7 @@
@@ -40,7 +40,7 @@
@@ -62,7 +62,7 @@ @@ -74,13 +74,19 @@
-

({{cont}}) item

-

({{cont}}) items

+

+ ({{getCantidadProductos()}}) items +

Total

{{getTotal() | currency}}

- diff --git a/src/app/components/sidebar/sidebar.component.ts b/src/app/components/sidebar/sidebar.component.ts index 44c4a99..d7d0e4e 100644 --- a/src/app/components/sidebar/sidebar.component.ts +++ b/src/app/components/sidebar/sidebar.component.ts @@ -3,7 +3,6 @@ import { Producto } from 'src/app/wrappers/producto'; import { appSettings } from 'src/etc/AppSettings'; import { ProductoService } from 'src/app/services/producto.service'; - @Component({ selector: 'app-sidebar', templateUrl: './sidebar.component.html', @@ -11,93 +10,65 @@ import { ProductoService } from 'src/app/services/producto.service'; }) export class SidebarComponent implements OnInit { - private cont: number = 1; - private min: number = 1; - private max: number = 50; + private cantTotal: number = 0; + private cantMin: number = 1; + private cantMax: number = 50; private total: number = 0; private apiUrl: string = appSettings.apiUrl; public productosCarrito: Producto[] = []; - constructor(private productoService: ProductoService) { - - this.productosCarrito = this.productoService.productos; - } + constructor(private productoService: ProductoService) { } ngOnInit() { + this.productosCarrito = this.productoService.productos; } getCantidadProductos() { - for (let i = 0; i < this.productosCarrito.length; i++) { - this.productosCarrito[i].cantidad = 1 - this.cont++; - } - return this.cont; + setTimeout(() => { + var cantTotalAux = 0; + this.productosCarrito.forEach(producto => { + + cantTotalAux += producto.cantidad; + }, 250); + this.cantTotal = cantTotalAux; + }) + return this.cantTotal; } getTotal() { - let subTotal = 0; + var subTotal = 0; for (let i = 0; i < this.productosCarrito.length; i++) { subTotal = subTotal + (this.productosCarrito[i].PreVen * this.productosCarrito[i].cantidad); } return this.total = subTotal; } - public aumentarContador(index) { - - this.cont++; - for (let i = 0; i < this.productosCarrito.length; i++) { - if (i === index) { - this.total = this.total + this.productosCarrito[i].PreVen; - return (this.productosCarrito[i].cantidad === this.max) ? - this.productosCarrito[i].cantidad : this.productosCarrito[i].cantidad++; - - } - } - } - - decrementarContador(index) { + aumentarCantidad(producto: Producto) { - for (let i = 0; i < this.productosCarrito.length; i++) { - if (i === index && this.productosCarrito[i].cantidad > this.min) { - this.productosCarrito[i].cantidad--; - this.cont--; - break; - } + if (producto.cantidad < this.cantMax) { + producto.cantidad++; + this.cantTotal++ } - - this.getTotal() } - setCantidadItems() { + restarCantidad(producto: Producto) { - this.cont = 0; - for (let i = 0; i < this.productosCarrito.length; i++) { - this.cont += this.productosCarrito[i].cantidad; - } - } - - deleteProducto(index: number) { - - for (let i = 0; i < this.productosCarrito.length; i++) { - if (i === index) { - this.cont -= this.productosCarrito[i].cantidad; - //Elimina del total el PreVen de todo el item - this.total = this.total - (this.productosCarrito[i].PreVen * this.productosCarrito[i].cantidad); - this.productosCarrito.reverse().splice(i, 1); - return; - } + if (producto.cantidad > this.cantMin) { + producto.cantidad--; + this.cantTotal--; } } - cleanCarrito() { + deleteProducto(producto: Producto, index: number) { - this.productoService.productos = this.productosCarrito = []; - this.total = 0; - this.cont = 0; + this.cantTotal -= producto.cantidad; + this.total -= producto.PreVen * producto.cantidad; + this.productosCarrito.reverse().splice(index, 1); + return; } esPersonalizable(producto: Producto) {