feat(search): implement search functionality for employees

This commit is contained in:
Jan Gleytenhoover 2025-01-15 13:29:30 +01:00
parent 9219b92049
commit d80f98f2a0
Signed by: jank
GPG key ID: 50620ADD22CD330B
3 changed files with 42 additions and 9 deletions

View file

@ -19,7 +19,7 @@ h1 {
.search-bar { .search-bar {
display: flex; display: flex;
margin-bottom: 20px; margin-bottom: 10px;
align-items: center; align-items: center;
} }

View file

@ -13,10 +13,13 @@
</div> </div>
<div class="header-actions"> <div class="header-actions">
<div class="search-bar"> <form [formGroup]="searchForm">
<input type="text" placeholder="Search employee"> <div class="search-bar">
<button>Search</button> <input type="text" placeholder="Search employee" formControlName="search">
</div> <button (click)="submit()">Search</button>
</div>
<p class="text-body-tertiary">Search for a propertiy of an Employee. eg. First Name</p>
</form>
<button class="add-button">Add employee</button> <button class="add-button">Add employee</button>
</div> </div>
@ -33,7 +36,8 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@for (employee of employees | async; track employee) { @if (employees) {
@for (employee of employees; track employee) {
<tr> <tr>
<td>{{ employee.firstName }}</td> <td>{{ employee.firstName }}</td>
<td>{{ employee.lastName }}</td> <td>{{ employee.lastName }}</td>
@ -47,6 +51,7 @@
</td> </td>
</tr> </tr>
} }
}
</tbody> </tbody>
</table> </table>
</div> </div>

View file

@ -3,20 +3,48 @@ import { CommonModule } from '@angular/common';
import { Observable, of } from 'rxjs'; import { Observable, of } from 'rxjs';
import { EmployeeResponseDTO } from '../../models/mitarbeiter'; import { EmployeeResponseDTO } from '../../models/mitarbeiter';
import { EmployeeService } from '../../service/employee.service'; import { EmployeeService } from '../../service/employee.service';
import { FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
@Component({ @Component({
selector: 'app-mitarbeiterverwaltung-view', selector: 'app-mitarbeiterverwaltung-view',
templateUrl: './mitarbeiterverwaltung-view.component.html', templateUrl: './mitarbeiterverwaltung-view.component.html',
styleUrls: ['./mitarbeiterverwaltung-view.component.css'], styleUrls: ['./mitarbeiterverwaltung-view.component.css'],
standalone: true, standalone: true,
imports: [CommonModule] imports: [CommonModule, ReactiveFormsModule]
}) })
export class MitarbeiterverwaltungViewComponent implements OnInit { export class MitarbeiterverwaltungViewComponent implements OnInit {
employees: Observable<Array<EmployeeResponseDTO>> = of([]); employees: Array<EmployeeResponseDTO> = [];
searchForm!: FormGroup;
constructor(private employeeService: EmployeeService) {} constructor(private employeeService: EmployeeService) {}
submit() {
const searchTerm = this.searchForm.get("search")?.value || '';
this.employeeService.getAllEmployees().subscribe(employees => {
let foundEmployees: Array<EmployeeResponseDTO> = [];
for (const employee of employees) {
if (
employee.firstName.toLowerCase().includes(searchTerm.toLowerCase()) ||
employee.lastName.toLowerCase().includes(searchTerm.toLowerCase()) ||
employee.street.toLowerCase().includes(searchTerm.toLowerCase()) ||
employee.postcode.toLowerCase().includes(searchTerm.toLowerCase()) ||
employee.city.toLowerCase().includes(searchTerm.toLowerCase()) ||
employee.phone.toLowerCase().includes(searchTerm.toLowerCase()) ||
searchTerm == ''
) {
foundEmployees.push(employee);
}
}
this.employees = foundEmployees;
});
}
ngOnInit(): void { ngOnInit(): void {
this.employees = this.employeeService.getAllEmployees(); this.searchForm = new FormGroup({
search: new FormControl(''),
});
this.employeeService.getAllEmployees().subscribe(employees => this.employees = employees);
} }
} }