diff --git a/ogWebconsole/src/app/components/software-profile/create-software-profile/create-software-profile.component.html b/ogWebconsole/src/app/components/software-profile/create-software-profile/create-software-profile.component.html index 325d220..a448bcc 100644 --- a/ogWebconsole/src/app/components/software-profile/create-software-profile/create-software-profile.component.html +++ b/ogWebconsole/src/app/components/software-profile/create-software-profile/create-software-profile.component.html @@ -2,7 +2,6 @@ -
diff --git a/ogWebconsole/src/app/components/software-profile/create-software-profile/create-software-profile.component.ts b/ogWebconsole/src/app/components/software-profile/create-software-profile/create-software-profile.component.ts index 5398b22..22440a6 100644 --- a/ogWebconsole/src/app/components/software-profile/create-software-profile/create-software-profile.component.ts +++ b/ogWebconsole/src/app/components/software-profile/create-software-profile/create-software-profile.component.ts @@ -1,21 +1,21 @@ -import {Component, Inject} from '@angular/core'; -import {FormBuilder, FormControl, FormGroup, Validators} from "@angular/forms"; -import {HttpClient} from "@angular/common/http"; -import {MAT_DIALOG_DATA, MatDialogRef} from "@angular/material/dialog"; -import {ToastrService} from "ngx-toastr"; -import {DataService as SoftwareService} from "../../software/data.service"; -import {DataService} from "../data.service"; -import {Observable, startWith} from "rxjs"; +import { Component, Inject, OnInit } from '@angular/core'; +import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms'; +import { HttpClient } from '@angular/common/http'; +import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog'; +import { ToastrService } from 'ngx-toastr'; +import { DataService as SoftwareService } from '../../software/data.service'; +import { DataService } from '../data.service'; +import { Observable, startWith } from 'rxjs'; import { debounceTime, switchMap, map } from 'rxjs/operators'; @Component({ selector: 'app-create-software-profile', templateUrl: './create-software-profile.component.html', - styleUrl: './create-software-profile.component.css' + styleUrls: ['./create-software-profile.component.css'] }) -export class CreateSoftwareProfileComponent { +export class CreateSoftwareProfileComponent implements OnInit { baseUrl: string = import.meta.env.NG_APP_BASE_API_URL; - formGroup: FormGroup; + formGroup: FormGroup; private apiUrl = `${this.baseUrl}/software-profiles`; softwareCollection: any[] = []; organizationalUnits: any[] = []; @@ -45,11 +45,13 @@ export class CreateSoftwareProfileComponent { ngOnInit(): void { if (this.data) { - this.load() + this.softwareProfileId = this.data['@id']; + this.patchFormData(); } + this.loadSoftware(); - this.loadOrganizationalUnits() - this.loadOperativeSystems() + this.loadOrganizationalUnits(); + this.loadOperativeSystems(); this.filteredSoftware = this.softwareControl.valueChanges.pipe( startWith(''), @@ -58,42 +60,50 @@ export class CreateSoftwareProfileComponent { ); } + patchFormData(): void { + this.formGroup.patchValue({ + description: this.data.description || '', + comments: this.data.comments || '', + organizationalUnit: this.data.organizationalUnit ? this.data.organizationalUnit['@id'] : null, + operativeSystem: this.data.operativeSystem ? this.data.operativeSystem['@id'] : null, + }); + } + loadSoftware() { - this.http.get( `${this.baseUrl}/software?&page=1&itemsPerPage=10`).subscribe( + this.http.get(`${this.baseUrl}/software?page=1&itemsPerPage=10`).subscribe( response => { this.softwareCollection = response['hydra:member']; }, error => { - console.error('Error fetching parent units:', error); + console.error('Error fetching software:', error); } ); } loadOrganizationalUnits() { - this.http.get( `${this.baseUrl}/organizational-units?&page=1&itemsPerPage=10000`).subscribe( + this.http.get(`${this.baseUrl}/organizational-units?page=1&itemsPerPage=10000`).subscribe( response => { this.organizationalUnits = response['hydra:member']; }, error => { - console.error('Error fetching parent units:', error); + console.error('Error fetching organizational units:', error); } ); } loadOperativeSystems() { - this.http.get( `${this.baseUrl}/operative-systems?&page=1&itemsPerPage=10000`).subscribe( + this.http.get(`${this.baseUrl}/operative-systems?page=1&itemsPerPage=10000`).subscribe( response => { this.operativeSystems = response['hydra:member']; }, error => { - console.error('Error fetching parent units:', error); + console.error('Error fetching operative systems:', error); } ); } private _filterSoftware(value: string): Observable { - - return this.softwareDataService.getSoftwareCollection({ 'name': value}).pipe( + return this.softwareDataService.getSoftwareCollection({ name: value }).pipe( map(response => response || []) ); } @@ -109,23 +119,6 @@ export class CreateSoftwareProfileComponent { this.softwareControl.setValue(''); } - load(): void { - this.dataService.getSoftwareProfile(this.data).subscribe({ - next: (response) => { - this.formGroup = this.fb.group({ - description: [response.description], - comments: [response.comments], - organizationalUnit: [response.organizationalUnit ? response.organizationalUnit['@id'] : null], - operativeSystem: [response.operativeSystem ? response.operativeSystem['@id'] : null], - }); - this.softwareProfileId = response['@id']; - }, - error: (err) => { - console.error('Error fetching software:', err); - } - }); - } - addSoftware() { const software = this.softwareCollection.find(s => s.id === this.selectedSoftware); if (software && !this.selectedSoftwares.includes(software)) { @@ -144,7 +137,6 @@ export class CreateSoftwareProfileComponent { onSubmit(): void { if (this.formGroup.valid) { - const payload = { description: this.formGroup.value.description, comments: this.formGroup.value.comments, @@ -155,24 +147,24 @@ export class CreateSoftwareProfileComponent { if (this.softwareProfileId) { this.http.put(`${this.baseUrl}${this.softwareProfileId}`, payload).subscribe( - (response) => { + () => { this.toastService.success('Software editado correctamente'); this.dialogRef.close(); }, (error) => { - this.toastService.error(error['error']['hydra:description']); - console.error('Error al editar el comando', error); + this.toastService.error(error.error['hydra:description']); + console.error('Error al editar el software', error); } ); } else { - this.http.post(`${this.baseUrl}/software-profiles`, payload).subscribe( - (response) => { + this.http.post(`${this.apiUrl}`, payload).subscribe( + () => { this.toastService.success('Software añadido correctamente'); this.dialogRef.close(); }, (error) => { - this.toastService.error(error['error']['hydra:description']); - console.error('Error al añadir comando', error); + this.toastService.error(error.error['hydra:description']); + console.error('Error al añadir software', error); } ); } diff --git a/ogWebconsole/src/app/components/software-profile/software-profile.component.ts b/ogWebconsole/src/app/components/software-profile/software-profile.component.ts index 4034adb..4673bab 100644 --- a/ogWebconsole/src/app/components/software-profile/software-profile.component.ts +++ b/ogWebconsole/src/app/components/software-profile/software-profile.component.ts @@ -5,7 +5,6 @@ import {MatDialog} from "@angular/material/dialog"; import {HttpClient} from "@angular/common/http"; import {DataService} from "../software/data.service"; import {ToastrService} from "ngx-toastr"; -import {CreateSoftwareComponent} from "../software/create-software/create-software.component"; import {DeleteModalComponent} from "../../shared/delete_modal/delete-modal/delete-modal.component"; import {PageEvent} from "@angular/material/paginator"; import {CreateSoftwareProfileComponent} from "./create-software-profile/create-software-profile.component"; @@ -90,7 +89,7 @@ export class SoftwareProfileComponent { editSoftware(softwareProfile: any): void { const dialogRef = this.dialog.open(CreateSoftwareProfileComponent, { width: '600px', - data: softwareProfile['@id'] + data: softwareProfile }); dialogRef.afterClosed().subscribe(result => {