From 8474cf73e4859453949c73e75aebb451830ba3f8 Mon Sep 17 00:00:00 2001 From: Constantin Simonis Date: Thu, 9 Jan 2025 09:12:33 +0100 Subject: [PATCH] wip --- src/app/Qualification.ts | 7 ++ .../create-employee.component.ts | 60 ------------- src/app/employee/Employee.ts | 19 ++-- .../create/create.component.css} | 0 .../create/create.component.html} | 9 +- src/app/employee/create/create.component.ts | 87 +++++++++++++++++++ src/app/employee/table/table.component.ts | 3 +- 7 files changed, 116 insertions(+), 69 deletions(-) create mode 100644 src/app/Qualification.ts delete mode 100644 src/app/create-employee/create-employee.component.ts rename src/app/{create-employee/create-employee.component.css => employee/create/create.component.css} (100%) rename src/app/{create-employee/create-employee.component.html => employee/create/create.component.html} (78%) create mode 100644 src/app/employee/create/create.component.ts diff --git a/src/app/Qualification.ts b/src/app/Qualification.ts new file mode 100644 index 0000000..c5821f7 --- /dev/null +++ b/src/app/Qualification.ts @@ -0,0 +1,7 @@ +export class Qualification { + constructor( + public id?: number, + public skill?: string + ) { + } +} diff --git a/src/app/create-employee/create-employee.component.ts b/src/app/create-employee/create-employee.component.ts deleted file mode 100644 index 89b1154..0000000 --- a/src/app/create-employee/create-employee.component.ts +++ /dev/null @@ -1,60 +0,0 @@ -import {Component, inject, OnInit} from '@angular/core'; -import {FormBuilder, FormControl, FormGroup, ReactiveFormsModule, Validators} from "@angular/forms"; -import {MatFormField, MatLabel} from "@angular/material/form-field"; -import {MatInput} from "@angular/material/input"; -import {MatButton} from "@angular/material/button"; -import { - MatDialogActions, - MatDialogClose, - MatDialogContent, - MatDialogRef, - MatDialogTitle -} from "@angular/material/dialog"; -import {Employee} from "../Employee"; -import EmployeeApiService from "../services/employee-api.service"; -import {catchError} from "rxjs"; - -@Component({ - selector: 'app-create-employee', - imports: [ - ReactiveFormsModule, - MatFormField, - MatInput, - MatButton, - MatLabel, - MatDialogContent, - MatDialogTitle, - MatDialogActions, - MatDialogClose - ], - templateUrl: './create-employee.component.html', - standalone: true, - styleUrl: './create-employee.component.css' -}) -export class CreateEmployeeComponent implements OnInit{ - employeeForm!: FormGroup; - employeeService: EmployeeApiService = inject(EmployeeApiService); - formBuilder: FormBuilder = inject(FormBuilder); - dialogRef: MatDialogRef = inject(MatDialogRef); - - ngOnInit(): void { - this.employeeForm = this.formBuilder.group({ - firstName: ['', Validators.required], - lastName: ['', Validators.required], - street: ['', Validators.required], - postcode: ['', [Validators.required, Validators.minLength(5), Validators.maxLength(5)]], - city: ['', Validators.required], - phone: ['', Validators.required], - }); - } - - submit() { - if (!this.employeeForm.valid) { - console.error('Form invalid'); - return; - } - - this.employeeService.create(this.employeeForm.value as Employee).subscribe(); - this.dialogRef.close(); - } -} diff --git a/src/app/employee/Employee.ts b/src/app/employee/Employee.ts index 1debefc..ca15e4f 100644 --- a/src/app/employee/Employee.ts +++ b/src/app/employee/Employee.ts @@ -1,10 +1,15 @@ +import {Qualification} from "../Qualification"; + export class Employee { - constructor(public id?: number, - public lastName?: string, - public firstName?: string, - public street?: string, - public postcode?: string, - public city?: string, - public phone?: string) { + constructor( + public id?: number, + public lastName?: string, + public firstName?: string, + public street?: string, + public postcode?: string, + public city?: string, + public phone?: string, + public skillSet?: Qualification[] + ) { } } diff --git a/src/app/create-employee/create-employee.component.css b/src/app/employee/create/create.component.css similarity index 100% rename from src/app/create-employee/create-employee.component.css rename to src/app/employee/create/create.component.css diff --git a/src/app/create-employee/create-employee.component.html b/src/app/employee/create/create.component.html similarity index 78% rename from src/app/create-employee/create-employee.component.html rename to src/app/employee/create/create.component.html index bf4c399..bd139c9 100644 --- a/src/app/create-employee/create-employee.component.html +++ b/src/app/employee/create/create.component.html @@ -1,6 +1,6 @@

Create Employee

-
+
@@ -36,6 +36,13 @@ + + Skills + + {{ skill.skill }} + + + diff --git a/src/app/employee/create/create.component.ts b/src/app/employee/create/create.component.ts new file mode 100644 index 0000000..98e6a39 --- /dev/null +++ b/src/app/employee/create/create.component.ts @@ -0,0 +1,87 @@ +import {Component, inject, OnInit} from '@angular/core'; +import {FormArray, FormBuilder, FormGroup, ReactiveFormsModule, Validators} from "@angular/forms"; +import {MatFormField, MatLabel} from "@angular/material/form-field"; +import {MatInput} from "@angular/material/input"; +import {MatButton} from "@angular/material/button"; +import { + MatDialogActions, + MatDialogClose, + MatDialogContent, + MatDialogRef, + MatDialogTitle +} from "@angular/material/dialog"; +import {Employee} from "../Employee"; +import EmployeeApiService from "../../services/employee-api.service"; +import {Observable} from "rxjs"; +import {MatOption, MatSelect} from "@angular/material/select"; +import {AsyncPipe, NgForOf, NgIf} from "@angular/common"; +import {Qualification} from "../../Qualification"; +import QualificationService from "../../services/qualification.service"; + +@Component({ + selector: 'app-create-employee', + imports: [ + ReactiveFormsModule, + MatFormField, + MatInput, + MatButton, + MatLabel, + MatDialogContent, + MatDialogTitle, + MatDialogActions, + MatDialogClose, + MatSelect, + MatOption, + NgForOf, + NgIf, + AsyncPipe, + ], + templateUrl: './create.component.html', + standalone: true, + styleUrl: './create.component.css' +}) +export class CreateComponent implements OnInit { + employeeForm: FormGroup | null = null; + employeeService: EmployeeApiService = inject(EmployeeApiService); + formBuilder: FormBuilder = inject(FormBuilder); + dialogRef: MatDialogRef = inject(MatDialogRef); + qualificationService: QualificationService = inject(QualificationService); + skills!: Observable + formSkills: FormArray = this.formBuilder.array([]); + + ngOnInit(): void { + this.employeeForm = this.formBuilder.group({ + firstName: ['', Validators.required], + lastName: ['', Validators.required], + street: ['', Validators.required], + postcode: ['', [Validators.required, Validators.minLength(5), Validators.maxLength(5)]], + city: ['', Validators.required], + phone: ['', Validators.required], + skillSet: this.formSkills + }); + + this.skills = this.qualificationService.getAll(); + + this.skills.subscribe((qualifications: Qualification[]) => { + qualifications.forEach((qualification: Qualification) => { + + this.formSkills.push(this.formBuilder.group(qualification)); + }); + }) + } + + submit() { + console.log(this.employeeForm?.controls['skillSet'].value) + if (this.employeeForm === null || !this.employeeForm.valid) { + console.error('Form invalid'); + return; + } + const newEmployee = this.employeeForm.value as Employee; + //newEmployee.skillSet = newEmployee.skillSet?.map((selected: boolean, index: number) => {return }) + + return; + + this.employeeService.create(newEmployee).subscribe(); + this.dialogRef.close(); + } +} diff --git a/src/app/employee/table/table.component.ts b/src/app/employee/table/table.component.ts index edef320..def3f10 100644 --- a/src/app/employee/table/table.component.ts +++ b/src/app/employee/table/table.component.ts @@ -17,6 +17,7 @@ import {MatSortModule} from '@angular/material/sort'; import {MatDialog} from "@angular/material/dialog"; import {DeleteComponent} from "../delete/delete.component"; import EmployeeApiService from "../../services/employee-api.service"; +import {CreateComponent} from "../create/create.component"; @Component({ selector: 'app-employee-list', @@ -76,7 +77,7 @@ export class TableComponent implements OnInit{ } protected showCreateEmployeeModal() { - this.createEmployeeDialogue.open(CreateEmployeeComponent) + this.createEmployeeDialogue.open(CreateComponent) .afterClosed() .subscribe(() => { this.employees$ = this.fetchEmployees();