2021-10-24 05:48:06 +02:00
|
|
|
import { useCallback, useEffect, useState } from 'react'
|
2021-10-24 05:19:39 +02:00
|
|
|
import useTranslation from 'next-translate/useTranslation'
|
|
|
|
import setLanguage from 'next-translate/setLanguage'
|
2021-10-24 05:48:06 +02:00
|
|
|
import classNames from 'classnames'
|
2021-10-24 05:19:39 +02:00
|
|
|
|
|
|
|
import { Arrow } from './Arrow'
|
|
|
|
import { LanguageFlag } from './LanguageFlag'
|
2021-10-24 05:48:06 +02:00
|
|
|
import i18n from '../../../i18n.json'
|
2021-10-24 05:19:39 +02:00
|
|
|
|
|
|
|
export const Language: React.FC = () => {
|
|
|
|
const { lang: currentLanguage } = useTranslation()
|
|
|
|
const [hiddenMenu, setHiddenMenu] = useState(true)
|
|
|
|
|
2021-10-24 05:48:06 +02:00
|
|
|
const handleHiddenMenu = useCallback(() => {
|
|
|
|
setHiddenMenu(!hiddenMenu)
|
|
|
|
}, [hiddenMenu])
|
|
|
|
|
2021-10-24 05:19:39 +02:00
|
|
|
useEffect(() => {
|
|
|
|
if (!hiddenMenu) {
|
|
|
|
window.document.addEventListener('click', handleHiddenMenu)
|
|
|
|
} else {
|
|
|
|
window.document.removeEventListener('click', handleHiddenMenu)
|
|
|
|
}
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
window.document.removeEventListener('click', handleHiddenMenu)
|
|
|
|
}
|
2021-10-24 05:48:06 +02:00
|
|
|
}, [hiddenMenu, handleHiddenMenu])
|
2021-10-24 05:19:39 +02:00
|
|
|
|
2021-10-24 05:48:06 +02:00
|
|
|
const handleLanguage = async (language: string): Promise<void> => {
|
2021-10-24 05:19:39 +02:00
|
|
|
await setLanguage(language)
|
|
|
|
handleHiddenMenu()
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2022-01-14 23:15:51 +01:00
|
|
|
<div className='relative flex flex-col justify-center items-center cursor-pointer'>
|
2021-10-24 05:48:06 +02:00
|
|
|
<div
|
|
|
|
data-cy='language-click'
|
|
|
|
className='flex items-center mr-5'
|
|
|
|
onClick={handleHiddenMenu}
|
|
|
|
>
|
|
|
|
<LanguageFlag language={currentLanguage} />
|
|
|
|
<Arrow />
|
2021-10-24 05:19:39 +02:00
|
|
|
</div>
|
|
|
|
|
2021-10-24 05:48:06 +02:00
|
|
|
<ul
|
|
|
|
data-cy='languages-list'
|
|
|
|
className={classNames(
|
2022-01-14 23:15:51 +01:00
|
|
|
'flex flex-col justify-center items-center absolute p-0 -bottom-16 z-10 w-24 mt-3 mr-4 rounded-lg list-none shadow-light dark:shadow-dark bg-white dark:bg-black',
|
2021-10-24 05:48:06 +02:00
|
|
|
{ hidden: hiddenMenu }
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
{i18n.locales.map((language, index) => {
|
|
|
|
if (language === currentLanguage) {
|
|
|
|
return null
|
2021-10-24 05:19:39 +02:00
|
|
|
}
|
2021-10-24 05:48:06 +02:00
|
|
|
return (
|
|
|
|
<li
|
|
|
|
key={index}
|
|
|
|
className='flex items-center justify-center w-full h-12 hover:bg-[#4f545c] hover:bg-opacity-20 pl-2'
|
|
|
|
onClick={async () => await handleLanguage(language)}
|
|
|
|
>
|
|
|
|
<LanguageFlag language={language} />
|
|
|
|
</li>
|
|
|
|
)
|
|
|
|
})}
|
|
|
|
</ul>
|
|
|
|
</div>
|
2021-10-24 05:19:39 +02:00
|
|
|
)
|
|
|
|
}
|