diff --git a/src/main/webapp/app/entities/categoria/update/categoria-update.component.ts b/src/main/webapp/app/entities/categoria/update/categoria-update.component.ts index b5d24ae..45f8a09 100644 --- a/src/main/webapp/app/entities/categoria/update/categoria-update.component.ts +++ b/src/main/webapp/app/entities/categoria/update/categoria-update.component.ts @@ -14,6 +14,7 @@ import { CategoriaService } from '../service/categoria.service'; }) export class CategoriaUpdateComponent implements OnInit { isSaving = false; + public categorias?: ICategoria[]; editForm = this.fb.group({ id: [], @@ -21,12 +22,22 @@ export class CategoriaUpdateComponent implements OnInit { estado: [null, [Validators.required]], }); - constructor(protected categoriaService: CategoriaService, protected activatedRoute: ActivatedRoute, protected fb: FormBuilder) {} + constructor(protected categoriaService: CategoriaService, protected activatedRoute: ActivatedRoute, protected fb: FormBuilder) { + this.categorias = []; + this.loadAll(); + } ngOnInit(): void { this.activatedRoute.data.subscribe(({ categoria }) => { this.updateForm(categoria); }); + this.loadAll(); + } + + loadAll(): void { + this.categoriaService.query().subscribe(res => { + this.categorias = res.body ?? []; + }); } previousState(): void { @@ -36,13 +47,25 @@ export class CategoriaUpdateComponent implements OnInit { save(): void { this.isSaving = true; const categoria = this.createFromForm(); - if (categoria.id !== undefined) { - this.subscribeToSaveResponse(this.categoriaService.update(categoria)); + const condicion = this.categoryExists(categoria); + if (!condicion) { + if (categoria.id !== undefined) { + this.subscribeToSaveResponse(this.categoriaService.update(categoria)); + } else { + this.subscribeToSaveResponse(this.categoriaService.create(categoria)); + } } else { - this.subscribeToSaveResponse(this.categoriaService.create(categoria)); + this.previousState(); } } + protected categoryExists(categoria: ICategoria): boolean { + this.loadAll(); + var condicion = this.categorias!.some(cat => cat.nombre === categoria.nombre); + debugger; + return condicion; + } + protected subscribeToSaveResponse(result: Observable>): void { result.pipe(finalize(() => this.onSaveFinalize())).subscribe( () => this.onSaveSuccess(),