summaryrefslogtreecommitdiff
path: root/client/src/components/LanguageSelect/LanguageSelect.jsx
blob: dd2997f3457bd502348f242a2f6b3990f8ad63fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import React, { useState, useEffect } from 'react'
import { useTranslation } from 'react-i18next'
import i18next from 'i18next'
import { Button, Popover, List, ListItem, ListSubheader } from '@mui/material'

import styles from './LanguageSelect.module.css'

const languageMap = {
  en: { label: 'English', dir: 'ltr', active: true },
  hi: { label: 'Hindi', dir: 'ltr', active: false }
}

const LanguageSelect = () => {
  const selected = localStorage.getItem('i18nextLng') || 'en'
  const { t } = useTranslation()

  const [menuAnchor, setMenuAnchor] = useState(null)
  useEffect(() => {
    document.body.dir = languageMap[selected]?.dir
  }, [menuAnchor, selected])

  return (
    <div className={styles.language}>
      <Button
        onClick={({ currentTarget }) => setMenuAnchor(currentTarget)}
        sx={{ margin: '0px 15px', padding: '0px' }}
      >
        <img
          src={`${process.env.PUBLIC_URL}/assets/images/language.svg`}
          height="80px"
          width="80px"
        />
      </Button>
      <Popover
        open={!!menuAnchor}
        anchorEl={menuAnchor}
        onClose={() => setMenuAnchor(null)}
        anchorOrigin={{
          vertical: 'bottom',
          horizontal: 'right'
        }}
        transformOrigin={{
          vertical: 'top',
          horizontal: 'right'
        }}
      >
        <div>
          <List>
            <ListSubheader>{t('SELECT_LANGUAGE')}</ListSubheader>
            {Object.keys(languageMap)?.map((item) => (
              <ListItem
                button
                key={item}
                onClick={() => {
                  i18next.changeLanguage(item)
                  setMenuAnchor(null)
                }}
              >
                {languageMap[item].label}
              </ListItem>
            ))}
          </List>
        </div>
      </Popover>
    </div>
  )
}

export default LanguageSelect