From b86cf8d93c5a246446fafaec6787e10749ab50f1 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Thu, 24 Mar 2022 11:12:21 +0100 Subject: [PATCH] MOBILE-3833 course: Decrease WS calls to get_courses_by_fields --- .../components/myoverview/myoverview.ts | 15 ++++++++++--- .../recentlyaccessedcourses.ts | 21 ++++++++++++------- .../starredcourses/starredcourses.ts | 13 ++++++++++-- 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/src/addons/block/myoverview/components/myoverview/myoverview.ts b/src/addons/block/myoverview/components/myoverview/myoverview.ts index c1f84f909..8c97fa6c6 100644 --- a/src/addons/block/myoverview/components/myoverview/myoverview.ts +++ b/src/addons/block/myoverview/components/myoverview/myoverview.ts @@ -203,6 +203,15 @@ export class AddonBlockMyOverviewComponent extends CoreBlockBaseComponent implem await this.invalidateCourses(courseIds); } + /** + * Invalidate list of courses. + * + * @return Promise resolved when done. + */ + protected invalidateCourseList(): Promise { + return CoreCourses.invalidateUserCourses(); + } + /** * Helper function to invalidate only selected courses. * @@ -213,7 +222,7 @@ export class AddonBlockMyOverviewComponent extends CoreBlockBaseComponent implem const promises: Promise[] = []; // Invalidate course completion data. - promises.push(CoreCourses.invalidateUserCourses().finally(() => + promises.push(this.invalidateCourseList().finally(() => CoreUtils.allPromises(courseIds.map((courseId) => AddonCourseCompletion.invalidateCourseCompletion(courseId))))); @@ -397,7 +406,7 @@ export class AddonBlockMyOverviewComponent extends CoreBlockBaseComponent implem course.hidden = !!data.value; } - await this.invalidateCourses([course.id]); + await this.invalidateCourseList(); await this.filterCourses(); } @@ -409,7 +418,7 @@ export class AddonBlockMyOverviewComponent extends CoreBlockBaseComponent implem course.lastaccess = CoreTimeUtils.timestamp(); - await this.invalidateCourses([course.id]); + await this.invalidateCourseList(); await this.filterCourses(); } } diff --git a/src/addons/block/recentlyaccessedcourses/components/recentlyaccessedcourses/recentlyaccessedcourses.ts b/src/addons/block/recentlyaccessedcourses/components/recentlyaccessedcourses/recentlyaccessedcourses.ts index eefd70ac4..ef7d67f3b 100644 --- a/src/addons/block/recentlyaccessedcourses/components/recentlyaccessedcourses/recentlyaccessedcourses.ts +++ b/src/addons/block/recentlyaccessedcourses/components/recentlyaccessedcourses/recentlyaccessedcourses.ts @@ -85,6 +85,17 @@ export class AddonBlockRecentlyAccessedCoursesComponent extends CoreBlockBaseCom await this.invalidateCourses(courseIds); } + /** + * Invalidate list of courses. + * + * @return Promise resolved when done. + */ + protected async invalidateCourseList(): Promise { + return this.site.isVersionGreaterEqualThan('3.8') + ? CoreCourses.invalidateRecentCourses() + : CoreCourses.invalidateUserCourses(); + } + /** * Helper function to invalidate only selected courses. * @@ -94,12 +105,8 @@ export class AddonBlockRecentlyAccessedCoursesComponent extends CoreBlockBaseCom protected async invalidateCourses(courseIds: number[]): Promise { const promises: Promise[] = []; - const invalidateCoursePromise = this.site.isVersionGreaterEqualThan('3.8') - ? CoreCourses.invalidateRecentCourses() - : CoreCourses.invalidateUserCourses(); - // Invalidate course completion data. - promises.push(invalidateCoursePromise.finally(() => + promises.push(this.invalidateCourseList().finally(() => CoreUtils.allPromises(courseIds.map((courseId) => AddonCourseCompletion.invalidateCourseCompletion(courseId))))); @@ -179,13 +186,13 @@ export class AddonBlockRecentlyAccessedCoursesComponent extends CoreBlockBaseCom this.courses.splice(courseIndex, 1); this.courses.unshift(course); - await this.invalidateCourses([course.id]); + await this.invalidateCourseList(); } if (data.action == CoreCoursesProvider.ACTION_STATE_CHANGED && data.state == CoreCoursesProvider.STATE_FAVOURITE && course) { course.isfavourite = !!data.value; - await this.invalidateCourses([course.id]); + await this.invalidateCourseList(); } } diff --git a/src/addons/block/starredcourses/components/starredcourses/starredcourses.ts b/src/addons/block/starredcourses/components/starredcourses/starredcourses.ts index c6e6fc447..c82a896dc 100644 --- a/src/addons/block/starredcourses/components/starredcourses/starredcourses.ts +++ b/src/addons/block/starredcourses/components/starredcourses/starredcourses.ts @@ -81,6 +81,15 @@ export class AddonBlockStarredCoursesComponent extends CoreBlockBaseComponent im await this.invalidateCourses(courseIds); } + /** + * Invalidate list of courses. + * + * @return Promise resolved when done. + */ + protected async invalidateCourseList(): Promise { + return AddonBlockStarredCourses.invalidateStarredCourses(); + } + /** * Helper function to invalidate only selected courses. * @@ -91,7 +100,7 @@ export class AddonBlockStarredCoursesComponent extends CoreBlockBaseComponent im const promises: Promise[] = []; // Invalidate course completion data. - promises.push(AddonBlockStarredCourses.invalidateStarredCourses().finally(() => + promises.push(this.invalidateCourseList().finally(() => CoreUtils.allPromises(courseIds.map((courseId) => AddonCourseCompletion.invalidateCourseCompletion(courseId))))); @@ -172,7 +181,7 @@ export class AddonBlockStarredCoursesComponent extends CoreBlockBaseComponent im this.courses.unshift(course); } - await this.invalidateCourses([course.id]); + await this.invalidateCourseList(); } }