refactor: bind components to api
This commit is contained in:
@ -1,38 +1,31 @@
|
||||
import {Component, EventEmitter, Output, ViewChild} from '@angular/core';
|
||||
import {HttpClientModule} from "@angular/common/http";
|
||||
import {Component, EventEmitter, Output} from '@angular/core';
|
||||
import {OtherComponent} from "@component/schedule/tabs/other/other.component";
|
||||
import {MatTab, MatTabChangeEvent, MatTabGroup} from "@angular/material/tabs";
|
||||
import {catchError, map, Observable, of, switchMap, tap} from "rxjs";
|
||||
import {map, Observable} from "rxjs";
|
||||
import {ReactiveFormsModule} from "@angular/forms";
|
||||
import {AsyncPipe, NgIf} from "@angular/common";
|
||||
import {MatButton} from "@angular/material/button";
|
||||
import {DataSpinnerComponent} from "@component/common/data-spinner/data-spinner.component";
|
||||
import {ProfessorResponse} from "@api/v1/professorResponse";
|
||||
import {FacultyResponse} from "@api/v1/facultyResponse";
|
||||
import {GroupResponse} from "@api/v1/groupResponse";
|
||||
import {CampusBasicInfoResponse} from "@api/v1/campusBasicInfoResponse";
|
||||
import {LectureHallResponse} from "@api/v1/lectureHallResponse";
|
||||
import {DisciplineService} from "@api/v1/discipline.service";
|
||||
import {GroupComponent} from "@component/schedule/tabs/group/group.component";
|
||||
import {ProfessorComponent} from "@component/schedule/tabs/professor/professor.component";
|
||||
import {LectureHallComponent} from "@component/schedule/tabs/lecture-hall/lecture-hall.component";
|
||||
import {FacultyService} from "@api/v1/faculty.service";
|
||||
import {GroupService} from "@api/v1/group.service";
|
||||
import {ProfessorService} from "@api/v1/professor.service";
|
||||
import {CampusService} from "@api/v1/campus.service";
|
||||
import {LectureHallService} from "@api/v1/lectureHall.service";
|
||||
import {ScheduleService} from "@api/v1/schedule.service";
|
||||
import {ScheduleResponse} from "@api/v1/scheduleResponse";
|
||||
|
||||
export enum TabsSelect {
|
||||
Group,
|
||||
Professor,
|
||||
LectureHall,
|
||||
Other
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'app-tabs',
|
||||
standalone: true,
|
||||
imports: [
|
||||
HttpClientModule,
|
||||
OtherComponent,
|
||||
MatTabGroup,
|
||||
MatTab,
|
||||
ReactiveFormsModule,
|
||||
AsyncPipe,
|
||||
NgIf,
|
||||
MatButton,
|
||||
DataSpinnerComponent,
|
||||
GroupComponent,
|
||||
@ -41,171 +34,122 @@ import {LectureHallService} from "@api/v1/lectureHall.service";
|
||||
],
|
||||
templateUrl: './tabs.component.html',
|
||||
styleUrl: './tabs.component.css',
|
||||
providers: [FacultyService, GroupService, ProfessorService, CampusService, LectureHallService, DisciplineService ]
|
||||
providers: [ScheduleService]
|
||||
})
|
||||
|
||||
export class TabsComponent {
|
||||
protected professorsData: ProfessorResponse[] = [];
|
||||
@Output() eventResult = new EventEmitter<[TabsSelect, number, Observable<ScheduleResponse[]>]>();
|
||||
|
||||
protected faculties: Observable<FacultyResponse[]> = of([]);
|
||||
protected groups: Observable<GroupResponse[]> = of([]);
|
||||
private groupsData: Observable<GroupResponse[]> = of([]);
|
||||
constructor(private scheduleApi: ScheduleService) {
|
||||
}
|
||||
|
||||
protected campuses: Observable<CampusBasicInfoResponse[]> = of([]);
|
||||
protected lectureHalls: Observable<LectureHallResponse[]> = of([]);
|
||||
private lectureHallsData: Observable<LectureHallResponse[]> = of([]);
|
||||
protected groupSelected(id: number) {
|
||||
this.eventResult.emit(
|
||||
[
|
||||
TabsSelect.Group,
|
||||
id,
|
||||
this.scheduleApi.getByGroup(id)
|
||||
.pipe(
|
||||
map(g =>
|
||||
g.map(data =>
|
||||
({
|
||||
dayOfWeek: data.dayOfWeek,
|
||||
pairNumber: data.pairNumber,
|
||||
isEven: data.isEven,
|
||||
discipline: data.discipline,
|
||||
disciplineId: data.disciplineId,
|
||||
isExcludedWeeks: data.isExcludedWeeks,
|
||||
weeks: data.weeks,
|
||||
typeOfOccupations: data.typeOfOccupations,
|
||||
group: data.group,
|
||||
groupId: data.groupId,
|
||||
lectureHalls: data.lectureHalls,
|
||||
lectureHallsId: data.lectureHallsId,
|
||||
professors: data.professors,
|
||||
professorsId: data.professorsId,
|
||||
campus: data.campus,
|
||||
campusId: data.campusId,
|
||||
linkToMeet: data.linkToMeet
|
||||
}))
|
||||
)
|
||||
)
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
// States
|
||||
protected facultiesLoaded: boolean | null = false;
|
||||
protected groupLoaded: boolean | null = false;
|
||||
protected campusesLoaded: boolean | null = false;
|
||||
protected lectureHallsLoaded: boolean | null = false;
|
||||
protected disciplinesLoaded: boolean | null = false;
|
||||
protected professorsLoaded: boolean | null = false;
|
||||
protected professorSelected(id: number) {
|
||||
this.eventResult.emit(
|
||||
[
|
||||
TabsSelect.Professor,
|
||||
id,
|
||||
this.scheduleApi.getByProfessor(id)
|
||||
.pipe(
|
||||
map(p =>
|
||||
p.map(data =>
|
||||
({
|
||||
dayOfWeek: data.dayOfWeek,
|
||||
pairNumber: data.pairNumber,
|
||||
isEven: data.isEven,
|
||||
discipline: data.discipline,
|
||||
disciplineId: data.disciplineId,
|
||||
isExcludedWeeks: data.isExcludedWeeks,
|
||||
weeks: data.weeks,
|
||||
typeOfOccupations: data.typeOfOccupations,
|
||||
group: data.group,
|
||||
groupId: data.groupId,
|
||||
lectureHalls: data.lectureHalls,
|
||||
lectureHallsId: data.lectureHallsId,
|
||||
professors: data.professors,
|
||||
professorsId: data.professorsId,
|
||||
campus: data.campus,
|
||||
campusId: data.campusId,
|
||||
linkToMeet: data.linkToMeet
|
||||
}))
|
||||
)
|
||||
)
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
@Output() groupSelected: EventEmitter<number> = new EventEmitter<number>();
|
||||
@Output() lectureHallSelected: EventEmitter<number> = new EventEmitter<number>();
|
||||
@Output() professorSelected: EventEmitter<number> = new EventEmitter<number>();
|
||||
|
||||
constructor(
|
||||
private facultyApi: FacultyService,
|
||||
private groupApi: GroupService,
|
||||
private professorApi: ProfessorService,
|
||||
private campusApi: CampusService,
|
||||
private lectureHallApi: LectureHallService,
|
||||
private disciplineApi: DisciplineService) {
|
||||
this.facultyLoad().then();
|
||||
protected lectureHallSelected(id: number) {
|
||||
this.eventResult.emit(
|
||||
[
|
||||
TabsSelect.LectureHall,
|
||||
id,
|
||||
this.scheduleApi.getByLectureHall(id)
|
||||
.pipe(
|
||||
map(lh =>
|
||||
lh.map(data =>
|
||||
({
|
||||
dayOfWeek: data.dayOfWeek,
|
||||
pairNumber: data.pairNumber,
|
||||
isEven: data.isEven,
|
||||
discipline: data.discipline,
|
||||
disciplineId: data.disciplineId,
|
||||
isExcludedWeeks: data.isExcludedWeeks,
|
||||
weeks: data.weeks,
|
||||
typeOfOccupations: data.typeOfOccupations,
|
||||
group: data.group,
|
||||
groupId: data.groupId,
|
||||
lectureHalls: data.lectureHalls,
|
||||
lectureHallsId: data.lectureHallsId,
|
||||
professors: data.professors,
|
||||
professorsId: data.professorsId,
|
||||
campus: data.campus,
|
||||
campusId: data.campusId,
|
||||
linkToMeet: data.linkToMeet
|
||||
}))
|
||||
)
|
||||
)
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
protected async chooseTabs(event: MatTabChangeEvent) {
|
||||
switch (event.index) {
|
||||
case 0:
|
||||
await this.facultyLoad();
|
||||
break;
|
||||
case 1:
|
||||
this.professorsLoad();
|
||||
break;
|
||||
case 2:
|
||||
await this.campusLoad();
|
||||
break;
|
||||
case 3:
|
||||
await this.extensionLoad();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
protected async facultyLoad() {
|
||||
if (this.facultiesLoaded === null) this.facultiesLoaded = false;
|
||||
if (this.facultiesLoaded) return;
|
||||
|
||||
this.faculties = this.facultyApi.getFaculties().pipe(
|
||||
tap(() => {
|
||||
this.facultiesLoaded = true;
|
||||
}),
|
||||
catchError((error) => {
|
||||
this.facultiesLoaded = null;
|
||||
throw error;
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
protected groupLoad(id: number) {
|
||||
if (this.groupLoaded === null) this.groupLoaded = false;
|
||||
|
||||
if (this.groupLoaded)
|
||||
this.groups = this.groupsData.pipe(map(data => data.filter(x => x.facultyId === id)));
|
||||
else
|
||||
this.groups = this.groupApi.getByFaculty(id).pipe(
|
||||
tap(() => {
|
||||
this.groupLoaded = false;
|
||||
}),
|
||||
catchError((error) => {
|
||||
this.groupLoaded = null;
|
||||
throw error;
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
protected professorsLoad() {
|
||||
if (this.professorsLoaded === null) this.professorsLoaded = false;
|
||||
|
||||
if (this.professorsLoaded) return;
|
||||
|
||||
this.professorApi.getProfessors().pipe(
|
||||
catchError((error) => {
|
||||
this.professorsLoaded = null;
|
||||
throw error;
|
||||
})
|
||||
).subscribe(data => {
|
||||
this.professorsData = data;
|
||||
this.professorEx.Data = data.map(x =>
|
||||
({
|
||||
id: x.id,
|
||||
name: x.altName ? x.altName : x.name,
|
||||
selected: false
|
||||
}));
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
protected async campusLoad() {
|
||||
if (this.campusesLoaded === null) this.campusesLoaded = false;
|
||||
|
||||
if (this.campusesLoaded) return;
|
||||
|
||||
this.campuses = this.campusApi.getCampus().pipe(
|
||||
tap(() => {
|
||||
this.campusesLoaded = true;
|
||||
}),
|
||||
catchError((error) => {
|
||||
this.campusesLoaded = null;
|
||||
throw error;
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
protected lectureHallLoad(id: number) {
|
||||
if (this.lectureHallsLoaded === null) this.lectureHallsLoaded = false;
|
||||
|
||||
if (this.lectureHallsLoaded)
|
||||
this.lectureHalls = this.lectureHallsData.pipe(map(data => data.filter(x => x.campusId === id)));
|
||||
else
|
||||
this.lectureHalls = this.lectureHallApi.getByCampus(id).pipe(
|
||||
tap(() => {
|
||||
this.lectureHallsLoaded = false;
|
||||
}),
|
||||
catchError((error) => {
|
||||
this.lectureHallsLoaded = null;
|
||||
throw error;
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
protected async loadLectureHalls() {
|
||||
if (!this.campusesLoaded)
|
||||
await this.campusLoad();
|
||||
|
||||
if (!this.lectureHallsLoaded) {
|
||||
this.lectureHallsData = this.lectureHallApi.getLectureHalls();
|
||||
this.lectureHallsData.pipe(
|
||||
switchMap(lectureHalls => this.campuses.pipe(
|
||||
map(campuses => {
|
||||
return lectureHalls.map(x => {
|
||||
const campus = campuses.find(c => c.id === x.campusId);
|
||||
const codeName = campus ? campus.codeName : '';
|
||||
return {
|
||||
id: x.id,
|
||||
name: `${x.name} (${codeName})`,
|
||||
selected: false
|
||||
};
|
||||
});
|
||||
})
|
||||
)),
|
||||
).subscribe(data => {
|
||||
this.lectureHallEx.Data = data;
|
||||
this.lectureHallsLoaded = true;
|
||||
this.campusesLoaded = true;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user