refactor: bind components to api
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
<div class="search-content">
|
||||
@if (professors.length === 0) {
|
||||
<app-loading-indicator [loading]="professorsLoaded !== null" (retryFunction)="professorsLoadRetry.emit()"/>
|
||||
<app-loading-indicator [loading]="professorsLoaded !== null" (retryFunction)="loadProfessors()"/>
|
||||
} @else {
|
||||
<mat-form-field color="accent" style="width: 100%;">
|
||||
<input type="text" placeholder="Поиск..." matInput [formControl]="professorControl" [matAutocomplete]="auto">
|
||||
|
@ -3,9 +3,10 @@ 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 {catchError, map, Observable, startWith} from "rxjs";
|
||||
import {LoadingIndicatorComponent} from "@component/common/loading-indicator/loading-indicator.component";
|
||||
import {ProfessorResponse} from "@api/v1/professorResponse";
|
||||
import {ProfessorService} from "@api/v1/professor.service";
|
||||
|
||||
@Component({
|
||||
selector: 'app-professor',
|
||||
@ -19,17 +20,37 @@ import {ProfessorResponse} from "@api/v1/professorResponse";
|
||||
LoadingIndicatorComponent
|
||||
],
|
||||
templateUrl: './professor.component.html',
|
||||
styleUrl: './professor.component.css'
|
||||
styleUrl: './professor.component.css',
|
||||
providers: [ProfessorService]
|
||||
})
|
||||
export class ProfessorComponent implements OnInit {
|
||||
protected professorControl = new FormControl();
|
||||
protected filteredProfessors!: Observable<ProfessorResponse[]>;
|
||||
|
||||
@Input() professors: ProfessorResponse[] = [];
|
||||
@Output() professorSelected = new EventEmitter<number>();
|
||||
protected professors: ProfessorResponse[] = [];
|
||||
protected professorsLoaded: boolean | null = false;
|
||||
|
||||
@Input() professorsLoaded: boolean | null = false;
|
||||
@Output() professorsLoadRetry: EventEmitter<void> = new EventEmitter<void>();
|
||||
@Output() eventResult = new EventEmitter<number>();
|
||||
|
||||
constructor(private api: ProfessorService) {
|
||||
this.loadProfessors();
|
||||
}
|
||||
|
||||
protected loadProfessors() {
|
||||
if (this.professors.length === 0) {
|
||||
this.professorsLoaded = false;
|
||||
|
||||
this.api.getProfessors()
|
||||
.pipe(catchError(error => {
|
||||
this.professorsLoaded = null;
|
||||
throw error;
|
||||
}))
|
||||
.subscribe(data => {
|
||||
this.professors = data;
|
||||
this.professorsLoaded = true;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
ngOnInit(): void {
|
||||
this.filteredProfessors = this.professorControl.valueChanges.pipe(
|
||||
@ -53,7 +74,7 @@ export class ProfessorComponent implements OnInit {
|
||||
const selectedOption = this.professors.find(teacher => teacher.id === event.option.value);
|
||||
if (selectedOption) {
|
||||
this.professorControl.setValue(selectedOption.name);
|
||||
this.professorSelected.emit(selectedOption.id);
|
||||
this.eventResult.emit(selectedOption.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user