refactor: transfer responsibility for the data to the parent

This commit is contained in:
Polianin Nikita 2024-02-17 12:17:30 +03:00
parent 8879c7f530
commit 9b1903f465

View File

@ -1,6 +1,5 @@
import {Component, EventEmitter, Output, ViewChild} from '@angular/core';
import {Component, EventEmitter, Input, Output, ViewChild} from '@angular/core';
import {MatExpansionModule, MatExpansionPanel} from "@angular/material/expansion";
import {ApiService} from "@service/api.service";
import {DataSpinnerComponent} from "@component/data-spinner/data-spinner.component";
import {MatChipListboxChange, MatChipsModule} from '@angular/material/chips';
import {FormControl, ReactiveFormsModule} from "@angular/forms";
@ -23,12 +22,11 @@ import {GroupResponse} from "@model/groupResponse";
styleUrl: './schedule-tabs-group.component.css'
})
export class ScheduleTabsGroupComponent {
protected faculties: Observable<FacultyResponse[]>;
protected facultiesId: number | null = null;
protected courseNumber: number | null = null;
protected groups: Observable<GroupResponse[]> = of([]);
protected filteredGroups: Observable<GroupResponse[]> = of([]);
protected courseNumbers: Observable<number[]> = of([]);
protected groups: Observable<GroupResponse[]> = of([]);
protected chipCourse: FormControl = new FormControl();
protected chipGroup: FormControl = new FormControl();
@ -36,12 +34,20 @@ export class ScheduleTabsGroupComponent {
@ViewChild('courseNumberPanel') courseNumberPanel!: MatExpansionPanel;
@ViewChild('groupPanel') groupPanel!: MatExpansionPanel;
@Output() groupSelected = new EventEmitter<number>();
@Input() faculties: Observable<FacultyResponse[]> = of([]);
constructor(private api: ApiService) {
this.faculties = api.get<FacultyResponse[]>("Faculty/Get");
@Input() set groupsSet(data: Observable<GroupResponse[]>) {
this.groups = data;
this.courseNumbers = this.groups.pipe(
map(data => data.map(g => g.courseNumber)),
map(courseNumbersArray => courseNumbersArray.filter((value, index, self) => self.indexOf(value) === index)),
map(uniqueCourseNumbers => uniqueCourseNumbers.sort((a, b) => a - b))
);
}
@Output() groupSelected = new EventEmitter<number>();
@Output() facultySelected = new EventEmitter<number>();
protected selectedFaculty(event: MatChipListboxChange) {
this.courseNumber = null;
this.groups = of([]);
@ -55,12 +61,7 @@ export class ScheduleTabsGroupComponent {
this.facultiesId = event.value;
this.courseNumberPanel.open();
this.groups = this.api.get<GroupResponse[]>("Group/GetByFacultyId/" + this.facultiesId);
this.courseNumbers = this.groups.pipe(
map(data => data.map(g => g.courseNumber)),
map(courseNumbersArray => courseNumbersArray.filter((value, index, self) => self.indexOf(value) === index)),
map(uniqueCourseNumbers => uniqueCourseNumbers.sort((a, b) => a - b))
);
this.facultySelected.emit(this.facultiesId!);
}
protected selectCourseNumber(event: MatChipListboxChange) {