oggui/ogWebconsole/src/app/components/groups/acctions-modal/acctions-modal.component.ts

82 lines
2.4 KiB
TypeScript

// componente
import { Component, Inject } from '@angular/core';
import { MatDialogRef, MAT_DIALOG_DATA, MatDialog } from '@angular/material/dialog';
import { ToastrService } from 'ngx-toastr';
import { CreatePxeBootFileComponent } from '../../ogboot/pxe-boot-files/create-pxeBootFile/create-pxe-boot-file/create-pxe-boot-file.component';
import { HttpClient } from '@angular/common/http';
import { CommandDetailComponent } from '../../commands/main-commands/detail-command/command-detail.component';
@Component({
selector: 'app-acctions-modal',
templateUrl: './acctions-modal.component.html',
styleUrls: ['./acctions-modal.component.css']
})
export class AcctionsModalComponent {
baseUrl: string = import.meta.env.NG_APP_BASE_API_URL;
selectedElements: any;
commands: any[] = [];
displayedColumns: string[] = ['name', 'createdBy', 'createdAt'];
private apiUrl = 'http://127.0.0.1:8001/commands?page=1&itemsPerPage=30';
ngOnInit(): void {
this.loadCommands();
}
loadCommands(): void {
this.http.get<any>(this.apiUrl).subscribe(
(data) => {
this.commands = data['hydra:member'];
},
(error) => {
console.error('Error fetching commands', error);
}
);
}
constructor(
private toastService: ToastrService,
public dialog: MatDialog,
private http: HttpClient,
@Inject(MAT_DIALOG_DATA) public data: any
) {
this.selectedElements = data?.selectedElements || [];
}
onSend(): void {
this.toastService.success('Acción enviada a: ' + this.selectedElements);
}
onPxeBootFile(): void {
const dialog = this.dialog.open(CreatePxeBootFileComponent, { data: this.selectedElements, width: '400px' });
dialog.afterClosed().subscribe(() => {
this.dialog.closeAll();
});
}
onCommandClick(command: any): void {
const payload = {
clients: this.selectedElements.map((uuid: any) => `/clients/${uuid}`)
};
const apiUrl = `${this.baseUrl}/commands/${command.uuid}/execute`;
this.http.post(apiUrl, payload).subscribe({
next: () => {
console.log('Command executed successfully');
this.loadCommands();
this.toastService.success('Command executed successfully');
},
error: (error) => {
console.error('Error executing command:', error);
}
});
}
chunkArray(arr: any[], chunkSize: number): any[] {
const chunks = [];
for (let i = 0; i < arr.length; i += chunkSize) {
chunks.push(arr.slice(i, i + chunkSize));
}
return chunks;
}
}