spee.ch/react/components/ChannelSelector.jsx

72 lines
2.4 KiB
React
Raw Normal View History

2018-01-04 23:14:03 +01:00
import React from 'react';
2018-01-06 03:26:57 +01:00
import ChannelLoginForm from './ChannelLoginForm.jsx';
import ChannelCreateForm from './ChannelCreateForm.jsx';
import { connect } from 'react-redux';
2018-01-04 23:14:03 +01:00
const LOGIN = 'login';
const CREATE = 'create';
2018-01-04 23:14:03 +01:00
class ChannelSelector extends React.Component {
constructor (props) {
super(props);
this.state = {
2018-01-06 00:11:45 +01:00
optionState: 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 () {
2018-01-10 20:26:01 +01:00
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 }) {
console.log('ChannelSelector will receive props');
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) {
this.setState({optionState: option});
}
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-06 00:11:45 +01:00
<select type="text" id="channel-name-select" className="select select--arrow" value={this.state.optionState} onChange={this.handleSelection}>
{ this.props.loggedInChannelName && <option value={this.props.loggedInChannelName} id="publish-channel-select-channel-option">{this.props.loggedInChannelName}</option> }
<option value="login">Existing</option>
<option value="create">New</option>
</select>
</div>
2018-01-10 20:26:01 +01:00
{ (this.state.optionState === LOGIN) && <ChannelLoginForm /> }
{ (this.state.optionState === CREATE) && <ChannelCreateForm /> }
</div>
)}
2018-01-04 23:14:03 +01:00
</div>
);
}
}
const mapStateToProps = state => {
return {
loggedInChannelName: state.loggedInChannel.name,
publishInChannel : state.publishInChannel,
};
};
export default connect(mapStateToProps, null)(ChannelSelector);