diff --git a/src/app/components/sidebar/sidebar.component.html b/src/app/components/sidebar/sidebar.component.html index f4ea36f..00bb33e 100644 --- a/src/app/components/sidebar/sidebar.component.html +++ b/src/app/components/sidebar/sidebar.component.html @@ -37,7 +37,7 @@
- +
\ No newline at end of file diff --git a/src/app/components/sidebar/sidebar.component.ts b/src/app/components/sidebar/sidebar.component.ts index ba4f1b4..5281245 100644 --- a/src/app/components/sidebar/sidebar.component.ts +++ b/src/app/components/sidebar/sidebar.component.ts @@ -1,5 +1,5 @@ import { Component, OnInit } from '@angular/core'; -import { timingSafeEqual } from 'crypto'; + @Component({ selector: 'app-sidebar', @@ -43,59 +43,82 @@ export class SidebarComponent implements OnInit { constructor() { } ngOnInit() { - this.getProductosCarrito(); + this.getCantidadProductos(); this.getTotal(); + } - getCantidadProductos(){ + getCantidadProductos() { var aux = 0; for (let i = 0; i < this.productos.length; i++) { ++aux; } return this.cont = aux; - } - getProductosCarrito() { - return this.productos; - } + getTotal() { - getTotal(){ - var subTotal; + let subTotal = 0; for (let i = 0; i < this.productos.length; i++) { - this.productos[i].cantidad; + subTotal = subTotal + (this.productos[i].precio * this.productos[i].cantidad); } + console.log(subTotal); + return this.total = subTotal; } - public aumentarContador(index) { - ++this.cont; - for (let i = 0; i < this.productos.length; i++) { + public aumentarContador(index) { + + ++this.cont; + for (let i = 0; i < this.productos.length; i++) { if (i === index) { + this.total = this.total + this.productos[i].precio; return (this.productos[i].cantidad === this.max) ? this.productos[i].cantidad : ++this.productos[i].cantidad; - + } } } decrementarContador(index) { - --this.cont; + for (let i = 0; i < this.productos.length; i++) { - if (i === index) { - return (this.productos[i].cantidad === this.min) ? - this.productos[i].cantidad : --this.productos[i].cantidad; + if (i === index && this.productos[i].cantidad > 1) { + --this.productos[i].cantidad; + --this.cont; + break; } } + + this.getTotal() + } + + setCantidadItems() { + + this.cont = 0; + for (let i = 0; i < this.productos.length; i++) { + this.cont += this.productos[i].cantidad; + } } deleteProducto(index) { - --this.cont; - for (let i = 0; i < this.productos.length; i++) + + for (let i = 0; i < this.productos.length; i++){ if (i === index) { + this.cont -= this.productos[i].cantidad; + //Elimina del total el precio de todo el item + this.total = this.total - (this.productos[i].precio * this.productos[i].cantidad); this.productos.splice(i, 1); - return this.productos; + return; } + } + } + clearCar(){ + let arr = []; + this.productos = arr; + this.total = 0; + this.cont = 0; } + }