2020-09-07 10:22:16 +02:00
|
|
|
import React, { useState, useRef } from 'react';
|
|
|
|
import { useDispatch, useSelector } from 'react-redux';
|
|
|
|
import Globe from 'assets/globe.svg';
|
|
|
|
import useDocumentClick from 'hooks/useDocumentClick';
|
|
|
|
import { updateApp } from 'redux/actions/app';
|
|
|
|
import Menu from './Menu';
|
|
|
|
import Button from './Button';
|
2020-09-08 04:48:40 +02:00
|
|
|
import { menuOptions } from 'lib/lang';
|
|
|
|
import styles from './LanguageButton.module.css';
|
2020-09-07 10:22:16 +02:00
|
|
|
|
2020-09-08 00:25:09 +02:00
|
|
|
export default function LanguageButton({ menuPosition = 'bottom', menuAlign = 'left' }) {
|
2020-09-07 10:22:16 +02:00
|
|
|
const dispatch = useDispatch();
|
|
|
|
const [showMenu, setShowMenu] = useState(false);
|
|
|
|
const locale = useSelector(state => state.app.locale);
|
|
|
|
const ref = useRef();
|
2020-09-08 04:48:40 +02:00
|
|
|
const selectedLocale = menuOptions.find(e => e.value === locale)?.display;
|
2020-09-07 10:22:16 +02:00
|
|
|
|
|
|
|
function handleSelect(value) {
|
|
|
|
dispatch(updateApp({ locale: value }));
|
|
|
|
window.localStorage.setItem('locale', value);
|
|
|
|
setShowMenu(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
useDocumentClick(e => {
|
|
|
|
if (!ref.current.contains(e.target)) {
|
|
|
|
setShowMenu(false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div ref={ref} className={styles.container}>
|
|
|
|
<Button icon={<Globe />} onClick={() => setShowMenu(true)} size="small">
|
2020-09-08 04:48:40 +02:00
|
|
|
<div className={locale}>{selectedLocale}</div>
|
2020-09-07 10:22:16 +02:00
|
|
|
</Button>
|
2020-09-08 00:25:09 +02:00
|
|
|
{showMenu && (
|
|
|
|
<Menu
|
|
|
|
options={menuOptions}
|
|
|
|
onSelect={handleSelect}
|
|
|
|
float={menuPosition}
|
|
|
|
align={menuAlign}
|
|
|
|
/>
|
|
|
|
)}
|
2020-09-07 10:22:16 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|