refactor: prevent default setting of organizational unit when not in edit mode and streamline data loading logic
testing/ogGui-multibranch/pipeline/head This commit looks good Details

pull/23/head
Lucas Lara García 2025-05-16 13:46:15 +02:00
parent 4ff21afb57
commit b0d255a7d1
1 changed files with 124 additions and 124 deletions

View File

@ -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<void> {
return new Promise((resolve, reject) => {
const url = `${this.baseUrl}/organizational-units/${uuid}`;
this.http.get<any>(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<any>(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<any>(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<void> {
return new Promise((resolve, reject) => {
const url = `${this.baseUrl}/organizational-units/${uuid}`;
this.http.get<any>(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<any>(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<any>(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 });
}