oggui/ogWebconsole/src/app/components/ogboot/pxe/pxe.component.ts

190 lines
5.3 KiB
TypeScript

import { HttpClient } from '@angular/common/http';
import { Component } from '@angular/core';
import { CreatePxeTemplateComponent } from './create-pxeTemplate/create-pxe-template.component';
import { MatDialog } from '@angular/material/dialog';
import { EditPxeTemplateComponent } from './edit-pxe-template/edit-pxe-template.component';
import {MatTableDataSource} from "@angular/material/table";
import {PageEvent} from "@angular/material/paginator";
import {ToastrService} from "ngx-toastr";
import {DatePipe} from "@angular/common";
import { DeleteModalComponent } from '../../../shared/delete_modal/delete-modal/delete-modal.component';
import {DataService} from "./data.service";
@Component({
selector: 'app-pxe',
templateUrl: './pxe.component.html',
styleUrl: './pxe.component.css'
})
export class PxeComponent {
pxeTemplates: any[] = [];
currentPage: number = 1;
dataSource = new MatTableDataSource<any>();
length: number = 0;
itemsPerPage: number = 10;
page: number = 1;
pageSizeOptions: number[] = [5, 10, 20, 40, 100];
selectedElements: string[] = [];
loading:boolean = false;
filters: { [key: string]: string } = {};
alertMessage: string | null = null;
datePipe: DatePipe = new DatePipe('es-ES');
selectedItem: any = null;
previewContent: any | null = null;
columns = [
{
columnDef: 'id',
header: 'ID',
cell: (user: any) => `${user.id}`
},
{
columnDef: 'name',
header: 'Nombre de la plantilla',
cell: (user: any) => `${user.name}`
},
{
columnDef: 'synchronized',
header: 'Creado en ogBoot',
cell: (user: any) => `${user.synchronized}`
},
{
columnDef: 'createdAt',
header: 'Fecha de creación',
cell: (user: any) => `${this.datePipe.transform(user.createdAt, 'dd/MM/yyyy hh:mm:ss')}`
}
];
displayedColumns = [...this.columns.map(column => column.columnDef), 'actions'];
private apiUrl = 'http://127.0.0.1:8080/pxe-templates';
constructor(
public dialog: MatDialog,
private http: HttpClient,
private toastService: ToastrService,
private dataService: DataService
) { }
ngOnInit(): void {
this.search()
this.loadAlert();
}
search(): void {
this.loading = true;
this.dataService.getPxeTemplates(this.filters).subscribe(
data => {
this.dataSource.data = data;
this.loading = false;
},
error => {
console.error('Error fetching pxe templates', error);
this.loading = false;
}
);
}
addPxeTemplate() {
const dialogRef = this.dialog.open(CreatePxeTemplateComponent, {
width: '600px'
});
dialogRef.afterClosed().subscribe(() => {
this.search();
});
}
showPxeInfo(template: any) {
this.selectedItem = template;
this.previewContent = template.templateContent;
}
toggleAction(image: any, action:string): void {
switch (action) {
case 'create':
this.http.post(`${this.apiUrl}/server/${image.uuid}/post`, {}).subscribe({
next: (response) => {
this.search();
// @ts-ignore
this.toastService.success(response.message);
},
error: (error) => {
this.toastService.error(error.error.error);
}
});
break;
case 'delete':
this.http.post(`${this.apiUrl}/server/${image.uuid}/delete`, {}).subscribe({
next: () => {
console.log('Plantilla cambiada');
this.search();
},
error: (error) => {
console.error('Error al cambiar la imagen:', error);
}
});
break;
default:
console.error('Acción no soportada:', action);
break;
}
}
editPxeTemplate(template: any) {
const dialogRef = this.dialog.open(EditPxeTemplateComponent, {
data: template,
width: '600px'
});
dialogRef.afterClosed().subscribe(() => {
this.search();
});
}
applyFilter() {
this.http.get<any>(`${this.apiUrl}?page=${this.page}&itemsPerPage=${this.itemsPerPage}`).subscribe({
next: (response) => {
this.dataSource.data = response['hydra:member'];
this.length = response['hydra:totalItems'];
},
error: (error) => {
console.error('Error al cargar las imágenes:', error);
}
});
}
onPageChange(event: PageEvent) {
this.page = event.pageIndex;
this.itemsPerPage = event.pageSize;
this.applyFilter();
}
loadAlert() {
this.http.get(`${this.apiUrl}/server/get-collection`)
.subscribe(response => {
// @ts-ignore
this.alertMessage = response.templates.length
}, error => {
console.error('Error al cargar la información del alert', error);
});
}
getIcon(): { name: string, color: string } {
if (Number(this.alertMessage) === this.length) {
return { name: 'check_circle', color: 'green' }; // Icono de check verde
} else {
return { name: 'cancel', color: 'red' }; // Icono de cruz roja
}
}
syncOgCore(): void {
this.http.post(`${this.apiUrl}/sync`, {})
.subscribe(response => {
this.toastService.success('Sincronización completada');
this.search()
}, error => {
console.error('Error al sincronizar', error);
this.toastService.error('Error al sincronizar');
});
}
}