Skip to content

Commit

Permalink
feat: Language selector with optional compact
Browse files Browse the repository at this point in the history
  • Loading branch information
igobranco committed Dec 11, 2024
1 parent bc8702e commit 648342b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 17 deletions.
55 changes: 42 additions & 13 deletions src/LanguageSelector/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,58 @@ const onLanguageSelected = async (username, selectedLanguageCode) => {
};

const LanguageSelector = ({
intl, options, authenticatedUser, ...props
intl, options, authenticatedUser, compact, ...props

Check failure on line 28 in src/LanguageSelector/index.jsx

View workflow job for this annotation

GitHub Actions / tests

'props' is defined but never used
}) => {

Check failure on line 29 in src/LanguageSelector/index.jsx

View workflow job for this annotation

GitHub Actions / tests

Block must not be padded by blank lines

const languageLabel = (languageCode) => {
const option = options.find( ({ value, label }) => value === languageCode )

Check failure on line 32 in src/LanguageSelector/index.jsx

View workflow job for this annotation

GitHub Actions / tests

There should be no space after this paren

Check failure on line 32 in src/LanguageSelector/index.jsx

View workflow job for this annotation

GitHub Actions / tests

'label' is defined but never used

Check failure on line 32 in src/LanguageSelector/index.jsx

View workflow job for this annotation

GitHub Actions / tests

There should be no space before this paren

Check failure on line 32 in src/LanguageSelector/index.jsx

View workflow job for this annotation

GitHub Actions / tests

Missing semicolon
return option ? option.label : null

Check failure on line 33 in src/LanguageSelector/index.jsx

View workflow job for this annotation

GitHub Actions / tests

Missing semicolon
}

Check failure on line 34 in src/LanguageSelector/index.jsx

View workflow job for this annotation

GitHub Actions / tests

Missing semicolon

const handleChange = (languageCode, event) => {
const previousSiteLanguage = getLocale();
console.debug(previousSiteLanguage, languageCode, authenticatedUser);

Check warning on line 38 in src/LanguageSelector/index.jsx

View workflow job for this annotation

GitHub Actions / tests

Unexpected console statement

if (previousSiteLanguage !== languageCode) {
onLanguageSelected(authenticatedUser?.username, languageCode);
}

event.target.parentElement.parentElement.querySelector(".languageLabel").innerHTML = languageLabel(languageCode);

Check failure on line 44 in src/LanguageSelector/index.jsx

View workflow job for this annotation

GitHub Actions / tests

Assignment to property of function parameter 'event'

Check failure on line 44 in src/LanguageSelector/index.jsx

View workflow job for this annotation

GitHub Actions / tests

Strings must use singlequote
};

const currentLangLabel = languageLabel(intl.locale)
const showLabel = !Boolean(compact || false);

return (
<Dropdown className="language-selector" >
<Dropdown.Toggle variant="outline-primary">
<FontAwesomeIcon icon={faGlobe} />
</Dropdown.Toggle>
<Dropdown.Menu>
{options.map(({ value, label }) => (
<Dropdown.Item key={value} eventKey={value} onSelect={handleChange}>
{label}
</Dropdown.Item>
))}
</Dropdown.Menu>
</Dropdown>
<>
<Dropdown className="language-selector">
<Dropdown.Toggle variant="outline-primary">
<FontAwesomeIcon icon={faGlobe} />
{showLabel && (
currentLangLabel ? (
<span class="pl-1 languageLabel">
{currentLangLabel}
</span>
) : (
<span class="pl-1">
<FormattedMessage
id="footer.languageForm.select.label"
defaultMessage="Choose Language"
description="The label for the laguage select part of the language selection form."
/>
</span>
)
)}
</Dropdown.Toggle>
<Dropdown.Menu>
{options.map(({ value, label }) => (
<Dropdown.Item key={value} eventKey={value} onSelect={handleChange}>
{label}
</Dropdown.Item>
))}
</Dropdown.Menu>
</Dropdown>
</>
);
};

Expand All @@ -57,6 +85,7 @@ LanguageSelector.propTypes = {
username: PropTypes.string,
}).isRequired,
intl: intlShape.isRequired,
compact: PropTypes.bool,
options: PropTypes.arrayOf(PropTypes.shape({
value: PropTypes.string,
label: PropTypes.string,
Expand Down
18 changes: 14 additions & 4 deletions src/learning-header/LearningHeader.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,20 @@ const LearningHeader = ({
</div>
{getConfig().ENABLE_HEADER_LANG_SELECTOR && (
<div className="mx-2 d-md-inline-flex">
<LanguageSelector
options={JSON.parse(getConfig().SITE_SUPPORTED_LANGUAGES)}
authenticatedUser={authenticatedUser}
/>
<Responsive maxWidth={1000}>
<LanguageSelector
options={JSON.parse(getConfig().SITE_SUPPORTED_LANGUAGES)}
compact={true}
authenticatedUser={authenticatedUser}
/>
</Responsive>
<Responsive minWidth={1000}>
<LanguageSelector
options={JSON.parse(getConfig().SITE_SUPPORTED_LANGUAGES)}
compact={false}
authenticatedUser={authenticatedUser}
/>
</Responsive>
</div>
)}
{showUserDropdown && authenticatedUser && (
Expand Down

0 comments on commit 648342b

Please sign in to comment.