64 lines
2.0 KiB
TypeScript
64 lines
2.0 KiB
TypeScript
import { Component, Inject } from '@angular/core';
|
|
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
|
|
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
|
|
import { HttpClient } from '@angular/common/http';
|
|
|
|
@Component({
|
|
selector: 'app-create-command',
|
|
templateUrl: './create-command.component.html',
|
|
styleUrls: ['./create-command.component.css']
|
|
})
|
|
export class CreateCommandComponent {
|
|
createCommandForm!: FormGroup;
|
|
isEditMode: boolean;
|
|
private apiUrl = 'http://127.0.0.1:8080/commands'; // URL para añadir o editar el comando
|
|
|
|
constructor(
|
|
private fb: FormBuilder,
|
|
private http: HttpClient,
|
|
public dialogRef: MatDialogRef<CreateCommandComponent>,
|
|
@Inject(MAT_DIALOG_DATA) public data: any
|
|
) {
|
|
this.isEditMode = data && data.command; // Verifica si `data` y `data.command` existen
|
|
}
|
|
|
|
ngOnInit(): void {
|
|
this.createCommandForm = this.fb.group({
|
|
name: [this.isEditMode ? this.data.command.name : '', Validators.required],
|
|
script: [this.isEditMode ? this.data.command.script : '', Validators.required],
|
|
readOnly: [this.isEditMode ? this.data.command.readOnly : false],
|
|
enabled: [this.isEditMode ? this.data.command.enabled : true],
|
|
comments: [this.isEditMode ? this.data.command.comments : '']
|
|
});
|
|
}
|
|
|
|
onCancel(): void {
|
|
this.dialogRef.close();
|
|
}
|
|
|
|
onSubmit(): void {
|
|
if (this.createCommandForm.valid) {
|
|
const commandData = this.createCommandForm.value;
|
|
if (this.isEditMode) {
|
|
this.http.patch(`${this.apiUrl}/${this.data.command.uuid}`, commandData).subscribe(
|
|
response => {
|
|
this.dialogRef.close(commandData);
|
|
},
|
|
error => {
|
|
console.error('Error editing command', error);
|
|
}
|
|
);
|
|
} else {
|
|
this.http.post(this.apiUrl, commandData).subscribe(
|
|
response => {
|
|
this.dialogRef.close(true);
|
|
},
|
|
error => {
|
|
console.error('Error adding command', error);
|
|
}
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|