From 65c9812e27942606be33dc5288d2374feb1da8f0 Mon Sep 17 00:00:00 2001 From: Ivo Branco Date: Wed, 11 Dec 2024 17:48:44 +0000 Subject: [PATCH] feat: change language selector to dropdown fccn/nau-technical#53 --- README.rst | 2 +- src/_footer.scss | 9 ++++ src/components/Footer.jsx | 14 ++--- src/components/Footer.test.jsx | 2 +- src/components/LanguageSelector.jsx | 83 +++++++++++++++++------------ 5 files changed, 67 insertions(+), 43 deletions(-) diff --git a/README.rst b/README.rst index 0bcb02a8f..17152cb02 100644 --- a/README.rst +++ b/README.rst @@ -50,7 +50,7 @@ Optionally, use the following variables to configure the Terms of Service Modal * ``TERMS_OF_SERVICE_URL`` - The URL for the terms of service. * ``TOS_AND_HONOR_CODE`` - The URL for the honor code. * ``ENABLE_FOOTER_LANG_SELECTOR`` - A boolean to enable the lnaguage selector in the footer component. -* ``SITE_SUPPORTED_LENGUAGES`` - A list with all the languages to display in the selector. +* ``SITE_SUPPORTED_LANGUAGES`` - A list with all the languages to display in the selector. Installation ============ diff --git a/src/_footer.scss b/src/_footer.scss index 72006de30..13d9e6687 100644 --- a/src/_footer.scss +++ b/src/_footer.scss @@ -8,6 +8,15 @@ $footer-links-navigation-color: $footer-text; background-color: $footer-background-color; padding-top: 2rem; padding-bottom: 2rem; + + .language-selector { + button { + color: $footer-text; + padding: 0; + border: unset; + background-color: unset; + } + } } .footer-copyright { diff --git a/src/components/Footer.jsx b/src/components/Footer.jsx index c149d04e8..833a88eca 100644 --- a/src/components/Footer.jsx +++ b/src/components/Footer.jsx @@ -137,17 +137,19 @@ class SiteFooter extends React.Component { /> + {config.ENABLE_FOOTER_LANG_SELECTOR && ( +
+ +
+ )} - {config.ENABLE_FOOTER_LANG_SELECTOR && ( - - )} diff --git a/src/components/Footer.test.jsx b/src/components/Footer.test.jsx index 010055eb7..55f399f3a 100644 --- a/src/components/Footer.test.jsx +++ b/src/components/Footer.test.jsx @@ -44,7 +44,7 @@ const FooterWithLanguageSelector = () => { LMS_BASE_URL: process.env.LMS_BASE_URL, SITE_NAME: process.env.SITE_NAME, ENABLE_FOOTER_LANG_SELECTOR: true, - SITE_SUPPORTED_LENGUAGES: [ + SITE_SUPPORTED_LANGUAGES: [ { label: 'English', value: 'en' }, { label: 'Português', value: 'pt-pt' }, ], diff --git a/src/components/LanguageSelector.jsx b/src/components/LanguageSelector.jsx index be7335129..5c000c60d 100644 --- a/src/components/LanguageSelector.jsx +++ b/src/components/LanguageSelector.jsx @@ -1,9 +1,12 @@ import React from 'react'; import PropTypes from 'prop-types'; +import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; +import { faGlobe } from '@fortawesome/free-solid-svg-icons'; import { publish } from '@edx/frontend-platform'; import { getLocale, injectIntl, intlShape, FormattedMessage, LOCALE_CHANGED, handleRtl, } from '@edx/frontend-platform/i18n'; +import { Dropdown } from '@openedx/paragon'; import { logError } from '@edx/frontend-platform/logging'; import { patchPreferences, postSetLang } from './data/api'; @@ -22,49 +25,58 @@ const onLanguageSelected = async (username, selectedLanguageCode) => { }; const LanguageSelector = ({ - intl, options, authenticatedUser, ...props + intl, options, authenticatedUser, compact, ...props }) => { - const handleSubmit = (e) => { - e.preventDefault(); + + const languageLabel = (languageCode) => { + const option = options.find( ({ value, label }) => value === languageCode ) + return option ? option.label : null + } + + const handleChange = (languageCode, event) => { const previousSiteLanguage = getLocale(); - const languageCode = e.target.elements['site-footer-language-select'].value; + console.debug(previousSiteLanguage, languageCode, authenticatedUser); + if (previousSiteLanguage !== languageCode) { onLanguageSelected(authenticatedUser?.username, languageCode); } + + event.target.parentElement.parentElement.querySelector(".languageLabel").innerHTML = languageLabel(languageCode); }; + const currentLangLabel = languageLabel(intl.locale) + const showLabel = !Boolean(compact || false); + return ( -
-
- {/* eslint-disable-next-line jsx-a11y/label-has-associated-control */} - - - -
-
+ <> + + + + {showLabel && ( + currentLangLabel ? ( + + {currentLangLabel} + + ) : ( + + + + ) + )} + + + {options.map(({ value, label }) => ( + + {label} + + ))} + + + ); }; @@ -73,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,