|
1 | | -/** |
2 | | - * LanguageManager.js |
3 | | - * |
4 | | - * Provides utility functions for updating the session language preferences for users. |
5 | | - */ |
6 | | - |
7 | 1 | import { getConfig } from '../config'; |
8 | | -import { getAuthenticatedHttpClient, getAuthenticatedUser } from '../auth'; |
9 | | -import { convertKeyNames, snakeCaseObject } from '../utils'; |
| 2 | +import { getAuthenticatedUser } from '../auth'; |
10 | 3 | import { getCookies, handleRtl, LOCALE_CHANGED } from './lib'; |
11 | | -import { logError } from '../logging'; |
12 | 4 | import { publish } from '../pubSub'; |
13 | | - |
14 | | -/** |
15 | | - * Updates user language preferences via the preferences API. |
16 | | - * |
17 | | - * This function converts preference data to snake_case and formats specific keys |
18 | | - * according to backend requirements before sending the PATCH request. |
19 | | - * |
20 | | - * @param {string} username - The username of the user whose preferences to update. |
21 | | - * @param {Object} preferenceData - The preference parameters to update (e.g., { prefLang: 'en' }). |
22 | | - * @returns {Promise} - A promise that resolves when the API call completes successfully, |
23 | | - * or rejects if there's an error with the request. |
24 | | - */ |
25 | | -export async function updateUserPreferences(username, preferenceData) { |
26 | | - const snakeCaseData = snakeCaseObject(preferenceData); |
27 | | - const formattedData = convertKeyNames(snakeCaseData, { |
28 | | - pref_lang: 'pref-lang', |
29 | | - }); |
30 | | - |
31 | | - return getAuthenticatedHttpClient().patch( |
32 | | - `${getConfig().LMS_BASE_URL}/api/user/v1/preferences/${username}`, |
33 | | - formattedData, |
34 | | - { headers: { 'Content-Type': 'application/merge-patch+json' } }, |
35 | | - ); |
36 | | -} |
37 | | - |
38 | | -/** |
39 | | - * Sets the language for the current session using the setlang endpoint. |
40 | | - * |
41 | | - * This function sends a POST request to the LMS setlang endpoint to change |
42 | | - * the language for the current user session. |
43 | | - * |
44 | | - * @param {string} languageCode - The language code to set (e.g., 'en', 'es', 'ar'). |
45 | | - * Should be a valid ISO language code supported by the platform. |
46 | | - * @returns {Promise} - A promise that resolves when the API call completes successfully, |
47 | | - * or rejects if there's an error with the request. |
48 | | - */ |
49 | | -export async function setSessionLanguage(languageCode) { |
50 | | - const formData = new FormData(); |
51 | | - formData.append('language', languageCode); |
52 | | - |
53 | | - const url = `${getConfig().LMS_BASE_URL}/i18n/setlang/`; |
54 | | - return getAuthenticatedHttpClient().post(url, formData, { |
55 | | - headers: { |
56 | | - Accept: 'application/json', |
57 | | - 'X-Requested-With': 'XMLHttpRequest', |
58 | | - }, |
59 | | - }); |
60 | | -} |
| 5 | +import { logError } from '../logging'; |
| 6 | +import { updateUserPreferences, setSessionLanguage } from './languageApi'; |
61 | 7 |
|
62 | 8 | /** |
63 | 9 | * Changes the user's language preference and applies it to the current session. |
|
0 commit comments