Wire up LanguageDropdownComponent to use Redux actions and remove local state.

This commit is contained in:
Daniel Ternyak 2017-04-14 01:19:26 -05:00
parent 29b3f9e44b
commit b6adc5330e

View File

@ -1,83 +1,45 @@
import React, {Component} from 'react' import React, {Component} from 'react'
import {languages} from 'reducers/config'
import PropTypes from 'prop-types';
const languages = [
{
'sign': 'en',
'name': 'English'
},
{
'sign': 'de',
'name': 'Deutsch'
},
{
'sign': 'el',
'name': 'Ελληνικά'
},
// {
// "sign": 'el',
// "name": 'Ελληνικά'
// },
// {
// "sign": 'el',
// "name": 'Ελληνικά'
// },
// {
// "sign": 'el',
// "name": 'Ελληνικά'
// },
// {
// "sign": 'el',
// "name": 'Ελληνικά'
// },
// {
// "sign": 'el',
// "name": 'Ελληνικά'
// },
]
export default class LanguageDropdownComponent extends Component { export default class LanguageDropdownComponent extends Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.state = {
languageToggle: false,
languageSelection: 0
}
} }
languageToggle() { static propTypes = {
let nextState = this.state; changeLanguage: PropTypes.func,
nextState['languageToggle'] = !nextState['languageToggle']; languageSelection: PropTypes.number,
this.setState(nextState) languageToggle: PropTypes.bool,
} toggleLanguageDropdown: PropTypes.func
};
changeLanguage(i) {
let nextState = this.state;
nextState["languageSelection"] = i;
nextState['languageToggle'] = false;
this.setState(nextState);
}
render() { render() {
let {
languageSelection,
changeLanguage,
toggleLanguageDropdown,
languageToggle
} = this.props;
return ( return (
<span className="dropdown"> <span className="dropdown">
<a tabIndex="0" aria-haspopup="true" aria-expanded="false" <a tabIndex="0" aria-haspopup="true" aria-expanded="false"
aria-label="change language. current language English" aria-label="change language. current language English"
className="dropdown-toggle" className="dropdown-toggle"
onClick={() => this.languageToggle()}> onClick={() => toggleLanguageDropdown()}>
{languages[this.state.languageSelection].name} {languages[languageSelection].name}
<i className="caret"/> <i className="caret"/>
</a> </a>
{ {
this.state.languageToggle && languageToggle &&
<ul className="dropdown-menu"> <ul className="dropdown-menu">
{languages.map((object, i) => { {languages.map((object, i) => {
return ( return (
<li key={i}> <li key={i}>
<a className={i === this.state.languageSelection ? 'active' : ''} <a className={i === languageSelection ? 'active' : ''}
onClick={() => this.changeLanguage(i)}> onClick={() => changeLanguage(i)}>
{object.name} {object.name}
</a> </a>
</li> </li>