From 2143f49d0d2df3c858eb4f73ceda37c83bee169c Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Tue, 22 Nov 2022 17:54:01 +0100 Subject: [PATCH] MOBILE-4059 user: Close inapp after submission --- scripts/langindex.json | 1 + .../support/authenticated-support-config.ts | 7 ++++ .../classes/support/guest-support-config.ts | 7 ++++ .../classes/support/null-support-config.ts | 7 ++++ .../user/classes/support/support-config.ts | 7 ++++ src/core/features/user/lang.json | 1 + src/core/features/user/services/support.ts | 33 +++++++++++++++++++ src/core/services/lang.ts | 27 +++++++++++++++ 8 files changed, 90 insertions(+) diff --git a/scripts/langindex.json b/scripts/langindex.json index a36006465..65bdc325e 100644 --- a/scripts/langindex.json +++ b/scripts/langindex.json @@ -2396,6 +2396,7 @@ "core.user.sendemail": "local_moodlemobileapp", "core.user.student": "moodle/defaultcoursestudent", "core.user.support": "local_moodlemobileapp", + "core.user.supportmessagesent": "user", "core.user.supportsubject": "local_moodlemobileapp", "core.user.teacher": "moodle/noneditingteacher", "core.user.useraccount": "moodle", diff --git a/src/core/features/user/classes/support/authenticated-support-config.ts b/src/core/features/user/classes/support/authenticated-support-config.ts index 36b87a446..361b82b54 100644 --- a/src/core/features/user/classes/support/authenticated-support-config.ts +++ b/src/core/features/user/classes/support/authenticated-support-config.ts @@ -66,6 +66,13 @@ export class CoreUserAuthenticatedSupportConfig extends CoreUserSupportConfig { return false; } + /** + * @inheritdoc + */ + getSupportPageLang(): string | null { + return this.site.infos?.lang ?? null; + } + /** * @inheritdoc */ diff --git a/src/core/features/user/classes/support/guest-support-config.ts b/src/core/features/user/classes/support/guest-support-config.ts index bfc921e12..4861bf4ca 100644 --- a/src/core/features/user/classes/support/guest-support-config.ts +++ b/src/core/features/user/classes/support/guest-support-config.ts @@ -65,6 +65,13 @@ export class CoreUserGuestSupportConfig extends CoreUserSupportConfig { return 'supportpage' in this.config; } + /** + * @inheritdoc + */ + getSupportPageLang(): string | null { + return this.config.lang ?? null; + } + /** * @inheritdoc */ diff --git a/src/core/features/user/classes/support/null-support-config.ts b/src/core/features/user/classes/support/null-support-config.ts index cfdc6d5c4..c152aa301 100644 --- a/src/core/features/user/classes/support/null-support-config.ts +++ b/src/core/features/user/classes/support/null-support-config.ts @@ -29,6 +29,13 @@ export class CoreUserNullSupportConfig extends CoreUserSupportConfig { return false; } + /** + * @inheritdoc + */ + getSupportPageLang(): string | null { + return null; + } + /** * @inheritdoc */ diff --git a/src/core/features/user/classes/support/support-config.ts b/src/core/features/user/classes/support/support-config.ts index 093d6069e..09df13244 100644 --- a/src/core/features/user/classes/support/support-config.ts +++ b/src/core/features/user/classes/support/support-config.ts @@ -24,6 +24,13 @@ export abstract class CoreUserSupportConfig { */ public abstract canContactSupport(): boolean; + /** + * Get language used in the support page, if any. + * + * @return Support page language. + */ + public abstract getSupportPageLang(): string | null; + /** * Get url to use for contacting support. * diff --git a/src/core/features/user/lang.json b/src/core/features/user/lang.json index 8a71aee4c..5be59a0aa 100644 --- a/src/core/features/user/lang.json +++ b/src/core/features/user/lang.json @@ -31,6 +31,7 @@ "sendemail": "Email", "student": "Student", "support": "Contact site support", + "supportmessagesent": "Your message has been sent.", "supportsubject": "[App] {{subject}}", "teacher": "Non-editing teacher", "userwithid": "User with ID {{id}}", diff --git a/src/core/features/user/services/support.ts b/src/core/features/user/services/support.ts index dffbfc7be..72c6cb84e 100644 --- a/src/core/features/user/services/support.ts +++ b/src/core/features/user/services/support.ts @@ -24,6 +24,7 @@ import { CoreEvents } from '@singletons/events'; import { CoreSubscriptions } from '@singletons/subscriptions'; import { AlertButton } from '@ionic/angular'; import { CoreDomUtils } from '@services/utils/dom'; +import { CoreLang } from '@services/lang'; /** * Handle site support. @@ -44,6 +45,7 @@ export class CoreUserSupportService { if (supportPageUrl.endsWith('/user/contactsitesupport.php')) { this.populateSupportForm(browser, options.subject, options.message); + this.listenSupportFormSubmission(browser, supportConfig.getSupportPageLang()); } await CoreEvents.waitUntil(CoreEvents.IAB_EXIT); @@ -106,6 +108,37 @@ export class CoreUserSupportService { CoreEvents.once(CoreEvents.IAB_EXIT, () => unsubscribe()); } + /** + * Set up listeners to close the browser when the contact form has been submitted. + * + * @param browser In App browser. + * @param lang Language used in the support page. + */ + protected async listenSupportFormSubmission(browser: InAppBrowserObject, lang: string | null): Promise { + const appSuccessMessage = Translate.instant('core.user.supportmessagesent'); + const lmsSuccessMessage = lang && await CoreLang.getMessage('core.user.supportmessagesent', lang); + const subscription = browser.on('loadstop').subscribe(async () => { + const result = await browser.executeScript({ + code: ` + [...document.querySelectorAll('.alert-success')].some( + div => + div.textContent?.includes(${JSON.stringify(lmsSuccessMessage)}) || + div.textContent?.includes(${JSON.stringify(appSuccessMessage)}) + ) + `, + }); + + if (!Array.isArray(result) || result[0] !== true) { + return; + } + + browser.close(); + CoreDomUtils.showAlert(undefined, appSuccessMessage); + }); + + CoreEvents.once(CoreEvents.IAB_EXIT, () => subscription.unsubscribe()); + } + } export const CoreUserSupport = makeSingleton(CoreUserSupportService); diff --git a/src/core/services/lang.ts b/src/core/services/lang.ts index 2773d8d48..15c2730b3 100644 --- a/src/core/services/lang.ts +++ b/src/core/services/lang.ts @@ -119,6 +119,33 @@ export class CoreLangProvider { return value.charAt(0).toUpperCase() + value.slice(1); } + /** + * Get message for the given language. + * + * @param key Message key. + * @param lang Language. + * @returns Message if found, null otherwise. + */ + async getMessage(key: string, lang: string): Promise { + const messages = await this.getMessages(lang); + + return messages[key] ?? null; + } + + /** + * Get messages for the given language. + * + * @param lang Language. + * @returns Messages. + */ + getMessages(lang: string): Promise> { + return new Promise(resolve => CoreSubscriptions.once( + Translate.getTranslation(lang), + messages => resolve(messages), + () => resolve({}), + )); + } + /** * Get the parent language defined on the language strings. *