Commit bd8d9956da7cebf322ace7d369fa3e4b3fa7c3fd

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

Cambio en seteo de informacion al sidebar.

src/app/components/busqueda-productos/busqueda-productos.component.html
... ... @@ -83,7 +83,7 @@
83 83 type="button"
84 84 class="btn btn-light btn-lg shadow-sm"
85 85 [routerLink]="['/inicio']">
86   - <span class="font-weight-normal h6">Volver&nbsp;&nbsp;</span>
  86 + <span class="font-weight-normal h6 pr-2">Volver</span>
87 87 <i class="fa fa-undo text-warning" aria-hidden="true"></i>
88 88 </button>
89 89 </div>
src/app/components/busqueda-productos/busqueda-productos.component.ts
1   -import { Component, OnInit, Input, ViewChild, AfterViewInit } from '@angular/core';
  1 +import { Component, OnInit } from '@angular/core';
2 2 import { ProductoService } from 'src/app/services/producto.service';
3 3 import { Producto } from 'src/app/wrappers/producto';
4 4 import { appSettings } from 'src/etc/AppSettings';
5   -import { SidebarComponent } from '../sidebar/sidebar.component';
6 5  
7 6 @Component({
8 7 selector: 'app-busqueda-productos',
... ... @@ -18,14 +17,9 @@ export class BusquedaProductosComponent implements OnInit {
18 17 private categoria: Categorias = Categorias.todos;
19 18 private apiUrl: string = appSettings.apiUrl;
20 19  
21   - productosParaCarrito : Producto[] = [];
22 20  
23 21 constructor(private productoService: ProductoService) { }
24 22  
25   - // ngAfterViewInit() {
26   - // this.sideBar.productos = [];
27   - // }
28   -
29 23 ngOnInit() {
30 24  
31 25 this.productoService.getAll()
... ... @@ -39,14 +33,15 @@ export class BusquedaProductosComponent implements OnInit {
39 33 }
40 34  
41 35 filterItems() {
  36 +
42 37 this.auxProductos = this.productos.filter(x => {
43 38 return x.DetArt.toLowerCase().includes(this.searchTerm.toLowerCase())
44 39 });
45 40 }
46 41  
47 42 agregarAlCarrito(producto: Producto) {
48   - producto.cantidad = 1;
49   - this.productosParaCarrito.push(producto);
  43 +
  44 + this.productoService.setProductos(producto);
50 45 }
51 46  
52 47 }
src/app/components/sidebar/sidebar.component.ts
1   -import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
  1 +import { Component, OnInit } from '@angular/core';
2 2 import { Producto } from 'src/app/wrappers/producto';
3 3 import { appSettings } from 'src/etc/AppSettings';
  4 +import { ProductoService } from 'src/app/services/producto.service';
4 5  
5 6  
6 7 @Component({
7 8 selector: 'app-sidebar',
8 9 templateUrl: './sidebar.component.html',
9 10 styleUrls: ['./sidebar.component.scss'],
10   - inputs: ['productosCarrito']
11 11 })
12 12 export class SidebarComponent implements OnInit {
13 13  
... ... @@ -19,9 +19,15 @@ export class SidebarComponent implements OnInit {
19 19  
20 20 public productosCarrito: Producto[] = [];
21 21  
22   - constructor() { }
  22 + constructor(private productoService: ProductoService) { }
23 23  
24   - ngOnInit() { }
  24 + ngOnInit() {
  25 +
  26 + this.productoService.productosEvent
  27 + .subscribe((data: Producto[]) => {
  28 + this.productosCarrito = data;
  29 + }, (error) => { console.error(error); })
  30 + }
25 31  
26 32 getCantidadProductos() {
27 33  
... ... @@ -57,7 +63,7 @@ export class SidebarComponent implements OnInit {
57 63 decrementarContador(index) {
58 64  
59 65 for (let i = 0; i < this.productosCarrito.length; i++) {
60   - if (i === index && this.productosCarrito[i].cantidad > 1) {
  66 + if (i === index && this.productosCarrito[i].cantidad > this.min) {
61 67 this.productosCarrito[i].cantidad--;
62 68 this.cont--;
63 69 break;
... ... @@ -75,7 +81,7 @@ export class SidebarComponent implements OnInit {
75 81 }
76 82 }
77 83  
78   - deleteProducto(index) {
  84 + deleteProducto(index: number) {
79 85  
80 86 for (let i = 0; i < this.productosCarrito.length; i++) {
81 87 if (i === index) {
src/app/services/producto.service.ts
... ... @@ -2,16 +2,28 @@ import { Injectable } from &#39;@angular/core&#39;;
2 2 import { HttpClient } from '@angular/common/http';
3 3 import { Observable } from 'rxjs';
4 4 import { appSettings } from 'src/etc/AppSettings';
  5 +import { Producto } from '../wrappers/producto';
  6 +import { EventEmitter } from '@angular/core';
5 7  
6 8 @Injectable({
7 9 providedIn: 'root'
8 10 })
9 11 export class ProductoService {
10 12  
  13 + productos : Producto[] = [];
  14 + productosEvent: EventEmitter<Producto[]> = new EventEmitter();
  15 +
11 16 constructor(private http: HttpClient) { }
12 17  
13 18 getAll(): Observable<any> {
  19 +
14 20 return this.http.get(`${appSettings.apiUrl}/articulos`);
15 21 }
16 22  
  23 + setProductos(producto){
  24 +
  25 + this.productos.push(producto);
  26 + this.productosEvent.emit(this.productos);
  27 + }
  28 +
17 29 }