refs #985 partition-assistant reloadData fix
parent
5bd9db1618
commit
d89fabf7f3
|
@ -1,8 +1,9 @@
|
|||
import { Component, Input, OnInit } from '@angular/core';
|
||||
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { ToastrService } from 'ngx-toastr';
|
||||
|
||||
interface Partition {
|
||||
uuid?: string; // Agregamos uuid opcional
|
||||
uuid?: string;
|
||||
partitionNumber: number;
|
||||
size: number;
|
||||
type: string;
|
||||
|
@ -18,19 +19,34 @@ interface Partition {
|
|||
styleUrls: ['./partition-assistant.component.css']
|
||||
})
|
||||
export class PartitionAssistantComponent implements OnInit {
|
||||
baseUrl: string = import.meta.env.NG_APP_BASE_API_URL;
|
||||
@Input() data: any;
|
||||
@Input() clientUuid: string | undefined;
|
||||
@Output() dataChange = new EventEmitter<any>();
|
||||
|
||||
errorMessage = '';
|
||||
originalPartitions: any[] = [];
|
||||
disks: { diskNumber: number; totalDiskSize: number; partitions: Partition[] }[] = [];
|
||||
|
||||
private apiUrl = 'http://127.0.0.1:8001/partitions';
|
||||
private apiUrl: string = this.baseUrl + '/partitions';
|
||||
|
||||
constructor(private http: HttpClient) {}
|
||||
constructor(private http: HttpClient, private toastService: ToastrService) {}
|
||||
|
||||
ngOnInit() {
|
||||
this.initializeDisks();
|
||||
this.loadClientData();
|
||||
}
|
||||
|
||||
loadClientData() {
|
||||
const url = `${this.baseUrl}/clients/${this.clientUuid}`;
|
||||
this.http.get(url).subscribe(
|
||||
(response) => {
|
||||
this.data = response;
|
||||
this.initializeDisks();
|
||||
},
|
||||
(error) => {
|
||||
console.error('Error al cargar los datos del cliente:', error);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
initializeDisks() {
|
||||
|
@ -49,10 +65,10 @@ export class PartitionAssistantComponent implements OnInit {
|
|||
disk!.totalDiskSize = this.convertBytesToGB(partition.size);
|
||||
} else {
|
||||
disk!.partitions.push({
|
||||
uuid: partition.uuid, // Incluimos el uuid
|
||||
uuid: partition.uuid,
|
||||
partitionNumber: partition.partitionNumber,
|
||||
size: this.convertBytesToGB(partition.size),
|
||||
type: partition.type,
|
||||
type: partition.type || partition.filesystem || 'NTFS',
|
||||
sizeBytes: partition.size,
|
||||
format: false,
|
||||
color: '#' + Math.floor(Math.random() * 16777215).toString(16),
|
||||
|
@ -72,7 +88,7 @@ export class PartitionAssistantComponent implements OnInit {
|
|||
}
|
||||
|
||||
convertBytesToGB(bytes: number): number {
|
||||
return bytes / (1024 * 1024 * 1024);
|
||||
return bytes / 1024;
|
||||
}
|
||||
|
||||
updatePartitionPercentages(partitions: Partition[], totalDiskSize: number) {
|
||||
|
@ -120,7 +136,7 @@ export class PartitionAssistantComponent implements OnInit {
|
|||
} else {
|
||||
this.errorMessage = '';
|
||||
partition.size = size;
|
||||
partition.sizeBytes = size * 1024 * 1024 * 1024;
|
||||
partition.sizeBytes = size * 1024;
|
||||
this.updatePartitionPercentages(disk.partitions, disk.totalDiskSize);
|
||||
}
|
||||
}
|
||||
|
@ -132,10 +148,7 @@ export class PartitionAssistantComponent implements OnInit {
|
|||
}
|
||||
|
||||
isPartitionModified(original: any, current: Partition): boolean {
|
||||
return (
|
||||
this.convertBytesToGB(original.size) !== current.size ||
|
||||
original.type !== current.type
|
||||
);
|
||||
return this.convertBytesToGB(original.size) !== current.size || original.type !== current.type;
|
||||
}
|
||||
|
||||
getModifiedOrNewPartitions() {
|
||||
|
@ -148,7 +161,6 @@ export class PartitionAssistantComponent implements OnInit {
|
|||
);
|
||||
|
||||
if (!originalPartition) {
|
||||
// Es una nueva partición
|
||||
modifiedPartitions.push({
|
||||
partition,
|
||||
diskNumber: disk.diskNumber,
|
||||
|
@ -156,7 +168,6 @@ export class PartitionAssistantComponent implements OnInit {
|
|||
isNew: true
|
||||
});
|
||||
} else if (this.isPartitionModified(originalPartition, partition)) {
|
||||
// La partición ha sido modificada
|
||||
modifiedPartitions.push({
|
||||
partition,
|
||||
diskNumber: disk.diskNumber,
|
||||
|
@ -188,25 +199,21 @@ export class PartitionAssistantComponent implements OnInit {
|
|||
};
|
||||
|
||||
if (isNew) {
|
||||
// Es una nueva partición, usamos POST
|
||||
this.http.post(this.apiUrl, payload).subscribe(
|
||||
(response) => {
|
||||
console.log('Partición creada exitosamente:', response);
|
||||
this.toastService.success('Partición creada exitosamente');
|
||||
this.loadClientData();
|
||||
},
|
||||
(error) => {
|
||||
console.error('Error al crear la partición:', error);
|
||||
}
|
||||
(error) => {}
|
||||
);
|
||||
} else if (partition.uuid) {
|
||||
// Es una partición existente modificada, usamos PATCH
|
||||
const patchUrl = `${this.apiUrl}/${partition.uuid}`;
|
||||
this.http.patch(patchUrl, payload).subscribe(
|
||||
(response) => {
|
||||
console.log('Partición actualizada exitosamente:', response);
|
||||
this.toastService.success('Partición actualizada exitosamente');
|
||||
this.loadClientData();
|
||||
},
|
||||
(error) => {
|
||||
console.error('Error al actualizar la partición:', error);
|
||||
}
|
||||
(error) => {}
|
||||
);
|
||||
}
|
||||
});
|
||||
|
@ -222,15 +229,13 @@ export class PartitionAssistantComponent implements OnInit {
|
|||
this.updatePartitionPercentages(disk.partitions, disk.totalDiskSize);
|
||||
|
||||
if (partition.uuid) {
|
||||
// La partición existía originalmente, enviamos DELETE
|
||||
const deleteUrl = `${this.apiUrl}/${partition.uuid}`;
|
||||
this.http.delete(deleteUrl).subscribe(
|
||||
(response) => {
|
||||
console.log('Partición eliminada exitosamente:', response);
|
||||
this.toastService.success('Partición eliminada exitosamente');
|
||||
this.loadClientData();
|
||||
},
|
||||
(error) => {
|
||||
console.error('Error al eliminar la partición:', error);
|
||||
}
|
||||
(error) => {}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue