Skip to content
Snippets Groups Projects
Commit 1a8360b0 authored by paborte's avatar paborte
Browse files

Modificada la forma de añadir productos a la cesta, para añadir productos y no...

Modificada la forma de añadir productos a la cesta, para añadir productos y no solo su id de producto
parent 6fe44c96
No related branches found
No related tags found
No related merge requests found
...@@ -55,11 +55,8 @@ ...@@ -55,11 +55,8 @@
<!-- Zona de carga de productos de la cesta de compra --> <!-- Zona de carga de productos de la cesta de compra -->
<div *ngIf="cesta.productos.length > 0"> <div *ngIf="cesta.productos.length > 0">
<div class="border" *ngFor="let p of productosEnCesta; let i = index"> <div class="border" *ngFor="let e of cesta.productos">
<app-item-cesta <app-item-cesta [entradaCesta]="e"></app-item-cesta>
[producto]="p"
[entradaCesta]="cesta.productos[i]"
></app-item-cesta>
</div> </div>
</div> </div>
<!-- Fin zona carga productos --> <!-- Fin zona carga productos -->
... ...
......
...@@ -17,7 +17,6 @@ export class CestaCompraComponent implements OnInit, OnDestroy { ...@@ -17,7 +17,6 @@ export class CestaCompraComponent implements OnInit, OnDestroy {
diasTipicosParaUnEnvio = 4; diasTipicosParaUnEnvio = 4;
cestaSubscription: Subscription; cestaSubscription: Subscription;
cesta: Cesta; cesta: Cesta;
productosEnCesta: Producto[];
sumaPreciosProductos = 0; sumaPreciosProductos = 0;
gastosDeEnvio = 5.95; gastosDeEnvio = 5.95;
...@@ -33,14 +32,9 @@ export class CestaCompraComponent implements OnInit, OnDestroy { ...@@ -33,14 +32,9 @@ export class CestaCompraComponent implements OnInit, OnDestroy {
this.cesta = cesta; this.cesta = cesta;
console.log(cesta); console.log(cesta);
this.sumaPreciosProductos = 0; this.sumaPreciosProductos = 0;
this.productosEnCesta = [];
this.cesta.productos.forEach((p) => { this.cesta.productos.forEach((p) => {
this.databaseService this.sumaPreciosProductos += p.producto.precio * p.unidades;
.getProductoFromDatabase(p.productoID)
.subscribe((productoDB) => {
this.productosEnCesta.push(productoDB);
this.sumaPreciosProductos += productoDB.precio * p.unidades;
});
}); });
}, },
() => {} () => {}
...@@ -63,12 +57,6 @@ export class CestaCompraComponent implements OnInit, OnDestroy { ...@@ -63,12 +57,6 @@ export class CestaCompraComponent implements OnInit, OnDestroy {
ngOnDestroy(): void { ngOnDestroy(): void {
this.cestaSubscription.unsubscribe(); this.cestaSubscription.unsubscribe();
} }
eliminarProducto(productoID: string) {
this.cestaService.deleteProducto(productoID);
if (this.cesta.productos.length === 0) {
this.cesta = null;
}
}
iniciarPedido() {} iniciarPedido() {}
} }
...@@ -3,19 +3,28 @@ ...@@ -3,19 +3,28 @@
<div class="col-12"> <div class="col-12">
<div class="pt-3 pb-3 border d-flex"> <div class="pt-3 pb-3 border d-flex">
<div class="col-4 d-flex flex-column align-items-center"> <div class="col-4 d-flex flex-column align-items-center">
<a [routerLink]="['/tienda/producto', producto.productoID]" <a
[routerLink]="[
'/tienda/producto',
entradaCesta.producto.productoID
]"
><img [src]="imagen | async" class="imgAux ms-3" ><img [src]="imagen | async" class="imgAux ms-3"
/></a> /></a>
</div> </div>
<div class="ms-3 col-8 d-flex flex-column"> <div class="ms-3 col-8 d-flex flex-column">
<a <a
[routerLink]="['/tienda/producto', producto.productoID]" [routerLink]="[
'/tienda/producto',
entradaCesta.producto.productoID
]"
class="text-dark text-decoration-none" class="text-dark text-decoration-none"
><h6>{{ producto.nombre }}</h6></a ><h6>{{ entradaCesta.producto.nombre }}</h6></a
> >
<h6 class=""> <h6 class="">
<label for="" class="verde-tienda">{{ producto.precio }} €</label> <label for="" class="verde-tienda"
>{{ entradaCesta.producto.precio }} €</label
>
<label for="" class="ms-2">/ Unidad</label> <label for="" class="ms-2">/ Unidad</label>
</h6> </h6>
<div class="row"> <div class="row">
...@@ -23,7 +32,7 @@ ...@@ -23,7 +32,7 @@
<label for="unidades">Unidades en la cesta</label> <label for="unidades">Unidades en la cesta</label>
<input <input
type="number" type="number"
[name]="producto.productoID" [name]="entradaCesta.producto.productoID"
min="1" min="1"
class="ms-2" class="ms-2"
style="max-width: 4rem" style="max-width: 4rem"
... ...
......
...@@ -11,8 +11,7 @@ import { CestaCompraService } from 'src/app/shared/services/cesta-compra.service ...@@ -11,8 +11,7 @@ import { CestaCompraService } from 'src/app/shared/services/cesta-compra.service
styleUrls: ['./item-cesta.component.css'], styleUrls: ['./item-cesta.component.css'],
}) })
export class ItemCestaComponent implements OnInit { export class ItemCestaComponent implements OnInit {
@Input('producto') producto: Producto; @Input('entradaCesta') entradaCesta: { producto: Producto; unidades: number };
@Input('entradaCesta') entradaCesta: { productoID: string; unidades: number };
imagen; imagen;
rutaProducto: string; rutaProducto: string;
botonCambiarUnidades: boolean = false; botonCambiarUnidades: boolean = false;
...@@ -24,14 +23,15 @@ export class ItemCestaComponent implements OnInit { ...@@ -24,14 +23,15 @@ export class ItemCestaComponent implements OnInit {
) {} ) {}
ngOnInit(): void { ngOnInit(): void {
const ref = this.storage.ref(this.producto.imagenIconoPath); const ref = this.storage.ref(this.entradaCesta.producto.imagenIconoPath);
this.imagen = ref.getDownloadURL(); this.imagen = ref.getDownloadURL();
console.log(this.entradaCesta); console.log(this.entradaCesta);
this.rutaProducto = '/tienda/producto' + this.producto.productoID; this.rutaProducto =
'/tienda/producto' + this.entradaCesta.producto.productoID;
} }
eliminarProducto() { eliminarProducto() {
this.cestaService.deleteProducto(this.producto.productoID); this.cestaService.deleteProducto(this.entradaCesta.producto.productoID);
} }
mostrarBotonCambiarUnidades() { mostrarBotonCambiarUnidades() {
this.botonCambiarUnidades = true; this.botonCambiarUnidades = true;
...@@ -39,7 +39,7 @@ export class ItemCestaComponent implements OnInit { ...@@ -39,7 +39,7 @@ export class ItemCestaComponent implements OnInit {
modificarCantidad() { modificarCantidad() {
this.botonCambiarUnidades = false; this.botonCambiarUnidades = false;
this.cestaService.modificarCantidadProducto({ this.cestaService.modificarCantidadProducto({
productoID: this.producto.productoID, producto: this.entradaCesta.producto,
unidades: this.entradaCesta.unidades, unidades: this.entradaCesta.unidades,
}); });
} }
... ...
......
import { Producto } from './producto.model';
export class Cesta { export class Cesta {
productos: { productos: {
productoID: string; producto: Producto;
unidades: number; unidades: number;
}[] /* Array con pares producto-unidades para saber los productos que hay en la cesta y las unidades de cada uno */; }[] /* Array con pares producto-unidades para saber los productos que hay en la cesta y las unidades de cada uno */;
constructor() { constructor() {
... ...
......
...@@ -3,6 +3,7 @@ import { BehaviorSubject } from 'rxjs'; ...@@ -3,6 +3,7 @@ import { BehaviorSubject } from 'rxjs';
import { PedidosComponent } from 'src/app/mi-cuenta/perfil-cuenta/pedidos/pedidos.component'; import { PedidosComponent } from 'src/app/mi-cuenta/perfil-cuenta/pedidos/pedidos.component';
import { Cesta } from '../interfaces/cesta.model'; import { Cesta } from '../interfaces/cesta.model';
import { Pedido } from '../interfaces/pedido.model'; import { Pedido } from '../interfaces/pedido.model';
import { Producto } from '../interfaces/producto.model';
import { DatabaseService } from './database.service'; import { DatabaseService } from './database.service';
@Injectable({ @Injectable({
...@@ -15,16 +16,16 @@ export class CestaCompraService implements OnInit { ...@@ -15,16 +16,16 @@ export class CestaCompraService implements OnInit {
constructor(private databaseService: DatabaseService) {} constructor(private databaseService: DatabaseService) {}
ngOnInit(): void {} ngOnInit(): void {}
addProducto(productoID: string, unidades: number) { addProducto(producto: Producto, unidades: number) {
let index = this.cesta.productos.findIndex( let index = this.cesta.productos.findIndex(
(p) => p.productoID === productoID (e) => e.producto.productoID === producto.productoID
); );
if (index > -1) { if (index > -1) {
this.cesta.productos[index].unidades = this.cesta.productos[index].unidades =
this.cesta.productos[index].unidades + unidades; this.cesta.productos[index].unidades + unidades;
} else { } else {
this.cesta.productos.push({ this.cesta.productos.push({
productoID: productoID, producto: producto,
unidades: unidades, unidades: unidades,
}); });
console.log('en addproducto'); console.log('en addproducto');
...@@ -37,7 +38,7 @@ export class CestaCompraService implements OnInit { ...@@ -37,7 +38,7 @@ export class CestaCompraService implements OnInit {
} }
deleteProducto(productoID: string) { deleteProducto(productoID: string) {
let index = this.cesta.productos.findIndex( let index = this.cesta.productos.findIndex(
(p) => p.productoID === productoID (p) => p.producto.productoID === productoID
); );
let cestaEmit = new Cesta(); let cestaEmit = new Cesta();
...@@ -46,17 +47,14 @@ export class CestaCompraService implements OnInit { ...@@ -46,17 +47,14 @@ export class CestaCompraService implements OnInit {
this.cestaBS.next(cestaEmit); this.cestaBS.next(cestaEmit);
} }
modificarCantidadProducto(producto: { modificarCantidadProducto(entrada: { producto: Producto; unidades: number }) {
productoID: string;
unidades: number;
}) {
let index = this.cesta.productos.findIndex( let index = this.cesta.productos.findIndex(
(p) => p.productoID === producto.productoID (e) => e.producto.productoID === entrada.producto.productoID
); );
let cestaEmit = new Cesta(); let cestaEmit = new Cesta();
cestaEmit.productos = this.cesta.productos; cestaEmit.productos = this.cesta.productos;
cestaEmit.productos[index] = producto; cestaEmit.productos[index] = entrada;
this.cestaBS.next(cestaEmit); this.cestaBS.next(cestaEmit);
} }
... ...
......
...@@ -22,6 +22,6 @@ export class CardProductoComponent implements OnInit { ...@@ -22,6 +22,6 @@ export class CardProductoComponent implements OnInit {
this.imagen = ref.getDownloadURL(); this.imagen = ref.getDownloadURL();
} }
addCesta() { addCesta() {
this.cestaService.addProducto(this.producto.productoID, 1); this.cestaService.addProducto(this.producto, 1);
} }
} }
...@@ -41,9 +41,6 @@ export class ProductoComponent implements OnInit { ...@@ -41,9 +41,6 @@ export class ProductoComponent implements OnInit {
return ref.getDownloadURL(); return ref.getDownloadURL();
} }
addCesta() { addCesta() {
this.cestaService.addProducto( this.cestaService.addProducto(this.producto, this.cantidadAIncluirEnCesta);
this.producto.productoID,
this.cantidadAIncluirEnCesta
);
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment