refactor: folders and components
This commit is contained in:
@ -0,0 +1,5 @@
|
||||
.search-content {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
<div class="search-content">
|
||||
@if (professors.length === 0) {
|
||||
<app-loading-indicator [loading]="professorsLoaded !== null" (retryFunction)="professorsLoadRetry.emit()"/>
|
||||
} @else {
|
||||
<mat-form-field color="accent" style="width: 100%;">
|
||||
<input type="text" placeholder="Поиск..." matInput [formControl]="professorControl" [matAutocomplete]="auto">
|
||||
|
||||
<mat-autocomplete #auto="matAutocomplete" (optionSelected)="onOptionSelected($event)"
|
||||
[autoActiveFirstOption]="false" [hideSingleSelectionIndicator]="true">
|
||||
@for (option of filteredProfessors | async; track option) {
|
||||
<mat-option [value]="option.id">
|
||||
{{ option.name }}
|
||||
</mat-option>
|
||||
}
|
||||
</mat-autocomplete>
|
||||
</mat-form-field>
|
||||
}
|
||||
</div>
|
@ -0,0 +1,59 @@
|
||||
import {Component, EventEmitter, Input, OnInit, Output} from "@angular/core";
|
||||
import {MatFormField, MatInput} from "@angular/material/input";
|
||||
import {FormControl, ReactiveFormsModule} from "@angular/forms";
|
||||
import {MatAutocompleteModule, MatAutocompleteSelectedEvent} from "@angular/material/autocomplete";
|
||||
import {AsyncPipe} from "@angular/common";
|
||||
import {map, Observable, startWith} from "rxjs";
|
||||
import {ProfessorResponse} from "@model/professorResponse";
|
||||
import {LoadingIndicatorComponent} from "@component/common/loading-indicator/loading-indicator.component";
|
||||
|
||||
@Component({
|
||||
selector: 'app-professor',
|
||||
standalone: true,
|
||||
imports: [
|
||||
MatAutocompleteModule,
|
||||
MatFormField,
|
||||
AsyncPipe,
|
||||
ReactiveFormsModule,
|
||||
MatInput,
|
||||
LoadingIndicatorComponent
|
||||
],
|
||||
templateUrl: './professor.component.html',
|
||||
styleUrl: './professor.component.css'
|
||||
})
|
||||
export class ProfessorComponent implements OnInit {
|
||||
protected professorControl = new FormControl();
|
||||
protected filteredProfessors!: Observable<ProfessorResponse[]>;
|
||||
|
||||
@Input() professors: ProfessorResponse[] = [];
|
||||
@Output() professorSelected = new EventEmitter<number>();
|
||||
|
||||
@Input() professorsLoaded: boolean | null = false;
|
||||
@Output() professorsLoadRetry: EventEmitter<void> = new EventEmitter<void>();
|
||||
|
||||
ngOnInit(): void {
|
||||
this.filteredProfessors = this.professorControl.valueChanges.pipe(
|
||||
startWith(''),
|
||||
map(value => this._filterProfessors(value))
|
||||
);
|
||||
}
|
||||
|
||||
private _filterProfessors(value: string | number): ProfessorResponse[] {
|
||||
if (typeof value === 'string') {
|
||||
if (value === '') return [];
|
||||
const filterValue = value.toLowerCase();
|
||||
return this.professors.filter(teacher => teacher.name.toLowerCase().includes(filterValue));
|
||||
} else {
|
||||
const selectedTeacher = this.professors.find(teacher => teacher.id === value);
|
||||
return selectedTeacher ? [selectedTeacher] : [];
|
||||
}
|
||||
}
|
||||
|
||||
protected onOptionSelected(event: MatAutocompleteSelectedEvent) {
|
||||
const selectedOption = this.professors.find(teacher => teacher.id === event.option.value);
|
||||
if (selectedOption) {
|
||||
this.professorControl.setValue(selectedOption.name);
|
||||
this.professorSelected.emit(selectedOption.id);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user