MOBILE-4059 user: Close inapp after submission

main
Noel De Martin 2022-11-22 17:54:01 +01:00
parent d78a7dd78b
commit 2143f49d0d
8 changed files with 90 additions and 0 deletions

View File

@ -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",

View File

@ -66,6 +66,13 @@ export class CoreUserAuthenticatedSupportConfig extends CoreUserSupportConfig {
return false;
}
/**
* @inheritdoc
*/
getSupportPageLang(): string | null {
return this.site.infos?.lang ?? null;
}
/**
* @inheritdoc
*/

View File

@ -65,6 +65,13 @@ export class CoreUserGuestSupportConfig extends CoreUserSupportConfig {
return 'supportpage' in this.config;
}
/**
* @inheritdoc
*/
getSupportPageLang(): string | null {
return this.config.lang ?? null;
}
/**
* @inheritdoc
*/

View File

@ -29,6 +29,13 @@ export class CoreUserNullSupportConfig extends CoreUserSupportConfig {
return false;
}
/**
* @inheritdoc
*/
getSupportPageLang(): string | null {
return null;
}
/**
* @inheritdoc
*/

View File

@ -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.
*

View File

@ -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}}",

View File

@ -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<void> {
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);

View File

@ -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<string | null> {
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<Record<string, string>> {
return new Promise(resolve => CoreSubscriptions.once(
Translate.getTranslation(lang),
messages => resolve(messages),
() => resolve({}),
));
}
/**
* Get the parent language defined on the language strings.
*