From b0d255a7d1e2b737ca286930c9e48bd83dfbf792 Mon Sep 17 00:00:00 2001 From: Lucas Lara Date: Fri, 16 May 2025 13:46:15 +0200 Subject: [PATCH] refactor: prevent default setting of organizational unit when not in edit mode and streamline data loading logic --- .../manage-organizational-unit.component.ts | 248 +++++++++--------- 1 file changed, 124 insertions(+), 124 deletions(-) diff --git a/ogWebconsole/src/app/components/groups/shared/organizational-units/manage-organizational-unit/manage-organizational-unit.component.ts b/ogWebconsole/src/app/components/groups/shared/organizational-units/manage-organizational-unit/manage-organizational-unit.component.ts index 649b584..792db32 100644 --- a/ogWebconsole/src/app/components/groups/shared/organizational-units/manage-organizational-unit/manage-organizational-unit.component.ts +++ b/ogWebconsole/src/app/components/groups/shared/organizational-units/manage-organizational-unit/manage-organizational-unit.component.ts @@ -177,7 +177,9 @@ export class ManageOrganizationalUnitComponent implements OnInit { } onParentChange(event: any): void { - this.setOrganizationalUnitDefaults(event.value); + if (!this.isEditMode) { + this.setOrganizationalUnitDefaults(event.value); + } } setOrganizationalUnitDefaults(unitId: string): void { @@ -205,6 +207,127 @@ export class ManageOrganizationalUnitComponent implements OnInit { } } + loadData(uuid: string): Promise { + return new Promise((resolve, reject) => { + const url = `${this.baseUrl}/organizational-units/${uuid}`; + + this.http.get(url).subscribe( + data => { + const exclude = data.excludeParentChanges; + + this.generalFormGroup.patchValue({ + name: data.name, + parent: data.parent ? data.parent['@id'] : '', + description: data.description, + type: data.type, + excludeParentChanges: exclude + }); + + this.additionalInfoFormGroup.patchValue({ + comments: data.comments + }); + + this.networkSettingsFormGroup.patchValue({ + proxy: data.networkSettings?.proxy, + dns: data.networkSettings?.dns, + netmask: data.networkSettings?.netmask, + router: data.networkSettings?.router, + ntp: data.networkSettings?.ntp, + netiface: data.networkSettings?.netiface, + p2pMode: data.networkSettings?.p2pMode, + p2pTime: data.networkSettings?.p2pTime, + mcastIp: data.networkSettings?.mcastIp, + mcastSpeed: data.networkSettings?.mcastSpeed, + mcastPort: data.networkSettings?.mcastPort, + mcastMode: data.networkSettings?.mcastMode, + menu: data.networkSettings?.menu ? data.networkSettings.menu['@id'] : null, + hardwareProfile: data.networkSettings?.hardwareProfile ? data.networkSettings.hardwareProfile['@id'] : null, + ogLive: data.networkSettings?.ogLive ? data.networkSettings.ogLive['@id'] : null, + repository: data.networkSettings?.repository ? data.networkSettings.repository['@id'] : null, + pxeTemplate: data.networkSettings?.pxeTemplate ? data.networkSettings.pxeTemplate['@id'] : null + }); + + this.classroomInfoFormGroup.patchValue({ + location: data.location, + projector: data.projector, + board: data.board, + capacity: data.capacity, + remoteCalendar: data.remoteCalendar ? data.remoteCalendar['@id'] : null + }); + + resolve(); + }, + + error => { + console.error('Error fetching data for edit:', error); + this.toastService.error('Error fetching data'); + reject(error); + this.onNoClick(); + } + ); + }); + } + + onSubmit() { + if (this.generalFormGroup.valid && this.additionalInfoFormGroup.valid && this.networkSettingsFormGroup.valid) { + const parentValue = this.generalFormGroup.value.parent; + const formData = { + name: this.generalFormGroup.value.name, + excludeParentChanges: this.generalFormGroup.value.excludeParentChanges, + parent: parentValue || null, + description: this.generalFormGroup.value.description, + comments: this.additionalInfoFormGroup.value.comments, + remoteCalendar: this.generalFormGroup.value.remoteCalendar, + type: this.generalFormGroup.value.type, + networkSettings: this.networkSettingsFormGroup.value, + location: this.classroomInfoFormGroup.value.location, + projector: this.classroomInfoFormGroup.value.projector, + board: this.classroomInfoFormGroup.value.board, + capacity: this.classroomInfoFormGroup.value.capacity, + }; + + if (this.isEditMode) { + const putUrl = `${this.baseUrl}/organizational-units/${this.data.uuid}`; + const headers = new HttpHeaders({ 'Content-Type': 'application/json' }); + + this.http.put(putUrl, formData, { headers }).subscribe( + response => { + this.unitAdded.emit(); + this.dialogRef.close({ success: true }); + this.toastService.success('Editado exitosamente', 'Éxito'); + }, + error => { + console.error('Error al realizar PUT:', error); + const errorMessages = error.error['hydra:description'].split('\n'); + errorMessages.forEach((message: string) => { + const cleanedMessage = message.replace(/networkSettings\.(\w+):/, 'Error $1:'); + this.toastService.error(cleanedMessage); + }); + } + ); + } else { + const postUrl = `${this.baseUrl}/organizational-units`; + const headers = new HttpHeaders({ 'Content-Type': 'application/json' }); + + this.http.post(postUrl, formData, { headers }).subscribe( + response => { + this.unitAdded.emit(response); + this.dialogRef.close({ success: true }); + this.toastService.success('Creado exitosamente', 'Éxito'); + }, + error => { + console.error('Error al realizar POST:', error); + const errorMessages = error.error['hydra:description'].split('\n'); + errorMessages.forEach((message: string) => { + const cleanedMessage = message.replace(/networkSettings\.(\w+):/, 'Error $1:'); + this.toastService.error(cleanedMessage); + }); + } + ); + } + } + } + getSelectedParentName(): string | undefined { const parentId = this.generalFormGroup.get('parent')?.value; return this.parentUnitsWithPaths.find(unit => unit.id === parentId)?.name; @@ -326,129 +449,6 @@ export class ManageOrganizationalUnitComponent implements OnInit { this.networkSettingsFormGroup.value.pxeTemplate = event.value; } - loadData(uuid: string): Promise { - return new Promise((resolve, reject) => { - const url = `${this.baseUrl}/organizational-units/${uuid}`; - - this.http.get(url).subscribe( - data => { - const exclude = data.excludeParentChanges; - - this.generalFormGroup.patchValue({ - name: data.name, - parent: data.parent ? data.parent['@id'] : '', - description: data.description, - type: data.type, - excludeParentChanges: exclude - }); - - this.additionalInfoFormGroup.patchValue({ - comments: data.comments - }); - - if (!exclude) { - this.networkSettingsFormGroup.patchValue({ - proxy: data.networkSettings?.proxy, - dns: data.networkSettings?.dns, - netmask: data.networkSettings?.netmask, - router: data.networkSettings?.router, - ntp: data.networkSettings?.ntp, - netiface: data.networkSettings?.netiface, - p2pMode: data.networkSettings?.p2pMode, - p2pTime: data.networkSettings?.p2pTime, - mcastIp: data.networkSettings?.mcastIp, - mcastSpeed: data.networkSettings?.mcastSpeed, - mcastPort: data.networkSettings?.mcastPort, - mcastMode: data.networkSettings?.mcastMode, - menu: data.networkSettings?.menu ? data.networkSettings.menu['@id'] : null, - hardwareProfile: data.networkSettings?.hardwareProfile ? data.networkSettings.hardwareProfile['@id'] : null, - ogLive: data.networkSettings?.ogLive ? data.networkSettings.ogLive['@id'] : null, - repository: data.networkSettings?.repository ? data.networkSettings.repository['@id'] : null, - pxeTemplate: data.networkSettings?.pxeTemplate ? data.networkSettings.pxeTemplate['@id'] : null - }); - } - - this.classroomInfoFormGroup.patchValue({ - location: data.location, - projector: data.projector, - board: data.board, - capacity: data.capacity, - remoteCalendar: data.remoteCalendar ? data.remoteCalendar['@id'] : null - }); - - resolve(); - }, - - error => { - console.error('Error fetching data for edit:', error); - this.toastService.error('Error fetching data'); - reject(error); - this.onNoClick(); - } - ); - }); - } - - onSubmit() { - if (this.generalFormGroup.valid && this.additionalInfoFormGroup.valid && this.networkSettingsFormGroup.valid) { - const parentValue = this.generalFormGroup.value.parent; - const formData = { - name: this.generalFormGroup.value.name, - excludeParentChanges: this.generalFormGroup.value.excludeParentChanges, - parent: parentValue || null, - description: this.generalFormGroup.value.description, - comments: this.additionalInfoFormGroup.value.comments, - remoteCalendar: this.generalFormGroup.value.remoteCalendar, - type: this.generalFormGroup.value.type, - networkSettings: this.networkSettingsFormGroup.value, - location: this.classroomInfoFormGroup.value.location, - projector: this.classroomInfoFormGroup.value.projector, - board: this.classroomInfoFormGroup.value.board, - capacity: this.classroomInfoFormGroup.value.capacity, - }; - - if (this.isEditMode) { - const putUrl = `${this.baseUrl}/organizational-units/${this.data.uuid}`; - const headers = new HttpHeaders({ 'Content-Type': 'application/json' }); - - this.http.put(putUrl, formData, { headers }).subscribe( - response => { - this.unitAdded.emit(); - this.dialogRef.close({ success: true }); - this.toastService.success('Editado exitosamente', 'Éxito'); - }, - error => { - console.error('Error al realizar PUT:', error); - const errorMessages = error.error['hydra:description'].split('\n'); - errorMessages.forEach((message: string) => { - const cleanedMessage = message.replace(/networkSettings\.(\w+):/, 'Error $1:'); - this.toastService.error(cleanedMessage); - }); - } - ); - } else { - const postUrl = `${this.baseUrl}/organizational-units`; - const headers = new HttpHeaders({ 'Content-Type': 'application/json' }); - - this.http.post(postUrl, formData, { headers }).subscribe( - response => { - this.unitAdded.emit(response); - this.dialogRef.close({ success: true }); - this.toastService.success('Creado exitosamente', 'Éxito'); - }, - error => { - console.error('Error al realizar POST:', error); - const errorMessages = error.error['hydra:description'].split('\n'); - errorMessages.forEach((message: string) => { - const cleanedMessage = message.replace(/networkSettings\.(\w+):/, 'Error $1:'); - this.toastService.error(cleanedMessage); - }); - } - ); - } - } - } - onNoClick(): void { this.dialogRef.close({ success: false }); }