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

main
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 { 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<CorePasswordModalResponse> => {
const response = await CoreCourses.validateGuestAccessPassword(guestInstanceId, password);
type ValidatorResponse = CorePasswordModalResponse & { cancel?: boolean };
const validatePassword = async (password: string): Promise<ValidatorResponse> => {
try {
const response = await CoreCourses.validateGuestAccessPassword(guestInstanceId, password);
validated = response.validated;
let error = response.hint;
if (!validated && !error) {
error = 'core.course.guestaccess_passwordinvalid';
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<ValidatorResponse>({
title: 'core.course.guestaccess',
validator: validatePassword,
});
if (!response.validated) {
if (!response.validated || response.cancel) {
return;
}
} 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.
*

View File

@ -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<CorePasswordModalResponse> {
async promptPassword<T extends CorePasswordModalResponse>(passwordParams?: CorePasswordModalParams): Promise<T> {
const { CorePasswordModalComponent } =
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',
showBackdrop: true,