2021-07-03 21:48:27 +00:00
|
|
|
import { Component, OnInit } from '@angular/core';
|
|
|
|
import { HttpResponse } from '@angular/common/http';
|
|
|
|
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
|
2021-08-01 03:53:35 +00:00
|
|
|
import { Observable } from 'rxjs';
|
|
|
|
import { finalize, map } from 'rxjs/operators';
|
2021-07-03 21:48:27 +00:00
|
|
|
|
2021-08-01 03:53:35 +00:00
|
|
|
import { IPlantilla, Plantilla } from '../plantilla.model';
|
2021-07-03 21:48:27 +00:00
|
|
|
import { PlantillaService } from '../service/plantilla.service';
|
|
|
|
import { PlantillaDeleteDialogComponent } from '../delete/plantilla-delete-dialog.component';
|
|
|
|
|
2021-08-01 03:53:35 +00:00
|
|
|
import { AccountService } from 'app/core/auth/account.service';
|
|
|
|
import { Account } from 'app/core/auth/account.model';
|
|
|
|
import { FormBuilder, Validators } from '@angular/forms';
|
|
|
|
import { EstadoPlantilla } from 'app/entities/enumerations/estado-plantilla.model';
|
|
|
|
import { ICategoria } from 'app/entities/categoria/categoria.model';
|
|
|
|
import { CategoriaService } from 'app/entities/categoria/service/categoria.service';
|
|
|
|
|
|
|
|
import * as dayjs from 'dayjs';
|
|
|
|
import { DATE_TIME_FORMAT } from 'app/config/input.constants';
|
2021-08-05 08:27:58 +00:00
|
|
|
import { PlantillaChangeStatusDialogComponent } from '../plantilla-change-status-dialog/plantilla-change-status-dialog.component';
|
|
|
|
|
|
|
|
import { faExchangeAlt } from '@fortawesome/free-solid-svg-icons';
|
2021-08-01 03:53:35 +00:00
|
|
|
|
2021-07-03 21:48:27 +00:00
|
|
|
@Component({
|
|
|
|
selector: 'jhi-plantilla',
|
|
|
|
templateUrl: './plantilla.component.html',
|
|
|
|
})
|
|
|
|
export class PlantillaComponent implements OnInit {
|
|
|
|
plantillas?: IPlantilla[];
|
|
|
|
isLoading = false;
|
2021-08-01 03:53:35 +00:00
|
|
|
isSaving = false;
|
|
|
|
createAnotherTemplate: Boolean = false;
|
|
|
|
|
|
|
|
account: Account | null = null;
|
|
|
|
categoriasSharedCollection: ICategoria[] = [];
|
2021-07-03 21:48:27 +00:00
|
|
|
|
2021-08-01 03:53:35 +00:00
|
|
|
templateCreateForm = this.fb.group({
|
|
|
|
id: [],
|
2021-08-02 02:17:50 +00:00
|
|
|
nombre: [null, [Validators.required, Validators.minLength(1), Validators.maxLength(50)]],
|
|
|
|
descripcion: [[Validators.required]],
|
|
|
|
precio: [null, [Validators.required, Validators.min(0)]],
|
|
|
|
categoria: [null, [Validators.required]],
|
2021-08-01 03:53:35 +00:00
|
|
|
});
|
|
|
|
|
2021-08-05 08:27:58 +00:00
|
|
|
faExchangeAlt = faExchangeAlt;
|
2021-08-06 05:56:00 +00:00
|
|
|
|
|
|
|
public searchString: string;
|
|
|
|
public estadoPlantilla: string;
|
|
|
|
|
2021-08-01 03:53:35 +00:00
|
|
|
constructor(
|
|
|
|
protected plantillaService: PlantillaService,
|
|
|
|
protected modalService: NgbModal,
|
|
|
|
protected accountService: AccountService,
|
|
|
|
protected fb: FormBuilder,
|
|
|
|
protected categoriaService: CategoriaService
|
2021-08-06 05:56:00 +00:00
|
|
|
) {
|
|
|
|
this.searchString = '';
|
|
|
|
this.estadoPlantilla = '';
|
|
|
|
}
|
2021-07-03 21:48:27 +00:00
|
|
|
|
|
|
|
loadAll(): void {
|
|
|
|
this.isLoading = true;
|
2021-08-06 05:56:00 +00:00
|
|
|
this.searchString = '';
|
|
|
|
this.estadoPlantilla = '';
|
2021-07-03 21:48:27 +00:00
|
|
|
this.plantillaService.query().subscribe(
|
|
|
|
(res: HttpResponse<IPlantilla[]>) => {
|
|
|
|
this.isLoading = false;
|
2021-08-05 06:48:49 +00:00
|
|
|
const tempPlantillas = res.body ?? [];
|
|
|
|
this.plantillas = tempPlantillas.filter(p => p.estado !== EstadoPlantilla.DELETED);
|
2021-07-03 21:48:27 +00:00
|
|
|
},
|
|
|
|
() => {
|
|
|
|
this.isLoading = false;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit(): void {
|
|
|
|
this.loadAll();
|
2021-08-01 03:53:35 +00:00
|
|
|
this.loadRelationshipsOptions();
|
2021-07-03 21:48:27 +00:00
|
|
|
}
|
|
|
|
|
2021-07-30 04:33:00 +00:00
|
|
|
trackId(_index: number, item: IPlantilla): number {
|
2021-07-03 21:48:27 +00:00
|
|
|
return item.id!;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(plantilla: IPlantilla): void {
|
|
|
|
const modalRef = this.modalService.open(PlantillaDeleteDialogComponent, { size: 'lg', backdrop: 'static' });
|
|
|
|
modalRef.componentInstance.plantilla = plantilla;
|
|
|
|
// unsubscribe not needed because closed completes on modal close
|
|
|
|
modalRef.closed.subscribe(reason => {
|
|
|
|
if (reason === 'deleted') {
|
|
|
|
this.loadAll();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2021-08-01 03:53:35 +00:00
|
|
|
|
2021-08-05 08:27:58 +00:00
|
|
|
cambiarEstado(plantilla: IPlantilla): void {
|
|
|
|
const modalRef = this.modalService.open(PlantillaChangeStatusDialogComponent, { size: 'lg', backdrop: 'static' });
|
|
|
|
modalRef.componentInstance.plantilla = plantilla;
|
|
|
|
// unsubscribe not needed because closed completes on modal close
|
|
|
|
modalRef.closed.subscribe(reason => {
|
|
|
|
if (reason === 'updated') {
|
|
|
|
this.loadAll();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-08-01 03:53:35 +00:00
|
|
|
isAdmin(): boolean {
|
|
|
|
return this.accountService.hasAnyAuthority('ROLE_ADMIN');
|
|
|
|
}
|
|
|
|
|
|
|
|
isAuthenticated(): boolean {
|
|
|
|
return this.accountService.isAuthenticated();
|
|
|
|
}
|
|
|
|
|
|
|
|
resetCreateTemplateForm(): void {
|
|
|
|
this.templateCreateForm.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
createAnotherTemplateChange(event: any): void {
|
|
|
|
// ID: #crearPlantilla
|
|
|
|
this.createAnotherTemplate = event.target.checked;
|
|
|
|
}
|
|
|
|
|
|
|
|
previousState(): void {
|
|
|
|
window.history.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
save(): void {
|
|
|
|
this.isSaving = true;
|
|
|
|
const plantilla = this.createFromForm();
|
|
|
|
if (plantilla.id !== undefined) {
|
|
|
|
this.subscribeToSaveResponse(this.plantillaService.update(plantilla));
|
|
|
|
} else {
|
|
|
|
this.subscribeToSaveResponse(this.plantillaService.create(plantilla));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
trackCategoriaById(index: number, item: ICategoria): number {
|
|
|
|
return item.id!;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected subscribeToSaveResponse(result: Observable<HttpResponse<IPlantilla>>): void {
|
|
|
|
result.pipe(finalize(() => this.onSaveFinalize())).subscribe(
|
|
|
|
() => this.onSaveSuccess(),
|
|
|
|
() => this.onSaveError()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected onSaveSuccess(): void {
|
|
|
|
this.templateCreateForm.reset();
|
|
|
|
this.plantillas = [];
|
|
|
|
this.loadAll();
|
|
|
|
if (!this.createAnotherTemplate) {
|
|
|
|
$('#cancelBtn').click();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected onSaveError(): void {
|
|
|
|
// Api for inheritance.
|
|
|
|
}
|
|
|
|
|
|
|
|
protected onSaveFinalize(): void {
|
|
|
|
this.isSaving = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected loadRelationshipsOptions(): void {
|
|
|
|
this.categoriaService
|
|
|
|
.query()
|
|
|
|
.pipe(map((res: HttpResponse<ICategoria[]>) => res.body ?? []))
|
|
|
|
.pipe(
|
|
|
|
map((categorias: ICategoria[]) =>
|
|
|
|
this.categoriaService.addCategoriaToCollectionIfMissing(categorias, this.templateCreateForm.get('categoria')!.value)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.subscribe((categorias: ICategoria[]) => (this.categoriasSharedCollection = categorias));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected createFromForm(): IPlantilla {
|
|
|
|
const now = dayjs();
|
|
|
|
|
|
|
|
return {
|
|
|
|
...new Plantilla(),
|
|
|
|
id: undefined,
|
|
|
|
nombre: this.templateCreateForm.get(['nombre'])!.value,
|
|
|
|
descripcion: this.templateCreateForm.get(['descripcion'])!.value,
|
|
|
|
fechaCreacion: dayjs(now, DATE_TIME_FORMAT),
|
|
|
|
estado: EstadoPlantilla.DRAFT,
|
|
|
|
precio: this.templateCreateForm.get(['precio'])!.value,
|
|
|
|
categoria: this.templateCreateForm.get(['categoria'])!.value,
|
|
|
|
};
|
|
|
|
}
|
2021-07-03 21:48:27 +00:00
|
|
|
}
|