File size: 1,566 Bytes
56b6519
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
70
import { t } from 'i18next';
import { useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';

import Card from '../../components/card/Card';
import SelectDropdown from '../../components/dropdown/SelectDropdown';

const languageOptions = [
  {
    id: 1,
    value: 'en-US',
  },
  {
    id: 2,
    value: 'de-DE',
  },
  {
    id: 3,
    value: 'fr-FR',
  },
  {
    id: 4,
    value: 'zh-CN',
  },
];

export const Settings = () => {
  // <---------------------
  // Lógica de selector de idiomas. TODO: Moverlo a componente

  const { i18n } = useTranslation();

  let storedLanguage = localStorage.getItem('system_language');

  if (!storedLanguage) {
    storedLanguage = 'en-US';
  }

  const [language, setLanguage] = useState({
    id: languageOptions.find(item => item.value === storedLanguage)?.id ?? 1,
    value: storedLanguage,
  });

  useEffect(() => {
    i18n.changeLanguage(language.value).catch(console.error);
    localStorage.setItem('system_language', language.value);
  }, [i18n, language]);

  // --------------------->

  return (
    <div className="p-4 md:w-2/3 md:mt-8 mx-auto">
      <Card title={t('generalSettings')}>
        <div className="px-2">
          <h2>{t('changeDisplayLanguage')}</h2>

          <div className="mt-2 mx-2">
            <SelectDropdown
              items={languageOptions}
              onChange={setLanguage}
              selected={language}
              title={t('changeDisplayLanguageInfo')}
            />
          </div>
        </div>
      </Card>
    </div>
  );
};