From fd64a0be841212156de4f561e65840e5acf56b1e Mon Sep 17 00:00:00 2001 From: apuente Date: Mon, 4 Nov 2024 12:45:42 +0100 Subject: [PATCH] refs #1045 Refactor create task stepper --- .../create-task/create-task.component.css | 10 +- .../create-task/create-task.component.html | 164 ++++++++---------- .../create-task/create-task.component.ts | 112 +++++++----- 3 files changed, 153 insertions(+), 133 deletions(-) diff --git a/ogWebconsole/src/app/components/commands/commands-task/create-task/create-task.component.css b/ogWebconsole/src/app/components/commands/commands-task/create-task/create-task.component.css index d5dad12..3c41e87 100644 --- a/ogWebconsole/src/app/components/commands/commands-task/create-task/create-task.component.css +++ b/ogWebconsole/src/app/components/commands/commands-task/create-task/create-task.component.css @@ -12,10 +12,16 @@ .button-container { display: flex; - justify-content: space-between; + justify-content: flex-end; margin-top: 20px; } mat-form-field { - margin-bottom: 16px; /* Espaciado entre campos */ + margin-bottom: 16px; +} + +.section-title { + margin-top: 24px; + margin-bottom: 8px; + font-weight: 500; } diff --git a/ogWebconsole/src/app/components/commands/commands-task/create-task/create-task.component.html b/ogWebconsole/src/app/components/commands/commands-task/create-task/create-task.component.html index 8b88d6b..fc2ed1e 100644 --- a/ogWebconsole/src/app/components/commands/commands-task/create-task/create-task.component.html +++ b/ogWebconsole/src/app/components/commands/commands-task/create-task/create-task.component.html @@ -2,107 +2,87 @@
- - - - - Información - - - - Selecciona Comandos - - - {{ group.name }} - - - Este campo es obligatorio - +

Información

+ + + Información + + -
- -
-
+

Seleccón de comandos

+ + + Selecciona Comandos + + + {{ group.name }} + + + Este campo es obligatorio + - - - - Selecciona Comandos Individuales (Opcional) - - - {{ command.name }} - - - + + Selecciona Comandos Individuales (Opcional) + + + {{ command.name }} + + + -
- - -
-
+

Fecha y hora de ejecución

+ + + Fecha de Ejecución + + + + Este campo es obligatorio + - - - - Fecha de Ejecución - - - - Este campo es obligatorio - + + Hora de Ejecución + + Este campo es obligatorio + - - Hora de Ejecución - - Este campo es obligatorio - +

Selecciona destino

