forked from openedx/frontend-component-header
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
138 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { getConfig } from '@edx/frontend-platform'; | ||
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth'; | ||
import { convertKeyNames, snakeCaseObject } from '@edx/frontend-platform/utils'; | ||
|
||
export async function patchPreferences(username, params) { | ||
let processedParams = snakeCaseObject(params); | ||
processedParams = convertKeyNames(processedParams, { | ||
pref_lang: 'pref-lang', | ||
}); | ||
|
||
await getAuthenticatedHttpClient() | ||
.patch(`${getConfig().LMS_BASE_URL}/api/user/v1/preferences/${username}`, processedParams, { | ||
headers: { 'Content-Type': 'application/merge-patch+json' }, | ||
}); | ||
|
||
return params; | ||
} | ||
|
||
export async function postSetLang(code) { | ||
const formData = new FormData(); | ||
const requestConfig = { | ||
headers: { | ||
Accept: 'application/json', | ||
'X-Requested-With': 'XMLHttpRequest', | ||
}, | ||
}; | ||
const url = `${getConfig().LMS_BASE_URL}/i18n/setlang/`; | ||
formData.append('language', code); | ||
|
||
await getAuthenticatedHttpClient() | ||
.post(url, formData, requestConfig); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { publish } from '@edx/frontend-platform'; | ||
import { | ||
getLocale, injectIntl, intlShape, FormattedMessage, LOCALE_CHANGED, handleRtl, | ||
} from '@edx/frontend-platform/i18n'; | ||
import { logError } from '@edx/frontend-platform/logging'; | ||
|
||
import { patchPreferences, postSetLang } from './data/api'; | ||
|
||
const onLanguageSelected = async (username, selectedLanguageCode) => { | ||
try { | ||
if (username) { | ||
await patchPreferences(username, { prefLang: selectedLanguageCode }); | ||
await postSetLang(selectedLanguageCode); | ||
} | ||
publish(LOCALE_CHANGED, getLocale()); | ||
handleRtl(); | ||
} catch (error) { | ||
logError(error); | ||
} | ||
}; | ||
|
||
const LanguageSelector = ({ | ||
intl, options, authenticatedUser, ...props | ||
}) => { | ||
const handleSubmit = (e) => { | ||
e.preventDefault(); | ||
const previousSiteLanguage = getLocale(); | ||
const languageCode = e.target.elements['site-header-language-select'].value; | ||
console.debug(previousSiteLanguage, languageCode, authenticatedUser); | ||
|
||
if (previousSiteLanguage !== languageCode) { | ||
onLanguageSelected(authenticatedUser?.username, languageCode); | ||
} | ||
}; | ||
|
||
return ( | ||
<form | ||
className="form-inline" | ||
onSubmit={handleSubmit} | ||
{...props} | ||
> | ||
<div className="form-group d-wrap"> | ||
{/* eslint-disable-next-line jsx-a11y/label-has-associated-control */} | ||
<label htmlFor="site-header-language-select" className="d-inline-block m-0 small"> | ||
<FormattedMessage | ||
id="footer.languageForm.select.label" | ||
defaultMessage="Choose Language" | ||
description="The label for the laguage select part of the language selection form." | ||
/> | ||
</label> | ||
<select | ||
id="site-header-language-select" | ||
className="form-control-sm mx-2" | ||
name="site-header-language-select" | ||
defaultValue={intl.locale} | ||
> | ||
{options.map(({ value, label }) => <option key={value} value={value}>{label}</option>)} | ||
</select> | ||
<button data-testid="site-header-submit-btn" className="btn btn-outline-primary btn-sm" type="submit"> | ||
<FormattedMessage | ||
id="footer.languageForm.submit.label" | ||
defaultMessage="Apply" | ||
description="The label for button to submit the language selection form." | ||
/> | ||
</button> | ||
</div> | ||
</form> | ||
); | ||
}; | ||
|
||
LanguageSelector.propTypes = { | ||
authenticatedUser: PropTypes.shape({ | ||
username: PropTypes.string, | ||
}).isRequired, | ||
intl: intlShape.isRequired, | ||
options: PropTypes.arrayOf(PropTypes.shape({ | ||
value: PropTypes.string, | ||
label: PropTypes.string, | ||
})).isRequired, | ||
}; | ||
|
||
export default injectIntl(LanguageSelector); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters