-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: change lang selector logic
- Loading branch information
Showing
7 changed files
with
203 additions
and
132 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
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 was deleted.
Oops, something went wrong.
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,82 @@ | ||
import React, { useMemo, useState } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { publish } from '@edx/frontend-platform'; | ||
import { | ||
injectIntl, LOCALE_CHANGED, getLocale, handleRtl, getPrimaryLanguageSubtag, | ||
} from '@edx/frontend-platform/i18n'; | ||
import { logError } from '@edx/frontend-platform/logging'; | ||
import { Dropdown, useWindowSize } from '@openedx/paragon'; | ||
import { Language } from '@openedx/paragon/icons'; | ||
import { getCookies } from '@edx/frontend-platform/i18n/lib'; | ||
import { patchPreferences, postSetLang } from './data'; | ||
|
||
const onLanguageSelected = async ({ langCookieName, username, selectedLlocale }) => { | ||
try { | ||
if (username) { | ||
await patchPreferences(username, { prefLang: selectedLlocale }); | ||
await postSetLang(selectedLlocale); | ||
} else { | ||
getCookies().set(langCookieName, selectedLlocale); | ||
} | ||
publish(LOCALE_CHANGED, getLocale()); | ||
handleRtl(); | ||
} catch (error) { | ||
logError(error); | ||
} | ||
}; | ||
const getLocaleName = (locale) => { | ||
const langName = new Intl.DisplayNames([locale], { type: 'language', languageDisplay: 'standard' }).of(locale); | ||
return langName.replace(/^\w/, (c) => c.toUpperCase()); | ||
}; | ||
|
||
const LanguageSelector = ({ | ||
langCookieName, options, username, | ||
}) => { | ||
const [currentLocale, setLocale] = useState(getLocale()); | ||
const { width } = useWindowSize(); | ||
|
||
const handleSelect = (selectedLlocale) => { | ||
if (currentLocale !== selectedLlocale) { | ||
onLanguageSelected({ langCookieName, username, selectedLlocale }); | ||
} | ||
setLocale(selectedLlocale); | ||
}; | ||
|
||
const currentLocaleLabel = useMemo(() => { | ||
if (width < 576) { | ||
return null; | ||
} | ||
if (width < 768) { | ||
return getPrimaryLanguageSubtag(currentLocale).toUpperCase(); | ||
} | ||
return getLocaleName(currentLocale); | ||
}, [currentLocale, width]); | ||
|
||
return ( | ||
<Dropdown onSelect={handleSelect}> | ||
<Dropdown.Toggle | ||
id="lang-selector-dropdown" | ||
iconBefore={Language} | ||
variant="outline-primary" | ||
size="sm" | ||
> | ||
{currentLocaleLabel} | ||
</Dropdown.Toggle> | ||
<Dropdown.Menu> | ||
{options.map((locale) => ( | ||
<Dropdown.Item key={`lang-selector-${locale}`} eventKey={locale}> | ||
{getLocaleName(locale)} | ||
</Dropdown.Item> | ||
))} | ||
</Dropdown.Menu> | ||
</Dropdown> | ||
); | ||
}; | ||
|
||
LanguageSelector.propTypes = { | ||
langCookieName: PropTypes.string.isRequired, | ||
options: PropTypes.arrayOf(PropTypes.string).isRequired, | ||
username: PropTypes.string, | ||
}; | ||
|
||
export default injectIntl(LanguageSelector); |
Oops, something went wrong.