+ + + Selecciona Unidad Organizacional + + + {{ unit.name }} + + + Este campo es obligatorio + -
- - -
-
+ + Selecciona Aula + + + {{ child.name }} + + + - - - - Selecciona Unidad Organizacional - - - {{ unit.name }} - - - Este campo es obligatorio - + + Selecciona Clientes + + + Seleccionar todos + + + {{ client.name }} ({{ client.ip }}) + + + - - Selecciona aula - - - {{ child.name }} - - - +
+ +
- - Selecciona Clientes - - - Seleccionar todos - - - {{ client.name }} ({{ client.ip }}) - - - - -
- - -
-
- -
diff --git a/ogWebconsole/src/app/components/commands/commands-task/create-task/create-task.component.ts b/ogWebconsole/src/app/components/commands/commands-task/create-task/create-task.component.ts index 4b06e85..8a90f7e 100644 --- a/ogWebconsole/src/app/components/commands/commands-task/create-task/create-task.component.ts +++ b/ogWebconsole/src/app/components/commands/commands-task/create-task/create-task.component.ts @@ -49,8 +49,6 @@ export class CreateTaskComponent implements OnInit { this.editing = true; this.loadTaskData(this.data.task); } - - console.log(this.data); } loadCommandGroups(): void { @@ -101,6 +99,76 @@ export class CreateTaskComponent implements OnInit { } } + private collectClassrooms(unit: any): any[] { + let classrooms = []; + if (unit.type === 'classroom') { + classrooms.push(unit); + } + if (unit.children && unit.children.length > 0) { + for (let child of unit.children) { + classrooms = classrooms.concat(this.collectClassrooms(child)); + } + } + return classrooms; + } + + onOrganizationalUnitChange(): void { + const selectedUnitId = this.taskForm.get('organizationalUnit')?.value; + const selectedUnit = this.availableOrganizationalUnits.find(unit => unit['@id'] === selectedUnitId); + + if (selectedUnit) { + this.selectedUnitChildren = this.collectClassrooms(selectedUnit); + } else { + this.selectedUnitChildren = []; + } + + this.taskForm.patchValue({ selectedChild: '', selectedClients: [] }); + this.selectedClients = []; + this.selectedClientIds.clear(); + } + + onChildChange(): void { + const selectedChildId = this.taskForm.get('selectedChild')?.value; + + if (!selectedChildId) { + this.selectedClients = []; + return; + } + + const url = `${this.baseUrl}${selectedChildId}`.replace(/([^:]\/)\/+/g, '$1'); + + this.http.get(url).subscribe( + (data) => { + if (Array.isArray(data.clients) && data.clients.length > 0) { + this.selectedClients = data.clients; + } else { + this.selectedClients = []; + this.toastr.warning('El aula seleccionada no tiene clientes.'); + } + + this.taskForm.patchValue({ selectedClients: [] }); + this.selectedClientIds.clear(); + }, + (error) => { + this.toastr.error('Error al cargar los detalles del aula seleccionada'); + } + ); + } + + toggleSelectAll() { + const allSelected = this.areAllSelected(); + if (allSelected) { + this.selectedClientIds.clear(); + } else { + this.selectedClients.forEach(client => this.selectedClientIds.add(client.uuid)); + } + this.taskForm.get('selectedClients')!.setValue(Array.from(this.selectedClientIds)); + } + + areAllSelected(): boolean { + return this.selectedClients.length > 0 && this.selectedClients.every(client => this.selectedClientIds.has(client.uuid)); + } + onCommandGroupChange(): void { const selectedGroupId = this.taskForm.get('commandGroup')?.value; this.http.get(`${this.baseUrl}/command-groups/${selectedGroupId}`).subscribe( @@ -113,40 +181,6 @@ export class CreateTaskComponent implements OnInit { ); } - onOrganizationalUnitChange(): void { - const selectedUnitId = this.taskForm.get('organizationalUnit')?.value; - const selectedUnit = this.availableOrganizationalUnits.find(unit => unit['@id'] === selectedUnitId); - this.selectedUnitChildren = selectedUnit ? selectedUnit.children : []; - } - - onChildChange(): void { - const selectedChildId = this.taskForm.get('selectedChild')?.value; - this.http.get(`${this.baseUrl}${selectedChildId}`).subscribe( - (data) => { - this.selectedClients = data.clients; - this.taskForm.patchValue({ selectedClients: [] }); - this.selectedClientIds.clear(); - }, - (error) => { - this.toastr.error('Error al cargar los detalles del aula seleccionada'); - } - ); - } - - toggleSelectAll() { - const allSelected = this.areAllSelected(); - if (allSelected) { - this.selectedClientIds.clear(); - } else { - this.selectedClients.forEach(client => this.selectedClientIds.add(client.uuid)); - } - this.taskForm.get('selectedClients')!.setValue(Array.from(this.selectedClientIds)); - } - - areAllSelected(): boolean { - return this.selectedClients.length > 0 && this.selectedClients.every(client => this.selectedClientIds.has(client.uuid)); - } - saveTask(): void { if (this.taskForm.invalid) { this.toastr.error('Por favor, rellene todos los campos obligatorios'); @@ -156,14 +190,14 @@ export class CreateTaskComponent implements OnInit { const formData = this.taskForm.value; const dateTime = this.combineDateAndTime(formData.date, formData.time); const selectedCommands = formData.extraCommands && formData.extraCommands.length > 0 - ? formData.extraCommands.map((id: any) => `/commands/${id}`) - : null; + ? formData.extraCommands.map((id: any) => `/commands/${id}`) + : null; const payload: any = { commandGroups: formData.commandGroup ? [`/command-groups/${formData.commandGroup}`] : null, dateTime: dateTime, notes: formData.notes || '', - clients: Array.from(this.selectedClientIds).map((uuid: string) => `/clients/${uuid}`), + clients: Array.from(this.selectedClientIds).map((uuid: string) => `/clients/${uuid}`), }; if (selectedCommands) {