change file structure
Reviewed-on: #21 Reviewed-by: Get in my car i have candy <huydw@proton.me>
This commit is contained in:
84
src/app/qualification/edit/edit.component.ts
Normal file
84
src/app/qualification/edit/edit.component.ts
Normal file
@ -0,0 +1,84 @@
|
||||
import {Component, inject} from '@angular/core';
|
||||
import {FormBuilder, FormsModule, ReactiveFormsModule, Validators} from "@angular/forms";
|
||||
import QualificationService from "../../services/qualification.service";
|
||||
import {
|
||||
MAT_DIALOG_DATA,
|
||||
MatDialogActions,
|
||||
MatDialogContent,
|
||||
MatDialogRef,
|
||||
MatDialogTitle
|
||||
} from "@angular/material/dialog";
|
||||
import {MatButton} from "@angular/material/button";
|
||||
import {MatError, MatFormField, MatLabel} from "@angular/material/form-field";
|
||||
import {MatInput} from "@angular/material/input";
|
||||
import {NgIf} from "@angular/common";
|
||||
import {Qualification} from "../Qualification";
|
||||
|
||||
@Component({
|
||||
selector: 'app-edit-qualification',
|
||||
imports: [
|
||||
FormsModule,
|
||||
MatButton,
|
||||
MatDialogActions,
|
||||
MatDialogContent,
|
||||
MatDialogTitle,
|
||||
MatError,
|
||||
MatFormField,
|
||||
MatInput,
|
||||
MatLabel,
|
||||
NgIf,
|
||||
ReactiveFormsModule
|
||||
],
|
||||
templateUrl: './edit.component.html',
|
||||
styleUrl: './edit.component.css'
|
||||
})
|
||||
export class EditComponent {
|
||||
public apiErrorMessage: string = '';
|
||||
public qualification: Qualification = inject(MAT_DIALOG_DATA);
|
||||
|
||||
private formBuilder: FormBuilder = inject(FormBuilder);
|
||||
private qualificationService: QualificationService = inject(QualificationService);
|
||||
private dialogRef: MatDialogRef<EditComponent> = inject(MatDialogRef);
|
||||
|
||||
qualificationForm = this.formBuilder.group({
|
||||
'skill': [this.qualification.skill, Validators.required],
|
||||
});
|
||||
|
||||
isFieldInvalid(fieldName: string): boolean {
|
||||
const field = this.qualificationForm.get(fieldName);
|
||||
|
||||
if (!field) {
|
||||
throw new Error('Form field does not exist: ' + fieldName)
|
||||
}
|
||||
|
||||
return field.invalid && (field.dirty || field.touched);
|
||||
}
|
||||
|
||||
getErrorMessage(fieldName: string): string {
|
||||
const field = this.qualificationForm.get(fieldName);
|
||||
|
||||
if (field?.errors?.['required']) {
|
||||
return 'This field is required';
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
edit() {
|
||||
if (!this.qualificationForm.valid) {
|
||||
console.error('Validation failed');
|
||||
return;
|
||||
}
|
||||
|
||||
this.qualificationService.edit(this.qualification.id, this.qualificationForm.value).subscribe({
|
||||
next: (createdQualification) => {
|
||||
this.dialogRef.close(createdQualification);
|
||||
},
|
||||
error: (error) => {
|
||||
console.error('Error creating qualification:', error);
|
||||
|
||||
this.apiErrorMessage = 'API Error';
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user