add form to create employee #18

Merged
csimonis merged 7 commits from feature/add-employee into main 2025-01-09 09:59:02 +00:00
6 changed files with 104 additions and 1 deletions
Showing only changes of commit d96a0342d0 - Show all commits

View File

@ -0,0 +1,45 @@
<h2 mat-dialog-title>Create Employee</h2>
<mat-dialog-content>
<form [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" required>
</mat-form-field>
</div>
<mat-form-field class="!w-full">
<mat-label>Phone</mat-label>
<input matInput formControlName="phone" required>
</mat-form-field>
<mat-dialog-actions align="end">
<button mat-flat-button color="primary" type="submit">Submit</button>
</mat-dialog-actions>
</div>
</form>
</mat-dialog-content>

View File

@ -0,0 +1,49 @@
import {Component, inject, OnInit} from '@angular/core';
import {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, MatDialogContent, MatDialogTitle} from "@angular/material/dialog";
import {Employee} from "../Employee";
import EmployeeApiService from "../services/employee-api.service";
@Component({
selector: 'app-create-employee',
imports: [
ReactiveFormsModule,
MatFormField,
MatInput,
MatButton,
MatLabel,
MatDialogContent,
MatDialogTitle,
MatDialogActions
],
templateUrl: './create-employee.component.html',
standalone: true,
styleUrl: './create-employee.component.css'
})
export class CreateEmployeeComponent implements OnInit{
employeeForm!: FormGroup;
employeeService: EmployeeApiService = inject(EmployeeApiService);
ngOnInit(): void {
this.employeeForm = new FormGroup({
firstName: new FormControl('', {validators: [Validators.required]}),
lastName: new FormControl('', {validators: [Validators.required]}),
street: new FormControl('', {validators: [Validators.required]}),
postcode: new FormControl('', {validators: [Validators.required, Validators.minLength(5), Validators.maxLength(5)]}),
city: new FormControl('', {validators: [Validators.required]}),
phone: new FormControl('', {validators: [Validators.required]}),
});
}
submit() {
if (!this.employeeForm.valid) {
console.error('Form invalid');
return;
}
this.employeeService.create(this.employeeForm.value as Employee).subscribe();
}
ptran marked this conversation as resolved Outdated
Outdated
Review

For what is 'as Employee'?

For what is 'as Employee'?
}

View File

@ -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>

View File

@ -41,6 +41,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 +74,8 @@ export class TableComponent implements OnInit{
protected openDeleteDialogue(employee: Employee): void {
this.deleteDialogue.open(DeleteComponent, {data: employee});
}
protected showCreateEmployeeModal() {
this.createEmployeeDialogue.open(CreateEmployeeComponent);
}
}

View File

@ -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)
}
}