Commit f8080ffa5acd1bfc2e24d5b86cf27dd0b0538a67

Authored by Marcelo Puebla
1 parent ec06683ada
Exists in master

Cambio en el servicio.

src/app/components/busqueda-productos/busqueda-productos.component.ts
1 import { Component, OnInit } from '@angular/core'; 1 import { Component, OnInit } from '@angular/core';
2 import { ProductoService } from 'src/app/services/producto.service'; 2 import { ProductoService } from 'src/app/services/producto.service';
3 import { Producto } from 'src/app/wrappers/producto'; 3 import { Producto } from 'src/app/wrappers/producto';
4 import { appSettings } from 'src/etc/AppSettings'; 4 import { appSettings } from 'src/etc/AppSettings';
5 5
6 @Component({ 6 @Component({
7 selector: 'app-busqueda-productos', 7 selector: 'app-busqueda-productos',
8 templateUrl: './busqueda-productos.component.html', 8 templateUrl: './busqueda-productos.component.html',
9 styleUrls: ['./busqueda-productos.component.scss'] 9 styleUrls: ['./busqueda-productos.component.scss']
10 }) 10 })
11 export class BusquedaProductosComponent implements OnInit { 11 export class BusquedaProductosComponent implements OnInit {
12 12
13 private productos: Producto[] = []; 13 private productos: Producto[] = [];
14 private auxProductos: Producto[] = []; 14 private auxProductos: Producto[] = [];
15 private searchTerm: string = ''; 15 private searchTerm: string = '';
16 private showSpinner: boolean = true; 16 private showSpinner: boolean = true;
17 private categoria: Categorias = Categorias.todos; 17 private categoria: Categorias = Categorias.todos;
18 private apiUrl: string = appSettings.apiUrl; 18 private apiUrl: string = appSettings.apiUrl;
19 19
20 20
21 constructor(private productoService: ProductoService) { } 21 constructor(private productoService: ProductoService) { }
22 22
23 ngOnInit() { 23 ngOnInit() {
24 24
25 this.productoService.getAll() 25 this.productoService.getAll()
26 .subscribe((data: Producto[]) => { 26 .subscribe((data: Producto[]) => {
27 27
28 this.auxProductos = this.productos = data; 28 this.auxProductos = this.productos = data;
29 }, (error) => { 29 }, (error) => {
30 this.showSpinner = false; 30 this.showSpinner = false;
31 console.error(error); 31 console.error(error);
32 }); 32 });
33 } 33 }
34 34
35 filterItems() { 35 filterItems() {
36 36
37 this.auxProductos = this.productos.filter(x => { 37 this.auxProductos = this.productos.filter(x => {
38 return x.DetArt.toLowerCase().includes(this.searchTerm.toLowerCase()) 38 return x.DetArt.toLowerCase().includes(this.searchTerm.toLowerCase())
39 }); 39 });
40 } 40 }
41 41
42 agregarAlCarrito(producto: Producto) { 42 agregarAlCarrito(producto: Producto) {
43 43
44 producto.cantidad = 1; 44 producto.cantidad = 1;
45 this.productoService.setProductos(producto); 45 this.productoService.productos.push(producto);
46 } 46 }
47 47
48 } 48 }
49 49
50 enum Categorias { 50 enum Categorias {
51 promosCombos = 1, 51 promosCombos = 1,
52 todos = 2, 52 todos = 2,
53 bebidas = 3, 53 bebidas = 3,
54 sandwicheria = 4, 54 sandwicheria = 4,
55 panaderia = 5, 55 panaderia = 5,
56 golosinas = 6, 56 golosinas = 6,
57 tabaqueria = 7, 57 tabaqueria = 7,
58 } 58 }
59 59
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
24 this.productosCarrito = this.productoService.productos;
25 }
23 26
24 ngOnInit() { 27 ngOnInit() {
25 28
26 this.productoService.productosEvent
27 .subscribe((data: Producto[]) => {
28 this.productosCarrito = data;
29 }, (error) => { console.error(error); })
30 } 29 }
31 30
32 getCantidadProductos() { 31 getCantidadProductos() {
33 32
34 for (let i = 0; i < this.productosCarrito.length; i++) { 33 for (let i = 0; i < this.productosCarrito.length; i++) {
35 this.productosCarrito[i].cantidad = 1 34 this.productosCarrito[i].cantidad = 1
36 this.cont++; 35 this.cont++;
37 } 36 }
38 return this.cont; 37 return this.cont;
39 } 38 }
40 39
41 getTotal() { 40 getTotal() {
42 41
43 let subTotal = 0; 42 let subTotal = 0;
44 for (let i = 0; i < this.productosCarrito.length; i++) { 43 for (let i = 0; i < this.productosCarrito.length; i++) {
45 subTotal = subTotal + (this.productosCarrito[i].PreVen * this.productosCarrito[i].cantidad); 44 subTotal = subTotal + (this.productosCarrito[i].PreVen * this.productosCarrito[i].cantidad);
46 } 45 }
47 return this.total = subTotal; 46 return this.total = subTotal;
48 } 47 }
49 48
50 public aumentarContador(index) { 49 public aumentarContador(index) {
51 50
52 this.cont++; 51 this.cont++;
53 for (let i = 0; i < this.productosCarrito.length; i++) { 52 for (let i = 0; i < this.productosCarrito.length; i++) {
54 if (i === index) { 53 if (i === index) {
55 this.total = this.total + this.productosCarrito[i].PreVen; 54 this.total = this.total + this.productosCarrito[i].PreVen;
56 return (this.productosCarrito[i].cantidad === this.max) ? 55 return (this.productosCarrito[i].cantidad === this.max) ?
57 this.productosCarrito[i].cantidad : this.productosCarrito[i].cantidad++; 56 this.productosCarrito[i].cantidad : this.productosCarrito[i].cantidad++;
58 57
59 } 58 }
60 } 59 }
61 } 60 }
62 61
63 decrementarContador(index) { 62 decrementarContador(index) {
64 63
65 for (let i = 0; i < this.productosCarrito.length; i++) { 64 for (let i = 0; i < this.productosCarrito.length; i++) {
66 if (i === index && this.productosCarrito[i].cantidad > this.min) { 65 if (i === index && this.productosCarrito[i].cantidad > this.min) {
67 this.productosCarrito[i].cantidad--; 66 this.productosCarrito[i].cantidad--;
68 this.cont--; 67 this.cont--;
69 break; 68 break;
70 } 69 }
71 } 70 }
72 71
73 this.getTotal() 72 this.getTotal()
74 } 73 }
75 74
76 setCantidadItems() { 75 setCantidadItems() {
77 76
78 this.cont = 0; 77 this.cont = 0;
79 for (let i = 0; i < this.productosCarrito.length; i++) { 78 for (let i = 0; i < this.productosCarrito.length; i++) {
80 this.cont += this.productosCarrito[i].cantidad; 79 this.cont += this.productosCarrito[i].cantidad;
81 } 80 }
82 } 81 }
83 82
84 deleteProducto(index: number) { 83 deleteProducto(index: number) {
85 84
86 for (let i = 0; i < this.productosCarrito.length; i++) { 85 for (let i = 0; i < this.productosCarrito.length; i++) {
87 if (i === index) { 86 if (i === index) {
88 this.cont -= this.productosCarrito[i].cantidad; 87 this.cont -= this.productosCarrito[i].cantidad;
89 //Elimina del total el PreVen de todo el item 88 //Elimina del total el PreVen de todo el item
90 this.total = this.total - (this.productosCarrito[i].PreVen * this.productosCarrito[i].cantidad); 89 this.total = this.total - (this.productosCarrito[i].PreVen * this.productosCarrito[i].cantidad);
91 this.productosCarrito.splice(i, 1); 90 this.productosCarrito.splice(i, 1);
92 return; 91 return;
93 } 92 }
94 } 93 }
95 } 94 }
96 95
97 cleanCarrito() { 96 cleanCarrito() {
98 97
99 this.productoService.productos = this.productosCarrito = []; 98 this.productoService.productos = this.productosCarrito = [];
100 this.total = 0; 99 this.total = 0;
101 this.cont = 0; 100 this.cont = 0;
src/app/services/producto.service.ts
1 import { Injectable } from '@angular/core'; 1 import { Injectable } from '@angular/core';
2 import { HttpClient } from '@angular/common/http'; 2 import { HttpClient } from '@angular/common/http';
3 import { Observable } from 'rxjs'; 3 import { Observable } from 'rxjs';
4 import { appSettings } from 'src/etc/AppSettings'; 4 import { appSettings } from 'src/etc/AppSettings';
5 import { Producto } from '../wrappers/producto'; 5 import { Producto } from '../wrappers/producto';
6 import { EventEmitter } from '@angular/core';
7 6
8 @Injectable({ 7 @Injectable({
9 providedIn: 'root' 8 providedIn: 'root'
10 }) 9 })
11 export class ProductoService { 10 export class ProductoService {
12 11
13 productos : Producto[] = []; 12 productos : Producto[] = [];
14 productosEvent: EventEmitter<Producto[]> = new EventEmitter();
15 13
16 constructor(private http: HttpClient) { } 14 constructor(private http: HttpClient) { }
17 15
18 getAll(): Observable<any> { 16 getAll(): Observable<any> {
19 17
20 return this.http.get(`${appSettings.apiUrl}/articulos`); 18 return this.http.get(`${appSettings.apiUrl}/articulos`);
21 } 19 }
22 20
23 setProductos(producto : Producto){ 21 setProductos(producto : Producto){
24 22
25 this.productos.push(producto); 23 this.productos.push(producto);
26 this.productosEvent.emit(this.productos);
27 } 24 }
28 25
29 } 26 }
30 27