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

implementada funcionalidad de que la cesta se guarde en el back-end para cada...

implementada funcionalidad de que la cesta se guarde en el back-end para cada accion que haga el usuario para no perder su estado.
parent b94336ee
Branches
No related tags found
No related merge requests found
import { Component, OnDestroy, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { Subscription } from 'rxjs';
import { Cesta } from 'src/app/shared/interfaces/cesta.model';
import { Pedido } from 'src/app/shared/interfaces/pedido.model';
import { Usuario } from 'src/app/shared/interfaces/usuario';
import { AuthService } from 'src/app/shared/services/auth.service';
......@@ -46,5 +47,6 @@ export class CompletarPedidoPagoComponent implements OnInit, OnDestroy {
this.pedido,
this.authService.credential.user.uid
);
this.cestaService.clearCestas();
}
}
......@@ -31,13 +31,13 @@ export class HeaderComponent implements OnInit, OnDestroy {
this.rutaCuenta = '/auth';
} else {
this.rutaCuenta = '/perfil';
console.log('el header tiene user');
}
},
() => {}
);
this.cestaSubscription = this.cestaService.cestaBS.subscribe((cesta) => {
this.cesta = cesta;
this.productosTotales = 0;
cesta.productos.forEach((p) => {
this.productosTotales += p.unidades;
......
......@@ -34,7 +34,17 @@ export class AuthService {
if (!userLogged) {
return;
}
console.log(userLogged);
this.auth.onAuthStateChanged((user) => {
if (user) {
console.log('idToken');
getIdToken(user);
} else {
console.log('logout');
this.logOut();
}
});
}
public loginUserWithEmail(
......
......@@ -12,6 +12,7 @@ import { DatabaseService } from './database.service';
})
export class CestaCompraService implements OnDestroy {
private userSubscription: Subscription;
private user: Usuario;
public cestaBS = new BehaviorSubject<Cesta>(new Cesta());
private cesta: Cesta = new Cesta();
private pedido: Pedido = null;
......@@ -25,6 +26,10 @@ export class CestaCompraService implements OnDestroy {
) {
this.userSubscription = this.authService.userBS.subscribe(
(user: Usuario) => {
if (user === null) {
return;
}
this.user = user;
this.cesta.productos = [];
user.cesta.map(
(pe) => {
......@@ -32,7 +37,10 @@ export class CestaCompraService implements OnDestroy {
.getProductoFromDatabase(pe.productoID)
.pipe(take(1))
.subscribe((p) => {
this.addProducto(p, pe.unidades);
this.cesta.productos.push({
producto: p,
unidades: pe.unidades,
});
this.cestaBS.next(this.cesta);
});
},
......@@ -55,6 +63,7 @@ export class CestaCompraService implements OnDestroy {
let index = this.cesta.productos.findIndex(
(e) => e.producto.productoID === producto.productoID
);
if (index > -1) {
this.cesta.productos[index].unidades =
this.cesta.productos[index].unidades + unidades;
......@@ -64,8 +73,29 @@ export class CestaCompraService implements OnDestroy {
unidades: unidades,
});
}
localStorage.removeItem('cesta');
localStorage.setItem('cesta', JSON.stringify(this.cesta));
if (this.user) {
let index2 = this.user.cesta.findIndex(
(e) => e.productoID === producto.productoID
);
if (index2 > -1) {
this.user.cesta[index2].unidades =
this.user.cesta[index2].unidades + unidades;
} else {
this.user.cesta.push({
productoID: producto.productoID,
unidades: unidades,
});
}
localStorage.removeItem('userLogged');
localStorage.setItem('userLogged', JSON.stringify(this.user));
this.authService.userBS.next(this.user);
this.databaseService
.updateUserInDatabase(this.user, this.authService.credential.user.uid)
.subscribe()
.unsubscribe();
console.log(index2);
}
this.cestaBS.next(this.cesta);
}
deleteProducto(productoID: string) {
......@@ -75,6 +105,20 @@ export class CestaCompraService implements OnDestroy {
this.cesta.productos.splice(index, 1);
this.cestaBS.next(this.cesta);
if (this.user) {
let index2 = this.user.cesta.findIndex(
(e) => e.productoID === productoID
);
this.user.cesta.splice(index2, 1);
}
localStorage.removeItem('userLogged');
localStorage.setItem('userLogged', JSON.stringify(this.user));
this.authService.userBS.next(this.user);
this.databaseService
.updateUserInDatabase(this.user, this.authService.credential.user.uid)
.subscribe()
.unsubscribe();
}
modificarCantidadProducto(entrada: { producto: Producto; unidades: number }) {
......@@ -84,6 +128,23 @@ export class CestaCompraService implements OnDestroy {
this.cesta.productos[index] = entrada;
this.cestaBS.next(this.cesta);
if (this.user) {
let index2 = this.user.cesta.findIndex(
(e) => e.productoID === entrada.producto.productoID
);
this.user.cesta[index2] = {
productoID: entrada.producto.productoID,
unidades: entrada.unidades,
};
}
localStorage.removeItem('userLogged');
localStorage.setItem('userLogged', JSON.stringify(this.user));
this.authService.userBS.next(this.user);
this.databaseService
.updateUserInDatabase(this.user, this.authService.credential.user.uid)
.subscribe()
.unsubscribe();
}
getFechaEntregaPrevistaPedido(): string {
const options: Intl.DateTimeFormatOptions = {
......@@ -134,4 +195,14 @@ export class CestaCompraService implements OnDestroy {
this.pedido = pedido;
this.pedidoBS.next(pedido);
}
clearCestas() {
this.cesta = new Cesta();
this.cestaBS.next(this.cesta);
this.user.cesta = [];
this.authService.userBS.next(this.user);
this.databaseService
.updateUserInDatabase(this.user, this.authService.credential.user.uid)
.subscribe()
.unsubscribe();
}
}
......@@ -43,9 +43,7 @@ export class DatabaseService {
direccionEnvio: user.direccionEnvio,
direccionFacturacion: user.direccionFacturacion,
metodosPago: [],
cesta: {
productos: [],
},
cesta: { productoID: '', unidades: 0 }[0],
})
.then(
(response) => {
......@@ -69,9 +67,7 @@ export class DatabaseService {
direccionEnvio: user.direccionEnvio,
direccionFacturacion: user.direccionFacturacion,
metodosPago: [],
cesta: {
productos: [],
},
cesta: user.cesta,
});
return from(updatePromise);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment