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 79c86c9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -322,26 +322,36 @@ export class CoreCourseSummaryPage implements OnInit, OnDestroy {

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
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 79c86c9

Please sign in to comment.