2021-07-03 21:48:27 +00:00
|
|
|
import { Component } from '@angular/core';
|
|
|
|
import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
|
2021-07-17 02:18:33 +00:00
|
|
|
import { IEncuesta } from 'app/entities/encuesta/encuesta.model';
|
2021-07-17 00:23:03 +00:00
|
|
|
import { EncuestaService } from 'app/entities/encuesta/service/encuesta.service';
|
2021-07-15 02:49:19 +00:00
|
|
|
import { EstadoCategoria } from 'app/entities/enumerations/estado-categoria.model';
|
2021-07-03 21:48:27 +00:00
|
|
|
|
2021-07-17 02:20:15 +00:00
|
|
|
import { Categoria, ICategoria } from '../categoria.model';
|
2021-07-03 21:48:27 +00:00
|
|
|
import { CategoriaService } from '../service/categoria.service';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
templateUrl: './categoria-delete-dialog.component.html',
|
|
|
|
})
|
|
|
|
export class CategoriaDeleteDialogComponent {
|
|
|
|
categoria?: ICategoria;
|
2021-07-17 02:18:33 +00:00
|
|
|
encuestas?: IEncuesta[];
|
|
|
|
encuestasFiltradas?: IEncuesta[];
|
2021-07-03 21:48:27 +00:00
|
|
|
|
2021-07-17 00:23:03 +00:00
|
|
|
constructor(
|
|
|
|
protected categoriaService: CategoriaService,
|
|
|
|
protected activeModal: NgbActiveModal,
|
|
|
|
protected encuestaService: EncuestaService
|
|
|
|
) {}
|
2021-07-03 21:48:27 +00:00
|
|
|
|
|
|
|
cancel(): void {
|
|
|
|
this.activeModal.dismiss();
|
|
|
|
}
|
|
|
|
|
2021-07-15 02:39:01 +00:00
|
|
|
confirmDelete(categoria: ICategoria): void {
|
2021-07-17 02:20:54 +00:00
|
|
|
this.ensureNulaExists();
|
|
|
|
const categoriaNula = new Categoria(0, 'Otra', EstadoCategoria.ACTIVE);
|
|
|
|
this.getEncuestas(categoria);
|
|
|
|
if (this.encuestas) {
|
|
|
|
this.encuestas!.forEach(encuesta => {
|
|
|
|
encuesta.categoria = categoriaNula;
|
|
|
|
this.encuestaService.update(encuesta);
|
|
|
|
});
|
|
|
|
}
|
2021-07-15 02:49:19 +00:00
|
|
|
categoria.estado = EstadoCategoria.INACTIVE;
|
|
|
|
this.categoriaService.update(categoria).subscribe(() => {
|
2021-07-03 21:48:27 +00:00
|
|
|
this.activeModal.close('deleted');
|
|
|
|
});
|
|
|
|
}
|
2021-07-17 02:20:15 +00:00
|
|
|
ensureNulaExists(): void {
|
|
|
|
const categoriaNula = new Categoria(0, 'Otra', EstadoCategoria.ACTIVE);
|
|
|
|
const categoria = this.categoriaService.find(0);
|
|
|
|
if (categoria) {
|
|
|
|
this.categoriaService.update(categoriaNula);
|
|
|
|
} else {
|
|
|
|
this.categoriaService.create(categoriaNula);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-17 02:18:33 +00:00
|
|
|
protected getEncuestas(categoria: ICategoria): void {
|
|
|
|
this.encuestaService.query().subscribe(res => {
|
|
|
|
this.encuestas = res.body ?? [];
|
|
|
|
});
|
|
|
|
if (this.encuestas) {
|
|
|
|
this.encuestasFiltradas = this.encuestas.filter(encuesta => {
|
|
|
|
encuesta.categoria!.id === categoria.id;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2021-07-03 21:48:27 +00:00
|
|
|
}
|