no registrar categoria si la nueva tiene un nombre duplicado
This commit is contained in:
		
							parent
							
								
									10b98b1b8e
								
							
						
					
					
						commit
						1308498603
					
				|  | @ -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,11 +47,23 @@ export class CategoriaUpdateComponent implements OnInit { | |||
|   save(): void { | ||||
|     this.isSaving = true; | ||||
|     const categoria = this.createFromForm(); | ||||
|     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.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<HttpResponse<ICategoria>>): void { | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue