Skip to content

Commit

Permalink
MOBILE-4009 enrol: Catch validation errors to cancel the prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
crazyserver committed Jul 18, 2023
1 parent c671428 commit 9a0b86e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);

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<ValidatorResponse> => {
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<ValidatorResponse>({
title: 'core.course.guestaccess',
validator: validatePassword,
});

if (!response.validated) {
if (!response.validated || response.cancel) {
return;
}
} catch {
Expand Down
18 changes: 18 additions & 0 deletions src/core/features/course/services/course-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
4 changes: 2 additions & 2 deletions src/core/services/utils/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 9a0b86e

Please sign in to comment.