feat: implement a search for professors
This commit is contained in:
@ -0,0 +1,71 @@
|
||||
import {Component, EventEmitter, Input, OnInit, Output} from "@angular/core";
|
||||
import {MatFormField, MatInput} from "@angular/material/input";
|
||||
import {FormControl, FormsModule, ReactiveFormsModule} from "@angular/forms";
|
||||
import {
|
||||
MatAutocomplete,
|
||||
MatAutocompleteSelectedEvent,
|
||||
MatAutocompleteTrigger,
|
||||
MatOptgroup,
|
||||
MatOption
|
||||
} from "@angular/material/autocomplete";
|
||||
import {AsyncPipe} from "@angular/common";
|
||||
import {DataSpinnerComponent} from "@component/data-spinner/data-spinner.component";
|
||||
import {map, Observable, startWith} from "rxjs";
|
||||
|
||||
|
||||
export interface Professors {
|
||||
id: number,
|
||||
name: string
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'app-schedule-tabs-professor',
|
||||
standalone: true,
|
||||
imports: [
|
||||
MatFormField,
|
||||
MatInput,
|
||||
FormsModule,
|
||||
MatAutocompleteTrigger,
|
||||
MatAutocomplete,
|
||||
MatOptgroup,
|
||||
ReactiveFormsModule,
|
||||
MatOption,
|
||||
AsyncPipe,
|
||||
DataSpinnerComponent
|
||||
],
|
||||
templateUrl: './schedule-tabs-professor.component.html',
|
||||
styleUrl: './schedule-tabs-professor.component.css'
|
||||
})
|
||||
export class ScheduleTabsProfessorComponent implements OnInit {
|
||||
protected teacherControl = new FormControl();
|
||||
protected filteredTeachers!: Observable<Professors[]>;
|
||||
|
||||
@Input() teachers: Professors[] = [];
|
||||
@Output() optionSelected = new EventEmitter<number>();
|
||||
|
||||
ngOnInit(): void {
|
||||
this.filteredTeachers = this.teacherControl.valueChanges.pipe(
|
||||
startWith(''),
|
||||
map(value => this._filterTeachers(value))
|
||||
);
|
||||
}
|
||||
|
||||
private _filterTeachers(value: string | number): Professors[] {
|
||||
if (typeof value === 'string') {
|
||||
if (value === '') return [];
|
||||
const filterValue = value.toLowerCase();
|
||||
return this.teachers.filter(teacher => teacher.name.toLowerCase().includes(filterValue));
|
||||
} else {
|
||||
const selectedTeacher = this.teachers.find(teacher => teacher.id === value);
|
||||
return selectedTeacher ? [selectedTeacher] : [];
|
||||
}
|
||||
}
|
||||
|
||||
protected onOptionSelected(event: MatAutocompleteSelectedEvent) {
|
||||
const selectedOption = this.teachers.find(teacher => teacher.id === event.option.value);
|
||||
if (selectedOption) {
|
||||
this.teacherControl.setValue(selectedOption.name);
|
||||
this.optionSelected.emit(selectedOption.id);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user