|
1 | 1 | import { Select } from 'antd'
|
2 |
| -import { Component } from 'react' |
3 |
| -import { connect } from 'react-redux' |
| 2 | +import { useEffect, useState } from 'react' |
| 3 | +import { useDispatch } from 'react-redux' |
4 | 4 | import { emitRootKeyChanged } from '../../redux/actions/GlobalActions'
|
5 | 5 | import {
|
6 | 6 | getCurrentLanguageOption,
|
7 | 7 | languagesOptions,
|
8 | 8 | setCurrentLanguageOption,
|
9 | 9 | } from '../../utils/Language'
|
10 | 10 |
|
11 |
| -class LanguageSelector extends Component< |
12 |
| - { forceReload?: boolean; emitRootKeyChanged: Function }, |
13 |
| - { currentLanguage: string } |
14 |
| -> { |
15 |
| - constructor(props: any) { |
16 |
| - super(props) |
17 |
| - this.state = { |
18 |
| - currentLanguage: getCurrentLanguageOption().value, |
19 |
| - } |
20 |
| - } |
| 11 | +interface LanguageSelectorProps { |
| 12 | + forceReload?: boolean |
| 13 | +} |
21 | 14 |
|
22 |
| - handleChange(value: string) { |
23 |
| - const self = this |
| 15 | +const LanguageSelector: React.FC<LanguageSelectorProps> = ({ forceReload }) => { |
| 16 | + const [currentLanguage, setCurrentLanguage] = useState( |
| 17 | + getCurrentLanguageOption().value |
| 18 | + ) |
| 19 | + const dispatch = useDispatch() |
24 | 20 |
|
| 21 | + const handleChange = (value: string) => { |
25 | 22 | setCurrentLanguageOption(value)
|
26 |
| - self.setState({ currentLanguage: value }) |
27 |
| - self.props.emitRootKeyChanged() |
28 |
| - if (self.props.forceReload) { |
| 23 | + setCurrentLanguage(value) |
| 24 | + dispatch(emitRootKeyChanged()) |
| 25 | + if (forceReload) { |
29 | 26 | window.location.reload()
|
30 | 27 | }
|
31 | 28 | }
|
32 | 29 |
|
33 |
| - render() { |
34 |
| - const self = this |
35 |
| - return ( |
36 |
| - <Select |
37 |
| - style={{ width: 150 }} |
38 |
| - options={languagesOptions} |
39 |
| - value={self.state.currentLanguage} |
40 |
| - onChange={(v) => { |
41 |
| - self.handleChange(v) |
42 |
| - }} |
43 |
| - /> |
44 |
| - ) |
45 |
| - } |
| 30 | + useEffect(() => { |
| 31 | + setCurrentLanguage(getCurrentLanguageOption().value) |
| 32 | + }, []) |
| 33 | + |
| 34 | + return ( |
| 35 | + <Select |
| 36 | + style={{ width: 150 }} |
| 37 | + options={languagesOptions} |
| 38 | + value={currentLanguage} |
| 39 | + onChange={handleChange} |
| 40 | + /> |
| 41 | + ) |
46 | 42 | }
|
47 | 43 |
|
48 |
| -export default connect( |
49 |
| - undefined, |
50 |
| - { |
51 |
| - emitRootKeyChanged: emitRootKeyChanged, |
52 |
| - }, |
53 |
| - undefined, |
54 |
| - { forwardRef: true } |
55 |
| -)(LanguageSelector) |
| 44 | +export default LanguageSelector |
0 commit comments