MOBILE-4009 enrol: Catch validation errors to cancel the prompt

This commit is contained in:
Pau Ferrer Ocaña 2023-07-14 14:23:15 +02:00
parent c6714285b0
commit 9a0b86e3e3
3 changed files with 43 additions and 18 deletions

View File

@ -39,7 +39,6 @@ import { CoreColors } from '@singletons/colors';
import { CorePath } from '@singletons/path'; import { CorePath } from '@singletons/path';
import { CorePromisedValue } from '@classes/promised-value'; import { CorePromisedValue } from '@classes/promised-value';
import { CorePlatform } from '@services/platform'; import { CorePlatform } from '@services/platform';
import { CoreCourse } from '@features/course/services/course';
import { CorePasswordModalResponse } from '@components/password-modal/password-modal'; import { CorePasswordModalResponse } from '@components/password-modal/password-modal';
import { CoreTime } from '@singletons/time'; import { CoreTime } from '@singletons/time';
import { CoreAnalytics, CoreAnalyticsEventType } from '@services/analytics'; import { CoreAnalytics, CoreAnalyticsEventType } from '@services/analytics';
@ -316,13 +315,13 @@ export class CoreCourseSummaryPage implements OnInit, OnDestroy {
const guestInstanceId = await this.guestInstanceId; const guestInstanceId = await this.guestInstanceId;
if (this.useGuestAccess && this.guestAccessPasswordRequired && guestInstanceId) { if (this.useGuestAccess && this.guestAccessPasswordRequired && guestInstanceId) {
// Check if the user has access to the course as guest with a previous sent password. // Check if the user has access to the course as guest with a previous sent password.
let validated = await CoreUtils.promiseWorks( let validated = await CoreCourseHelper.userHasAccessToCourse(this.courseId);
CoreCourse.getSections(this.courseId, true, true, { getFromCache: false, emergencyCache: false }, undefined, false),
);
if (!validated) { if (!validated) {
try { try {
const validatePassword = async (password: string): Promise<CorePasswordModalResponse> => { type ValidatorResponse = CorePasswordModalResponse & { cancel?: boolean };
const validatePassword = async (password: string): Promise<ValidatorResponse> => {
try {
const response = await CoreCourses.validateGuestAccessPassword(guestInstanceId, password); const response = await CoreCourses.validateGuestAccessPassword(guestInstanceId, password);
validated = response.validated; validated = response.validated;
@ -334,14 +333,22 @@ export class CoreCourseSummaryPage implements OnInit, OnDestroy {
return { return {
password, validated, error, password, validated, error,
}; };
} catch {
this.refreshData();
return {
password,
cancel: true,
};
}
}; };
const response = await CoreDomUtils.promptPassword({ const response = await CoreDomUtils.promptPassword<ValidatorResponse>({
title: 'core.course.guestaccess', title: 'core.course.guestaccess',
validator: validatePassword, validator: validatePassword,
}); });
if (!response.validated) { if (!response.validated || response.cancel) {
return; return;
} }
} catch { } catch {

View File

@ -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<boolean> {
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. * Delete course files.
* *

View File

@ -1887,11 +1887,11 @@ export class CoreDomUtilsProvider {
* @param passwordParams Params to show the modal. * @param passwordParams Params to show the modal.
* @returns Entered password, error and validation. * @returns Entered password, error and validation.
*/ */
async promptPassword(passwordParams?: CorePasswordModalParams): Promise<CorePasswordModalResponse> { async promptPassword<T extends CorePasswordModalResponse>(passwordParams?: CorePasswordModalParams): Promise<T> {
const { CorePasswordModalComponent } = const { CorePasswordModalComponent } =
await import('@/core/components/password-modal/password-modal.module'); await import('@/core/components/password-modal/password-modal.module');
const modalData = await CoreDomUtils.openModal<CorePasswordModalResponse>( const modalData = await CoreDomUtils.openModal<T>(
{ {
cssClass: 'core-password-modal', cssClass: 'core-password-modal',
showBackdrop: true, showBackdrop: true,