Commit bb81921ea490cd6a6c3163181756c2fa85aebac7
1 parent
8c519ea534
Exists in
master
Agregada logica para cargar muchas imagenes y para hacer delete
Showing
1 changed file
with
41 additions
and
15 deletions
Show diff stats
src/app/components/amb-imagenes/amb-imagenes.component.ts
| 1 | import { Component, OnInit } from '@angular/core'; | 1 | import { Component, OnInit } from '@angular/core'; |
| 2 | import { appSettings } from 'src/etc/AppSettings'; | 2 | import { appSettings } from 'src/etc/AppSettings'; |
| 3 | import { ProductoService } from 'src/app/services/producto.service'; | 3 | import { ProductoService } from 'src/app/services/producto.service'; |
| 4 | import { Producto } from 'src/app/wrappers/producto'; | 4 | import { Producto } from 'src/app/wrappers/producto'; |
| 5 | import { HttpClient } from '@angular/common/http'; | 5 | import { HttpClient } from '@angular/common/http'; |
| 6 | 6 | ||
| 7 | @Component({ | 7 | @Component({ |
| 8 | selector: 'app-amb-imagenes', | 8 | selector: 'app-amb-imagenes', |
| 9 | templateUrl: './amb-imagenes.component.html', | 9 | templateUrl: './amb-imagenes.component.html', |
| 10 | styleUrls: ['./amb-imagenes.component.scss'] | 10 | styleUrls: ['./amb-imagenes.component.scss'] |
| 11 | }) | 11 | }) |
| 12 | 12 | ||
| 13 | export class AmbImagenesComponent implements OnInit { | 13 | export class AmbImagenesComponent implements OnInit { |
| 14 | 14 | ||
| 15 | apiUrl = appSettings.apiUrl; | 15 | apiUrl = appSettings.apiUrl; |
| 16 | articulos: Producto[] = []; | 16 | articulos: Producto[] = []; |
| 17 | private auxProductos: Producto[] = []; | 17 | private auxProductos: Producto[] = []; |
| 18 | private searchTerm: string = ''; | 18 | private searchTerm: string = ''; |
| 19 | 19 | ||
| 20 | constructor(private productoService: ProductoService, private http: HttpClient) {} | 20 | constructor(private productoService: ProductoService, private http: HttpClient) {} |
| 21 | 21 | ||
| 22 | ngOnInit() { | 22 | ngOnInit() { |
| 23 | 23 | ||
| 24 | this.productoService.getAll().subscribe((productos: Producto[]) => { | 24 | this.productoService.getAll().subscribe((productos: Producto[]) => { |
| 25 | this.articulos = productos; | 25 | this.articulos = productos; |
| 26 | this.filterItems(); | 26 | this.filterItems(); |
| 27 | }); | 27 | }); |
| 28 | } | 28 | } |
| 29 | 29 | ||
| 30 | onFileSelected(event, articulo) { | 30 | this.articulos = productos; |
| 31 | this.filterItems(); | ||
| 32 | }, error => console.error(error)); | ||
| 33 | } | ||
| 31 | 34 | ||
| 32 | let file: File = event.target.files[0]; | 35 | onFileSelected(event, articulo: Producto) { |
| 33 | 36 | ||
| 34 | this.onLoad(file) | 37 | let auxFiles: FileList = event.target.files; |
| 35 | .then(result => { | 38 | Array.from(auxFiles).forEach(file => { |
| 36 | 39 | ||
| 37 | articulo.imagenes = []; | 40 | this.onLoad(file) |
| 41 | .then(result => { | ||
| 38 | 42 | ||
| 39 | document.getElementById(articulo.CodSec + articulo.CodArt)['src'] = result; | 43 | articulo.imagenes.push({ |
| 44 | name: file.name, | ||
| 45 | fromGallery: true, | ||
| 46 | imagen: result, | ||
| 47 | id_articulo: articulo.id | ||
| 48 | }); | ||
| 40 | 49 | ||
| 41 | this.saveInBase({ | 50 | this.saveInBase({ |
| 42 | name: file.name, | 51 | name: file.name, |
| 43 | base64: result, | 52 | base64: result, |
| 44 | codigo: articulo.CodArt, | 53 | codigo: articulo.CodArt, |
| 45 | sector: articulo.CodSec | 54 | sector: articulo.CodSec |
| 55 | }); | ||
| 46 | }); | 56 | }); |
| 47 | }); | 57 | }) |
| 48 | } | 58 | } |
| 49 | 59 | ||
| 50 | filterItems() { | 60 | filterItems() { |
| 51 | 61 | ||
| 52 | this.auxProductos = this.articulos.filter(x => { | 62 | this.auxProductos = this.articulos.filter(x => { |
| 53 | return x.DET_LAR.toLowerCase().includes(this.searchTerm.toLowerCase()) | 63 | return x.DET_LAR.toLowerCase().includes(this.searchTerm.toLowerCase()) |
| 54 | }); | 64 | }); |
| 55 | } | 65 | } |
| 56 | 66 | ||
| 57 | 67 | ||
| 58 | saveInBase(img) { | 68 | saveInBase(img) { |
| 59 | 69 | ||
| 60 | this.http.post(`${appSettings.apiUrl}/imagenes/guardar`, img) | 70 | this.productoService.saveInBase(img) |
| 61 | .subscribe(data => { | 71 | .subscribe(data => { |
| 62 | console.log(data); | 72 | |
| 63 | }); | 73 | }, error => console.error(error)); |
| 64 | } | 74 | } |
| 65 | 75 | ||
| 66 | onLoad(file) { | 76 | onLoad(file) { |
| 67 | 77 | ||
| 68 | return new Promise((resolve, reject) => { | 78 | return new Promise((resolve, reject) => { |
| 69 | 79 | ||
| 70 | var fr = new FileReader(); | 80 | var fr = new FileReader(); |
| 71 | 81 | ||
| 72 | fr.onload = function() { | 82 | fr.onload = function() { |
| 73 | 83 | ||
| 74 | resolve(fr.result); | 84 | resolve(fr.result); |
| 75 | }; | 85 | }; |
| 76 | 86 | ||
| 77 | fr.readAsDataURL(file); | 87 | fr.readAsDataURL(file); |
| 78 | }); | 88 | }); |
| 79 | 89 | ||
| 80 | } | 90 | } |
| 91 | |||
| 92 | deleteImage(imagenes, index: number) { | ||
| 93 | |||
| 94 | if (!imagenes[index].name) { | ||
| 95 | imagenes[index].name = imagenes[index].imagen; | ||
| 96 | } | ||
| 97 | |||
| 98 | this.productoService.deleteImage(imagenes[index]) | ||
| 99 | .subscribe(res => { | ||
| 100 | |||
| 101 | if (res) { | ||
| 102 | imagenes.splice(index, 1); | ||
| 103 | } | ||
| 104 | }, error => console.error(error)); | ||
| 105 | } | ||
| 106 | |||
| 81 | } | 107 | } |
| 82 | 108 |