88 lines
2.7 KiB
TypeScript
88 lines
2.7 KiB
TypeScript
import { HttpClient } from '@angular/common/http';
|
|
import { Component, Inject, OnInit } from '@angular/core';
|
|
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
|
|
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
|
|
import { ToastrService } from 'ngx-toastr';
|
|
|
|
@Component({
|
|
selector: 'app-create-pxe-template',
|
|
templateUrl: './create-pxe-template.component.html',
|
|
styleUrls: ['./create-pxe-template.component.css']
|
|
})
|
|
export class CreatePxeTemplateComponent implements OnInit {
|
|
templateForm!: FormGroup;
|
|
previewContent: string = '';
|
|
isEditMode: boolean = false;
|
|
|
|
constructor(
|
|
public dialogRef: MatDialogRef<CreatePxeTemplateComponent>,
|
|
private http: HttpClient,
|
|
private fb: FormBuilder,
|
|
private toastService: ToastrService,
|
|
@Inject(MAT_DIALOG_DATA) public data: any
|
|
) {}
|
|
|
|
ngOnInit() {
|
|
this.isEditMode = !!this.data; // Verifica si hay datos inyectados (modo edición)
|
|
|
|
this.templateForm = this.fb.group({
|
|
name: [this.data?.name || '', Validators.required],
|
|
templateContent: [this.data?.templateContent || '', Validators.required]
|
|
});
|
|
}
|
|
|
|
onSave(): void {
|
|
if (this.isEditMode) {
|
|
this.updatePxeTemplate();
|
|
} else {
|
|
this.createPxeTemplate();
|
|
}
|
|
}
|
|
|
|
createPxeTemplate(): void {
|
|
const formValues = this.templateForm.value;
|
|
const payload = {
|
|
name: formValues.name,
|
|
templateContent: formValues.templateContent
|
|
};
|
|
|
|
this.http.post<any>('http://127.0.0.1:8081/pxe-templates', payload).subscribe({
|
|
next: data => {
|
|
console.log('Plantilla PXE creada:', data);
|
|
this.toastService.success('Plantilla PXE creada exitosamente');
|
|
this.dialogRef.close(true);
|
|
},
|
|
error: error => {
|
|
console.error('Error al crear la plantilla PXE:', error);
|
|
this.toastService.error('Error al crear la plantilla PXE');
|
|
this.dialogRef.close(false);
|
|
}
|
|
});
|
|
}
|
|
|
|
updatePxeTemplate(): void {
|
|
const formValues = this.templateForm.value;
|
|
const payload = {
|
|
name: formValues.name,
|
|
templateContent: formValues.templateContent
|
|
};
|
|
|
|
this.http.patch<any>(`http://127.0.0.1:8081/pxe-templates/${this.data.uuid}`, payload).subscribe({
|
|
next: data => {
|
|
console.log('Plantilla PXE actualizada:', data);
|
|
this.toastService.success('Plantilla PXE actualizada exitosamente');
|
|
this.dialogRef.close(true);
|
|
},
|
|
error: error => {
|
|
console.error('Error al actualizar la plantilla PXE:', error);
|
|
this.toastService.error('Error al actualizar la plantilla PXE');
|
|
this.dialogRef.close(false);
|
|
}
|
|
});
|
|
}
|
|
|
|
onCancel(): void {
|
|
this.dialogRef.close(false);
|
|
}
|
|
}
|