diff --git a/ogWebconsole/src/app/components/ogdhcp/og-dhcp-subnets/add-clients-to-subnet/add-clients-to-subnet.component.html b/ogWebconsole/src/app/components/ogdhcp/og-dhcp-subnets/add-clients-to-subnet/add-clients-to-subnet.component.html index 3588c5a..44ec007 100644 --- a/ogWebconsole/src/app/components/ogdhcp/og-dhcp-subnets/add-clients-to-subnet/add-clients-to-subnet.component.html +++ b/ogWebconsole/src/app/components/ogdhcp/og-dhcp-subnets/add-clients-to-subnet/add-clients-to-subnet.component.html @@ -1,25 +1,32 @@

AƱade clientes a {{data.subnetName}}

- - - - - {{ client.name }} - - + + Unidad Organizativa + + {{ unit.name }} + -
-

Clientes seleccionados:

-
    -
  • + + Subunidad Organizativa + + {{ child.name }} + + + +
    + +
    + {{ client.name }} - -
  • -
+ +
+
+

No hay clientes disponibles

+
diff --git a/ogWebconsole/src/app/components/ogdhcp/og-dhcp-subnets/add-clients-to-subnet/add-clients-to-subnet.component.ts b/ogWebconsole/src/app/components/ogdhcp/og-dhcp-subnets/add-clients-to-subnet/add-clients-to-subnet.component.ts index 8efab27..33dbecd 100644 --- a/ogWebconsole/src/app/components/ogdhcp/og-dhcp-subnets/add-clients-to-subnet/add-clients-to-subnet.component.ts +++ b/ogWebconsole/src/app/components/ogdhcp/og-dhcp-subnets/add-clients-to-subnet/add-clients-to-subnet.component.ts @@ -1,10 +1,8 @@ import { Component, OnInit, Inject } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog'; -import {Observable, startWith} from "rxjs"; -import {map} from "rxjs/operators"; -import {FormControl} from "@angular/forms"; -import {ToastrService} from "ngx-toastr"; +import { FormControl } from '@angular/forms'; +import { ToastrService } from 'ngx-toastr'; @Component({ selector: 'app-add-clients-to-subnet', @@ -13,59 +11,93 @@ import {ToastrService} from "ngx-toastr"; }) export class AddClientsToSubnetComponent implements OnInit { baseUrl: string = import.meta.env.NG_APP_BASE_API_URL; + units: any[] = []; + childUnits: any[] = []; clients: any[] = []; - selectedClients: any[] = []; + selectedClients: string[] = []; loading: boolean = true; - filters: { [key: string]: string } = {}; - filteredClients!: Observable; - clientControl = new FormControl(); + unitControl = new FormControl(); + childUnitControl = new FormControl(); constructor( private http: HttpClient, public dialogRef: MatDialogRef, private toastService: ToastrService, @Inject(MAT_DIALOG_DATA) public data: { subnetUuid: string, subnetName: string } - ) {} + ) {} ngOnInit(): void { - console.log('Selected subnet UUID:', this.data); this.loading = true; + this.loadUnits(); + } - this.loadClients(); - - this.filteredClients = this.clientControl.valueChanges.pipe( - startWith(''), - map(value => (typeof value === 'string' ? value : value?.name)), - map(name => (name ? this._filterClients(name) : this.clients.slice())) + loadUnits() { + this.http.get(`${this.baseUrl}/organizational-units?page=1&itemsPerPage=50`).subscribe( + response => { + this.units = response['hydra:member'].filter((unit: { type: string; }) => unit.type === 'organizational-unit'); + this.loading = false; + }, + error => console.error('Error fetching organizational units:', error) ); } - loadClients() { - this.http.get( `${this.baseUrl}/clients?&page=1&itemsPerPage=10000&exists[subnet]=false`).subscribe( - response => { - this.clients = response['hydra:member']; - this.loading = false; - }, - error => { - console.error('Error fetching parent units:', error); - this.loading = false; + onUnitChange(unitId: string): void { + const unit = this.units.find(unit => unit.uuid === unitId); + this.childUnits = unit ? this.getAllChildren(unit) : []; + this.clients = []; + this.childUnitControl.setValue(null); + this.selectedClients = []; + } + + getAllChildren(unit: any): any[] { + let allChildren = []; + if (unit.children && unit.children.length > 0) { + for (const child of unit.children) { + allChildren.push(child); + allChildren = allChildren.concat(this.getAllChildren(child)); } - ); + } + return allChildren; + } + + onChildUnitChange(childUnitId: string): void { + const childUnit = this.childUnits.find(unit => unit.uuid === childUnitId); + this.clients = childUnit && childUnit.clients ? childUnit.clients : []; + this.selectedClients = []; + } + + toggleClientSelection(clientId: string): void { + const index = this.selectedClients.indexOf(clientId); + if (index >= 0) { + this.selectedClients.splice(index, 1); + } else { + this.selectedClients.push(clientId); + } + } + + toggleSelectAll(): void { + if (this.areAllClientsSelected()) { + this.selectedClients = []; + } else { + this.selectedClients = this.clients.map(client => client.uuid); + } + } + + areAllClientsSelected(): boolean { + return this.selectedClients.length === this.clients.length; } save() { - this.selectedClients.forEach(client => { - const postData = { - client: client['@id'] - }; + this.selectedClients.forEach(clientId => { + const postData = { client: `/clients/${clientId}` }; this.http.post(`${this.baseUrl}/og-dhcp/server/${this.data.subnetUuid}/post-host`, postData).subscribe( response => { - this.toastService.success(`Cliente ${client.name} asignado correctamente`); + this.toastService.success(`Cliente asignado correctamente`); }, error => { - console.error(`Error al asignar el cliente ${client.name}:`, error); - this.toastService.error(`Error al asignar el cliente ${client.name}: ${error.error['hydra:description']}`); + console.error(`Error al asignar el cliente:`, error); + this.toastService.error(`Error al asignar el cliente: ${error.error['hydra:description']}`); } ); }); @@ -76,27 +108,4 @@ export class AddClientsToSubnetComponent implements OnInit { close() { this.dialogRef.close(); } - - removeClient(client: any) { - const index = this.selectedClients.indexOf(client); - if (index >= 0) { - this.selectedClients.splice(index, 1); - } - } - - private _filterClients(name: string): any[] { - const filterValue = name.toLowerCase(); - return this.clients.filter(client => client.name.toLowerCase().includes(filterValue)); - } - - displayFnClient(client: any): string { - return client && client.name ? client.name : ''; - } - - onOptionClientSelected(client: any) { - if (!this.selectedClients.includes(client)) { - this.selectedClients.push(client); - } - this.clientControl.setValue(''); - } }