add form to create employee #18
7
src/app/Qualification.ts
Normal file
7
src/app/Qualification.ts
Normal file
@ -0,0 +1,7 @@
|
||||
export class Qualification {
|
||||
constructor(
|
||||
public id?: number,
|
||||
public skill?: string
|
||||
) {
|
||||
}
|
||||
}
|
@ -1,10 +1,15 @@
|
||||
import {Qualification} from "../Qualification";
|
||||
|
||||
export class Employee {
|
||||
constructor(public id?: number,
|
||||
constructor(
|
||||
public id?: number,
|
||||
public lastName?: string,
|
||||
public firstName?: string,
|
||||
public street?: string,
|
||||
public postcode?: string,
|
||||
public city?: string,
|
||||
public phone?: string) {
|
||||
public phone?: string,
|
||||
public skillSet?: Qualification[]
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
0
src/app/employee/create/create.component.css
Normal file
0
src/app/employee/create/create.component.css
Normal file
46
src/app/employee/create/create.component.html
Normal file
46
src/app/employee/create/create.component.html
Normal file
@ -0,0 +1,46 @@
|
||||
<h2 mat-dialog-title>Create Employee</h2>
|
||||
<mat-dialog-content>
|
||||
<form *ngIf="employeeForm" [formGroup]="employeeForm" (ngSubmit)="submit()">
|
||||
<div class="!space-y-4">
|
||||
<div class="flex gap-x-4">
|
||||
<mat-form-field class="!w-full">
|
||||
<mat-label>First Name</mat-label>
|
||||
<input matInput formControlName="firstName" required>
|
||||
</mat-form-field>
|
||||
|
||||
<mat-form-field class="!w-full">
|
||||
<mat-label>Last Name</mat-label>
|
||||
<input matInput formControlName="lastName" required>
|
||||
</mat-form-field>
|
||||
</div>
|
||||
|
||||
<mat-form-field class="!w-full">
|
||||
<mat-label>Street</mat-label>
|
||||
<input matInput formControlName="street" required>
|
||||
</mat-form-field>
|
||||
|
||||
<div class="flex gap-x-4">
|
||||
<mat-form-field class="!w-full">
|
||||
<mat-label>City</mat-label>
|
||||
<input matInput formControlName="city" required>
|
||||
</mat-form-field>
|
||||
|
||||
<mat-form-field class="!w-1/2">
|
||||
<mat-label>Postcode</mat-label>
|
||||
<input matInput formControlName="postcode" minlength="5" maxlength="5" type="number" required>
|
||||
</mat-form-field>
|
||||
</div>
|
||||
|
||||
<mat-form-field class="!w-full">
|
||||
<mat-label>Phone</mat-label>
|
||||
<input matInput formControlName="phone" required type="number">
|
||||
</mat-form-field>
|
||||
|
||||
<mat-dialog-actions align="end">
|
||||
<button mat-button mat-dialog-close>Cancel</button>
|
||||
<button mat-flat-button color="primary" type="submit">Submit</button>
|
||||
</mat-dialog-actions>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
</mat-dialog-content>
|
61
src/app/employee/create/create.component.ts
Normal file
61
src/app/employee/create/create.component.ts
Normal file
@ -0,0 +1,61 @@
|
||||
import {Component, inject, OnInit} from '@angular/core';
|
||||
import {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 {NgIf} from "@angular/common";
|
||||
|
||||
@Component({
|
||||
selector: 'app-create-employee',
|
||||
imports: [
|
||||
ReactiveFormsModule,
|
||||
MatFormField,
|
||||
MatInput,
|
||||
MatButton,
|
||||
MatLabel,
|
||||
MatDialogContent,
|
||||
MatDialogTitle,
|
||||
MatDialogActions,
|
||||
MatDialogClose,
|
||||
NgIf,
|
||||
],
|
||||
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<CreateComponent> = 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 === null || !this.employeeForm.valid) {
|
||||
console.error('Form invalid');
|
||||
return;
|
||||
}
|
||||
|
||||
this.employeeService.create(this.employeeForm.value as Employee).subscribe();
|
||||
this.dialogRef.close();
|
||||
}
|
||||
}
|
@ -4,7 +4,7 @@
|
||||
<div class="!space-y-6">
|
||||
<div class="!flex !justify-between !items-center">
|
||||
<h2 class="!text-2xl !font-semibold !text-gray-900">Employee Directory</h2>
|
||||
<button mat-flat-button color="primary" class="!bg-blue-600 !text-white">
|
||||
<button mat-flat-button color="primary" class="!bg-blue-600 !text-white" (click)="showCreateEmployeeModal()">
|
||||
<mat-icon class="!mr-2">add</mat-icon>
|
||||
Add Employee
|
||||
</button>
|
||||
|
@ -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',
|
||||
@ -41,6 +42,7 @@ export class TableComponent implements OnInit{
|
||||
private readonly apiService: EmployeeApiService = inject(EmployeeApiService);
|
||||
private readonly snackBar: MatSnackBar = inject(MatSnackBar);
|
||||
private readonly deleteDialogue: MatDialog = inject(MatDialog);
|
||||
private readonly createEmployeeDialogue: MatDialog = inject(MatDialog);
|
||||
|
||||
private static readonly MAX_RETRIES = 3;
|
||||
public employees$: Observable<Employee[]> = of([]);
|
||||
@ -73,4 +75,12 @@ export class TableComponent implements OnInit{
|
||||
protected openDeleteDialogue(employee: Employee): void {
|
||||
this.deleteDialogue.open(DeleteComponent, {data: employee});
|
||||
}
|
||||
|
||||
protected showCreateEmployeeModal() {
|
||||
this.createEmployeeDialogue.open(CreateComponent)
|
||||
.afterClosed()
|
||||
.subscribe(() => {
|
||||
this.employees$ = this.fetchEmployees();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -19,4 +19,8 @@ export default class EmployeeApiService {
|
||||
public getAll(): Observable<Employee[]> {
|
||||
return this.http.get<Employee[]>(`${EmployeeApiService.BASE_URL}/employees`)
|
||||
}
|
||||
|
||||
public create(employee: Employee) {
|
||||
return this.http.post<Employee>(`${EmployeeApiService.BASE_URL}/employees`, employee)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user