// @flow import { FormField } from 'component/common/form'; import * as MODALS from 'constants/modal_types'; import HOMEPAGE_LANGUAGES, { getHomepageLanguage } from 'constants/homepage_languages'; import Nag from 'component/common/nag'; import React from 'react'; import usePersistedState from 'effects/use-persisted-state'; import { getLanguageEngName, getLanguageName } from 'constants/languages'; const LOCALE_OPTIONS = { BOTH: 'both', LANG: 'lang', HOME: 'home', }; type Props = { localeLangs: Array, noLanguageSet: boolean, // redux homepageCode: string, doSetLanguage: (string) => void, doSetHomepage: (string) => void, doOpenModal: (string, {}) => void, }; export default function NagLocaleSwitch(props: Props) { const { localeLangs, noLanguageSet, homepageCode, doSetLanguage, doSetHomepage, doOpenModal } = props; const [localeSwitchDismissed, setLocaleSwitchDismissed] = usePersistedState('locale-switch-dismissed', false); const hasHomepageForLang = localeLangs.some((lang) => getHomepageLanguage(lang)); const hasHomepageSet = homepageCode !== 'en'; const optionToSwitch = (!hasHomepageForLang || hasHomepageSet) && !noLanguageSet ? undefined : !hasHomepageForLang || hasHomepageSet ? LOCALE_OPTIONS.LANG : !noLanguageSet ? LOCALE_OPTIONS.HOME : LOCALE_OPTIONS.BOTH; const [switchOption, setSwitchOption] = React.useState(optionToSwitch); if (localeSwitchDismissed || !optionToSwitch) return null; const message = __( // If no homepage, only suggest language switch optionToSwitch === LOCALE_OPTIONS.LANG ? 'There are language translations available for your location! Do you want to switch from English?' : optionToSwitch === LOCALE_OPTIONS.BOTH ? 'Homepage and language translations are available for your location! Do you want to switch?' : 'A homepage is available for your location! Do you want to switch?' ); function dismissNag() { setLocaleSwitchDismissed(true); } function handleSwitch() { const homepages = []; localeLangs.forEach((lang) => { const homepageLanguage = getHomepageLanguage(lang); if (homepageLanguage && !homepages.includes(homepageLanguage)) { homepages.push(homepageLanguage); } }); const homeSwitchSelected = switchOption === LOCALE_OPTIONS.BOTH || switchOption === LOCALE_OPTIONS.HOME; const multipleHomepages = homeSwitchSelected && homepages.length > 1; const langSwitchSelected = switchOption === LOCALE_OPTIONS.BOTH || switchOption === LOCALE_OPTIONS.LANG; const multipleLangs = langSwitchSelected && localeLangs.length > 1; // if language or homepage has more than 1 option, modal for selection // if some has only one option, still show the selection for confirmation of what's being switched if (multipleHomepages || multipleLangs) { doOpenModal(MODALS.CONFIRM, { title: __('Choose Your Preference'), body: ( <> {langSwitchSelected && } {homeSwitchSelected && } ), onConfirm: (closeModal) => { if (langSwitchSelected) { // $FlowFixMe const selection = document.querySelector('.language-switch.checked').id.split(' ')[1]; doSetLanguage(selection); } if (homeSwitchSelected) { // $FlowFixMe const selection = document.querySelector('.homepage-switch.checked').id.split(' ')[1]; let homepageSelection = ''; Object.values(HOMEPAGE_LANGUAGES).some((lang, index) => { if (lang === selection) { homepageSelection = Object.keys(HOMEPAGE_LANGUAGES)[index]; return true; } }); doSetHomepage(homepageSelection); } dismissNag(); closeModal(); }, }); // if selected switch has only one option, just make the switch } else { const onlyLanguage = localeLangs[0]; if (langSwitchSelected) doSetLanguage(onlyLanguage); if (homeSwitchSelected) doSetHomepage(onlyLanguage); dismissNag(); } } return ( setSwitchOption(e.target.value)} > ) } actionText={__('Switch Now')} onClick={handleSwitch} onClose={dismissNag} closeTitle={__('Dismiss')} /> ); } type HomepageProps = { homepages: Array, }; const HomepageSelect = (props: HomepageProps) => { const { homepages } = props; const [selection, setSelection] = React.useState(homepages[0]); return ( <>

{__('Homepage')}

{homepages.map((homepage) => ( setSelection(homepage)} /> ))} ); }; type LangProps = { langs: Array, }; const LanguageSelect = (props: LangProps) => { const { langs } = props; const [selection, setSelection] = React.useState(langs[0]); return ( <>

{__('Language')}

{langs.map((lang) => { const language = getLanguageEngName(lang); const languageName = getLanguageName(lang); const label = language === languageName ? language : `${language} - ${languageName}`; return ( setSelection(lang)} /> ); })} ); };