From 9a0b86e3e32e99e0b2c57b2fb1c023569c7bab78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Fri, 14 Jul 2023 14:23:15 +0200 Subject: [PATCH] MOBILE-4009 enrol: Catch validation errors to cancel the prompt --- .../course-summary/course-summary.page.ts | 41 +++++++++++-------- .../features/course/services/course-helper.ts | 18 ++++++++ src/core/services/utils/dom.ts | 4 +- 3 files changed, 44 insertions(+), 19 deletions(-) diff --git a/src/core/features/course/pages/course-summary/course-summary.page.ts b/src/core/features/course/pages/course-summary/course-summary.page.ts index 8185b598982..878c0d2a0f6 100644 --- a/src/core/features/course/pages/course-summary/course-summary.page.ts +++ b/src/core/features/course/pages/course-summary/course-summary.page.ts @@ -39,7 +39,6 @@ import { CoreColors } from '@singletons/colors'; import { CorePath } from '@singletons/path'; import { CorePromisedValue } from '@classes/promised-value'; import { CorePlatform } from '@services/platform'; -import { CoreCourse } from '@features/course/services/course'; import { CorePasswordModalResponse } from '@components/password-modal/password-modal'; import { CoreTime } from '@singletons/time'; import { CoreAnalytics, CoreAnalyticsEventType } from '@services/analytics'; @@ -316,32 +315,40 @@ export class CoreCourseSummaryPage implements OnInit, OnDestroy { const guestInstanceId = await this.guestInstanceId; if (this.useGuestAccess && this.guestAccessPasswordRequired && guestInstanceId) { // Check if the user has access to the course as guest with a previous sent password. - let validated = await CoreUtils.promiseWorks( - CoreCourse.getSections(this.courseId, true, true, { getFromCache: false, emergencyCache: false }, undefined, false), - ); + let validated = await CoreCourseHelper.userHasAccessToCourse(this.courseId); if (!validated) { try { - const validatePassword = async (password: string): Promise => { - const response = await CoreCourses.validateGuestAccessPassword(guestInstanceId, password); - - validated = response.validated; - let error = response.hint; - if (!validated && !error) { - error = 'core.course.guestaccess_passwordinvalid'; + type ValidatorResponse = CorePasswordModalResponse & { cancel?: boolean }; + const validatePassword = async (password: string): Promise => { + try { + const response = await CoreCourses.validateGuestAccessPassword(guestInstanceId, password); + + validated = response.validated; + let error = response.hint; + if (!validated && !error) { + error = 'core.course.guestaccess_passwordinvalid'; + } + + return { + password, validated, error, + }; + } catch { + this.refreshData(); + + return { + password, + cancel: true, + }; } - - return { - password, validated, error, - }; }; - const response = await CoreDomUtils.promptPassword({ + const response = await CoreDomUtils.promptPassword({ title: 'core.course.guestaccess', validator: validatePassword, }); - if (!response.validated) { + if (!response.validated || response.cancel) { return; } } catch { diff --git a/src/core/features/course/services/course-helper.ts b/src/core/features/course/services/course-helper.ts index 7c02eb06f34..40a5c87d9cf 100644 --- a/src/core/features/course/services/course-helper.ts +++ b/src/core/features/course/services/course-helper.ts @@ -1940,6 +1940,24 @@ export class CoreCourseHelperProvider { } } + /** + * Check if user can access the course. + * + * @param courseId Course ID. + * @returns Promise resolved with boolean: whether user can access the course. + */ + async userHasAccessToCourse(courseId: number): Promise { + if (CoreNetwork.isOnline()) { + return CoreUtils.promiseWorks( + CoreCourse.getSections(courseId, true, true, { getFromCache: false, emergencyCache: false }, undefined, false), + ); + } else { + return CoreUtils.promiseWorks( + CoreCourse.getSections(courseId, true, true, { getCacheUsingCacheKey: true }, undefined, false), + ); + } + } + /** * Delete course files. * diff --git a/src/core/services/utils/dom.ts b/src/core/services/utils/dom.ts index caa533f8910..d17696b342f 100644 --- a/src/core/services/utils/dom.ts +++ b/src/core/services/utils/dom.ts @@ -1887,11 +1887,11 @@ export class CoreDomUtilsProvider { * @param passwordParams Params to show the modal. * @returns Entered password, error and validation. */ - async promptPassword(passwordParams?: CorePasswordModalParams): Promise { + async promptPassword(passwordParams?: CorePasswordModalParams): Promise { const { CorePasswordModalComponent } = await import('@/core/components/password-modal/password-modal.module'); - const modalData = await CoreDomUtils.openModal( + const modalData = await CoreDomUtils.openModal( { cssClass: 'core-password-modal', showBackdrop: true,