sinonimo.component.ts
2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { Component, OnInit } from '@angular/core';
import { ISinonimo } from 'src/app/interfaces/ISinonimo';
import { IArticulo } from 'src/app/interfaces/IArticulo';
import { BsModalRef } from 'ngx-bootstrap/modal';
import { Subject, forkJoin } from 'rxjs';
import { ArticuloService } from 'src/app/services/articulo/articulo.service';
import { element } from 'protractor';
@Component({
selector: 'app-sinonimo',
templateUrl: './sinonimo.component.html',
styleUrls: ['./sinonimo.component.scss']
})
export class SinonimoComponent implements OnInit {
sinonimos: ISinonimo[] = [];
isValid: boolean;
onClose: Subject<any>;
articulo: IArticulo;
isSinonimoSelected = false;
currentIndex = 0;
constructor(
private modalRef: BsModalRef,
private articuloService: ArticuloService,
) {
this.onClose = new Subject();
}
ngOnInit() {
for (const s of this.sinonimos) {
for (const a of s.productos) {
a.cantidad = 0;
}
}
}
validate() {
this.isValid = true;
for (const s of this.sinonimos) {
if (s.cantidadRestante > 0) {
this.isValid = false;
break;
}
}
return !this.isValid ? 'disabled' : 'btn-effect';
}
validateNext() {
const sinonimo = this.sinonimos[this.currentIndex]
sinonimo.selected = (sinonimo.cantidadRestante > 0) ? false : true;
return !sinonimo.selected ? 'disabled' : 'btn-effect';
}
goNext() {
if (!this.sinonimos[this.currentIndex].selected) return;
this.currentIndex++;
}
continue() {
if (!this.isValid) return;
if (this.isSinonimoSelected) return;
this.isSinonimoSelected = true;
const observables = [];
const cantidades = [];
for (const s of this.sinonimos) {
for (const articulo of s.productos) {
if (articulo.cantidad === 0) continue;
cantidades.push(articulo.cantidad);
observables.push(this.articuloService.getById(articulo.id));
}
}
forkJoin(observables)
.subscribe((res: IArticulo[]) => {
res.forEach((articulo, i) => {
articulo.cantidad = cantidades[i];
});
this.modalRef.hide();
this.onClose.next({
articulos: res,
});
}, err => console.error(err));
}
sumarCantidadSinonimo(articulo: IArticulo, i: number) {
if (this.sinonimos[i].cantidadRestante === 0) return;
articulo.cantidad++;
this.sinonimos[i].cantidadRestante--;
}
restarCantidadSinonimo(articulo: IArticulo, i: number) {
if (this.sinonimos[i].cantidadRestante === this.sinonimos[i].cantidad) return;
if (articulo.cantidad === 0) return;
articulo.cantidad--;
this.sinonimos[i].cantidadRestante++;
}
scrollTo(index: number) {
const el = document.getElementById(index.toString());
el.scrollIntoView({ behavior: 'smooth' });
}
close() {
this.modalRef.hide();
this.onClose.next();
}
}