Skip to content

Commit a3f20aa

Browse files
committed
Fix dashboard timetable loading behavior #715
1 parent 44b0594 commit a3f20aa

File tree

1 file changed

+37
-32
lines changed

1 file changed

+37
-32
lines changed

src/app/dashboard/components/dashboard-timetable/dashboard-timetable.component.ts

+37-32
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import {
1111
of,
1212
shareReplay,
1313
switchMap,
14-
tap,
1514
} from "rxjs";
1615
import { LessonPresencesRestService } from "src/app/shared/services/lesson-presences-rest.service";
16+
import { LoadingService } from "src/app/shared/services/loading-service";
1717
import { StudentsRestService } from "src/app/shared/services/students-rest.service";
1818
import { TeachersRestService } from "src/app/shared/services/teachers-rest.service";
1919
import { UserSettingsService } from "src/app/shared/services/user-settings.service";
@@ -49,7 +49,7 @@ export class DashboardTimetableComponent {
4949
isTeacher$ = this.dashboardService.hasLessonTeacherRole$;
5050
isStudent$ = this.dashboardService.hasStudentRole$;
5151
date$ = new BehaviorSubject(startOfDay(new Date()));
52-
loading$ = new BehaviorSubject(true);
52+
loading$ = this.loadingService.loading$;
5353
timetableEntries$ = this.loadTimetableEntries();
5454

5555
constructor(
@@ -58,20 +58,18 @@ export class DashboardTimetableComponent {
5858
private lessonPresencesService: LessonPresencesRestService,
5959
private userSettings: UserSettingsService,
6060
private dashboardService: DashboardService,
61+
private loadingService: LoadingService,
6162
) {}
6263

6364
gotoToday(): void {
64-
this.loading$.next(true);
6565
this.date$.next(startOfDay(new Date()));
6666
}
6767

6868
gotoPreviousDay(): void {
69-
this.loading$.next(true);
7069
this.date$.next(subDays(this.date$.getValue(), 1));
7170
}
7271

7372
gotoNextDay(): void {
74-
this.loading$.next(true);
7573
this.date$.next(addDays(this.date$.getValue(), 1));
7674
}
7775

@@ -82,33 +80,41 @@ export class DashboardTimetableComponent {
8280
private loadTimetableEntries(): Observable<
8381
ReadonlyArray<DashboardTimetableEntry>
8482
> {
85-
return combineLatest([this.isTeacher$, this.isStudent$]).pipe(
86-
switchMap(([isTeacher, isStudent]) => {
87-
if (isTeacher) {
88-
return this.fetchTimetableEntries("teacher");
89-
} else if (isStudent) {
90-
return this.fetchTimetableEntries("student");
91-
}
92-
return of([]);
83+
return combineLatest([
84+
this.isTeacher$,
85+
this.isStudent$,
86+
this.userId$,
87+
this.date$,
88+
]).pipe(
89+
switchMap(([isTeacher, isStudent, userId, date]) => {
90+
const fetch = () => {
91+
if (isTeacher) {
92+
return this.fetchTimetableEntries("teacher", userId, date);
93+
} else if (isStudent) {
94+
return this.fetchTimetableEntries("student", userId, date);
95+
}
96+
return of([]);
97+
};
98+
return this.loadingService.load(fetch());
9399
}),
94-
tap(() => this.loading$.next(false)),
95100
shareReplay(1),
96101
);
97102
}
98103

99104
private fetchTimetableEntries(
100105
userType: "teacher" | "student",
106+
userId: number,
107+
date: Date,
101108
): Observable<ReadonlyArray<DashboardTimetableEntry>> {
102-
return combineLatest([this.userId$, this.date$]).pipe(
103-
switchMap(([userId, date]) => {
104-
const params: Dict<string> = {
105-
"filter.From": `=${format(date, "yyyy-MM-dd")}`,
106-
sort: "From,To",
107-
};
108-
return userType === "teacher"
109-
? this.teachersService.getTimetableEntries(userId, params)
110-
: this.studentsService.getTimetableEntries(userId, params);
111-
}),
109+
const params: Dict<string> = {
110+
"filter.From": `=${format(date, "yyyy-MM-dd")}`,
111+
sort: "From,To",
112+
};
113+
return (
114+
userType === "teacher"
115+
? this.teachersService.getTimetableEntries(userId, params)
116+
: this.studentsService.getTimetableEntries(userId, params)
117+
).pipe(
112118
map((entries) => entries.map(convertTimetableEntry)),
113119
map((entries) => uniqBy(entries, (entry) => entry.id)), // Due to a bug, the backend returns duplicate entries, so we filter them out here (this can be removed once fixed)
114120
switchMap((entries) => {
@@ -119,7 +125,7 @@ export class DashboardTimetableComponent {
119125
// comma-separated) we fetch the study classes separately from the
120126
// lesson presences for now. This can be removed, once the classes are
121127
// provided on the timetable entries in a clean way.
122-
return this.loadStudyClasses().pipe(
128+
return this.loadStudyClasses(date).pipe(
123129
map((studyClasses) => decorateStudyClasses(entries, studyClasses)),
124130
);
125131
} else {
@@ -130,12 +136,11 @@ export class DashboardTimetableComponent {
130136
);
131137
}
132138

133-
private loadStudyClasses(): Observable<Dict<ReadonlyArray<string>>> {
134-
return this.date$.pipe(
135-
switchMap((date) =>
136-
this.lessonPresencesService.getLessonStudyClassesByDate(date),
137-
),
138-
map(createStudyClassesMap),
139-
);
139+
private loadStudyClasses(
140+
date: Date,
141+
): Observable<Dict<ReadonlyArray<string>>> {
142+
return this.lessonPresencesService
143+
.getLessonStudyClassesByDate(date)
144+
.pipe(map(createStudyClassesMap));
140145
}
141146
}

0 commit comments

Comments
 (0)