213 lines
6.2 KiB
TypeScript
213 lines
6.2 KiB
TypeScript
import {Component, OnInit, signal} from '@angular/core';
|
|
import { MatDialog } from '@angular/material/dialog';
|
|
import { HttpClient } from '@angular/common/http';
|
|
import { CreateImageComponent } from './create-image/create-image/create-image.component';
|
|
import { InfoImageComponent } from './info-image/info-image/info-image.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-images',
|
|
templateUrl: './images.component.html',
|
|
styleUrls: ['./images.component.css']
|
|
})
|
|
export class ImagesComponent implements OnInit {
|
|
baseUrl: string = import.meta.env.NG_APP_BASE_API_URL;
|
|
images: { downloadUrl: string; name: string; uuid: string }[] = [];
|
|
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;
|
|
readonly panelOpenState = signal(false);
|
|
datePipe: DatePipe = new DatePipe('es-ES');
|
|
columns = [
|
|
{
|
|
columnDef: 'id',
|
|
header: 'ID',
|
|
cell: (user: any) => `${user.id}`
|
|
},
|
|
{
|
|
columnDef: 'name',
|
|
header: 'Nombre de imagen',
|
|
cell: (user: any) => `${user.name}`
|
|
},
|
|
{
|
|
columnDef: 'downloadUrl',
|
|
header: 'Url descarga',
|
|
cell: (user: any) => `${user.downloadUrl}`
|
|
},
|
|
{
|
|
columnDef: 'isDefault',
|
|
header: 'Imagen por defecto',
|
|
cell: (user: any) => `${user.isDefault}`
|
|
},
|
|
{
|
|
columnDef: 'installed',
|
|
header: 'Imagen instalada en ogBoot',
|
|
cell: (user: any) => `${user.installed}`
|
|
},
|
|
{
|
|
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 = `${this.baseUrl}/og-lives`;
|
|
|
|
constructor(
|
|
public dialog: MatDialog,
|
|
private http: HttpClient,
|
|
private dataService: DataService,
|
|
private toastService: ToastrService
|
|
) {}
|
|
|
|
ngOnInit(): void {
|
|
this.search();
|
|
this.loadAlert();
|
|
}
|
|
|
|
addImage(): void {
|
|
const dialogRef = this.dialog.open(CreateImageComponent, {
|
|
width: '400px'
|
|
});
|
|
|
|
dialogRef.afterClosed().subscribe(result => {
|
|
console.log('The dialog was closed');
|
|
this.search();
|
|
});
|
|
}
|
|
|
|
search(): void {
|
|
this.loading = true;
|
|
this.dataService.getImages(this.filters).subscribe(
|
|
data => {
|
|
this.dataSource.data = data;
|
|
this.loading = false;
|
|
},
|
|
error => {
|
|
console.error('Error fetching og lives', error);
|
|
this.loading = false;
|
|
}
|
|
);
|
|
}
|
|
|
|
showInfo(image: any): void {
|
|
const dialogRef = this.dialog.open(InfoImageComponent, {
|
|
width: '700px',
|
|
data: image
|
|
});
|
|
}
|
|
|
|
toggleAction(image: any, action:string): void {
|
|
switch (action) {
|
|
case 'set-default':
|
|
this.http.post(`${this.apiUrl}/server/${image.uuid}/set-default`, {}).subscribe({
|
|
next: () => {
|
|
console.log('Imagen cambiada');
|
|
this.toastService.success('Petición de cambio de imagen enviada');
|
|
this.search();
|
|
},
|
|
error: (error) => {
|
|
console.error('Error al cambiar la imagen:', error);
|
|
this.toastService.error('Error al instalar la imagen por defecto');
|
|
}
|
|
});
|
|
break;
|
|
case 'install':
|
|
this.http.post(`${this.apiUrl}/server/${image.uuid}/install`, {}).subscribe({
|
|
next: () => {
|
|
console.log('Imagen cambiada');
|
|
this.toastService.success('Petición de instalación enviada');
|
|
this.search();
|
|
},
|
|
error: (error) => {
|
|
console.error('Error al instalar la imagen:', error);
|
|
this.toastService.error('Error al instalar la imagen');
|
|
}
|
|
});
|
|
break;
|
|
case 'uninstall':
|
|
this.http.post(`${this.apiUrl}/server/${image.uuid}/uninstall`, {}).subscribe({
|
|
next: () => {
|
|
console.log('Imagen cambiada');
|
|
this.toastService.success('Petición de desinstalación enviada');
|
|
/* this.deleteImage(image); */
|
|
this.search();
|
|
},
|
|
error: (error) => {
|
|
console.error('Error al desinstalar la imagen:', error);
|
|
this.toastService.error('Error al desinstalar la imagen');
|
|
}
|
|
});
|
|
break;
|
|
default:
|
|
console.error('Acción no soportada:', action);
|
|
break;
|
|
}
|
|
}
|
|
|
|
editImage(image: any): void {
|
|
const dialogRef = this.dialog.open(CreateImageComponent, {
|
|
width: '700px',
|
|
data: image
|
|
});
|
|
|
|
dialogRef.afterClosed().subscribe(result => {
|
|
if (result) {
|
|
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.installed_ogLives.length
|
|
}, error => {
|
|
console.error('Error al cargar la información del alert', error);
|
|
});
|
|
}
|
|
|
|
syncOgBoot(): 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');
|
|
});
|
|
}
|
|
}
|