Skip to content

Commit 31ed2d3

Browse files
committed
Language selector is a functional component now
1 parent 52c3c36 commit 31ed2d3

File tree

1 file changed

+27
-38
lines changed

1 file changed

+27
-38
lines changed
+27-38
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,44 @@
11
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'
44
import { emitRootKeyChanged } from '../../redux/actions/GlobalActions'
55
import {
66
getCurrentLanguageOption,
77
languagesOptions,
88
setCurrentLanguageOption,
99
} from '../../utils/Language'
1010

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+
}
2114

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()
2420

21+
const handleChange = (value: string) => {
2522
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) {
2926
window.location.reload()
3027
}
3128
}
3229

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+
)
4642
}
4743

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

Comments
 (0)