ahora tengo este array que se llena de datos @Component({
selector: 'app-nav',
templateUrl: './nav.component.html',
styleUrl: './nav.component.css',
})
export class NavComponent implements OnInit {
formCliente!: FormGroup;
dvCarro: DetalleVenta[] = [];
imagenesMap = new Map<number, string>();
numeroItemsCarro: number = 0;
constructor(private ventaService: VentaService, private productoService: ProductoService, private fb: FormBuilder) {}
ngOnInit(): void {
this.formulario();
this.mostrarProductosCarroCompra();
this.ventaService.itemsCarroCompra.subscribe(() => {
this.mostrarProductosCarroCompra();
});
}
mostrarProductosCarroCompra(): void {
this.ventaService.mostrarCarroCompra().subscribe({
next: (data: DetalleVenta[]) => {
this.dvCarro = data;
this.numeroItemsCarro = this.dvCarro.length;
this.obtenerImagenesProductos(this.dvCarro);
},
error: (error) => {
console.error('Error al mostrar el carro de compra', error);
},
});
}
obtenerImagenesProductos(detallesVenta: DetalleVenta[]): void {
detallesVenta.forEach((detalleVenta) => {
const producto = detalleVenta.producto;
if (!this.imagenesMap.has(producto.idproducto)) {
this.productoService.imagenProducto(producto.imagen).subscribe({
next: (data: Blob) => {
const objectURL = URL.createObjectURL(data);
this.imagenesMap.set(producto.idproducto, objectURL);
},
error: (error) => {
console.error('Error al obtener la imagen', error);
},
});
}
});
}
establecerImagen(producto: Producto | null): string {
if (!producto || !producto.idproducto || !this.imagenesMap.has(producto.idproducto)) {
return 'ERROR';
}
return this.imagenesMap.get(producto.idproducto) || 'ERROR';
}
quitarProducto(idproducto: number): void {
this.ventaService.quitarProducto(idproducto).subscribe({
next: () => {
this.mostrarProductosCarroCompra();
},
error: (error) => {
console.error('Error al quitar producto', error);
},
});
}
formulario(cliente?: Cliente): void {
this.formCliente = this.fb.group({
apellido: [
cliente ? cliente.apellido : '',
[
Validators.required,
Validators.minLength(2),
Validators.maxLength(50),
],
],
nombre: [
cliente ? cliente.nombre : '',
[
Validators.required,
Validators.minLength(3),
Validators.maxLength(50),
],
],
dni: [
cliente ? cliente.dni : '',
[Validators.required, Validators.pattern(/^\d{8}$/)],
],
direccion: [cliente ? cliente.direccion : ''],
telefono: [cliente ? cliente.telefono : ''],
});
['direccion', 'telefono'].forEach(
(campo) =>
this.formCliente.get(campo)?.clearValidators() &&
this.formCliente.get(campo)?.updateValueAndValidity()
);
}
checkControlClass(controlName: string): string {
const control = this.formCliente.get(controlName);
if (control?.invalid && (control.dirty || control.touched)) {
return 'is-invalid';
}
if (control?.valid && (control.dirty || control.touched)) {
return 'is-valid';
}
return '';
}
mostrarMensaje(datoInput: string): string {
const tipoDato = this.formCliente.get(datoInput);
if (tipoDato?.hasError('required')) {
return 'Este campo es requerido.';
}
if (tipoDato?.hasError('minlength')) {
return ;
}
if (tipoDato?.hasError('maxlength')) {
return ;
}
if (tipoDato?.hasError('pattern')) {
return ;
}
return '';
}