spee.ch/react/components/ChannelSelector.jsx

77 lines
2.5 KiB
React
Raw Normal View History

2018-01-04 23:14:03 +01:00
import React from 'react';
2018-01-13 00:02:42 +01:00
import PropTypes from 'prop-types';
2018-01-16 19:45:17 +01:00
import { connect } from 'react-redux';
import ChannelLoginForm from '../containers/ChannelLoginForm.jsx';
import ChannelCreateForm from '../containers/ChannelCreateForm.jsx';
2018-01-04 23:14:03 +01:00
2018-01-16 19:45:17 +01:00
const LOGIN = 'Existing';
const CREATE = 'New';
2018-01-04 23:14:03 +01:00
class ChannelSelector extends React.Component {
constructor (props) {
super(props);
this.state = {
2018-01-16 19:45:17 +01:00
selectedOption: LOGIN,
};
2018-01-06 00:11:45 +01:00
this.handleSelection = this.handleSelection.bind(this);
this.selectOption = this.selectOption.bind(this);
}
2018-01-05 22:59:25 +01:00
componentWillMount () {
console.log('ChannelSelector will mount');
2018-01-05 22:59:25 +01:00
if (this.props.loggedInChannelName) {
2018-01-10 20:26:01 +01:00
this.selectOption(this.props.loggedInChannelName);
}
}
componentWillReceiveProps ({ loggedInChannelName }) {
if (loggedInChannelName) {
this.selectOption(loggedInChannelName);
2018-01-05 22:59:25 +01:00
}
}
2018-01-06 00:11:45 +01:00
handleSelection (event) {
const selectedOption = event.target.selectedOptions[0].value;
2018-01-06 00:11:45 +01:00
this.selectOption(selectedOption);
}
selectOption (option) {
2018-01-16 19:45:17 +01:00
this.setState({selectedOption: option});
2018-01-06 00:11:45 +01:00
}
2018-01-04 23:14:03 +01:00
render () {
return (
<div>
{ this.props.publishInChannel && (
2018-01-10 20:26:01 +01:00
<div>
<p id="input-error-channel-select" className="info-message-placeholder info-message--failure">{this.props.channelError}</p>
<div className="column column--3">
<label className="label" htmlFor="channel-name-select">Channel:</label>
</div><div className="column column--7">
2018-01-16 19:45:17 +01:00
<select type="text" id="channel-name-select" className="select select--arrow" value={this.state.selectedOption} onChange={this.handleSelection}>
{ this.props.loggedInChannelName && <option value={this.props.loggedInChannelName} id="publish-channel-select-channel-option">{this.props.loggedInChannelName}</option> }
2018-01-16 19:45:17 +01:00
<option value={LOGIN}>Existing</option>
<option value={CREATE}>New</option>
</select>
</div>
2018-01-16 19:45:17 +01:00
{ (this.state.selectedOption === LOGIN) && <ChannelLoginForm /> }
2018-01-10 20:26:01 +01:00
2018-01-16 19:45:17 +01:00
{ (this.state.selectedOption === CREATE) && <ChannelCreateForm /> }
</div>
)}
2018-01-04 23:14:03 +01:00
</div>
);
}
}
const mapStateToProps = state => {
return {
loggedInChannelName: state.loggedInChannel.name,
publishInChannel : state.publishInChannel,
};
};
2018-01-13 00:02:42 +01:00
ChannelSelector.propTypes = {
loggedInChannelName: PropTypes.string,
publishInChannel : PropTypes.bool,
};
export default connect(mapStateToProps, null)(ChannelSelector);