2018-01-06 03:26:57 +01:00
import React from 'react' ;
2018-01-17 18:46:16 +01:00
import { setPublishInChannel } from '../actions' ;
import { connect } from 'react-redux' ;
import ChannelLoginForm from '../containers/ChannelLoginForm.jsx' ;
import ChannelCreateForm from '../containers/ChannelCreateForm.jsx' ;
const LOGIN = 'Existing' ;
const CREATE = 'New' ;
2018-01-06 03:26:57 +01:00
2018-01-17 00:55:29 +01:00
class channelSelect extends React . Component {
2018-01-06 03:26:57 +01:00
constructor ( props ) {
super ( props ) ;
2018-01-17 18:46:16 +01:00
this . state = {
selectedOption : LOGIN ,
} ;
2018-01-06 03:26:57 +01:00
this . toggleAnonymousPublish = this . toggleAnonymousPublish . bind ( this ) ;
2018-01-17 18:46:16 +01:00
this . handleSelection = this . handleSelection . bind ( this ) ;
this . selectOption = this . selectOption . bind ( this ) ;
}
componentWillMount ( ) {
console . log ( 'ChannelSelector will mount' ) ;
if ( this . props . loggedInChannelName ) {
this . selectOption ( this . props . loggedInChannelName ) ;
}
}
componentWillReceiveProps ( { loggedInChannelName } ) {
if ( loggedInChannelName ) {
this . selectOption ( loggedInChannelName ) ;
}
2018-01-06 03:26:57 +01:00
}
toggleAnonymousPublish ( event ) {
const value = event . target . value ;
if ( value === 'anonymous' ) {
2018-01-10 03:49:26 +01:00
this . props . onPublishInChannelChange ( false ) ;
2018-01-06 03:26:57 +01:00
} else {
2018-01-10 03:49:26 +01:00
this . props . onPublishInChannelChange ( true ) ;
2018-01-06 03:26:57 +01:00
}
}
2018-01-17 18:46:16 +01:00
handleSelection ( event ) {
const selectedOption = event . target . selectedOptions [ 0 ] . value ;
this . selectOption ( selectedOption ) ;
}
selectOption ( option ) {
this . setState ( { selectedOption : option } ) ;
}
2018-01-06 03:26:57 +01:00
render ( ) {
return (
2018-01-17 18:46:16 +01:00
< div >
< form >
< div className = "column column--3 column--med-10" >
< input type = "radio" name = "anonymous-or-channel" id = "anonymous-radio" className = "input-radio" value = "anonymous" checked = { ! this . props . publishInChannel } onChange = { this . toggleAnonymousPublish } / >
< label className = "label label--pointer" htmlFor = "anonymous-radio" > Anonymous < / label >
< / div >
< div className = "column column--7 column--med-10" >
< input type = "radio" name = "anonymous-or-channel" id = "channel-radio" className = "input-radio" value = "in a channel" checked = { this . props . publishInChannel } onChange = { this . toggleAnonymousPublish } / >
< label className = "label label--pointer" htmlFor = "channel-radio" > In a channel < / label >
< / div >
< / form >
{ this . props . publishInChannel && (
< 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" >
< 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 > }
< option value = { LOGIN } > Existing < / option >
< option value = { CREATE } > New < / option >
< / select >
< / div >
{ ( this . state . selectedOption === LOGIN ) && < ChannelLoginForm / > }
{ ( this . state . selectedOption === CREATE ) && < ChannelCreateForm / > }
< / div >
) }
< / div >
2018-01-06 03:26:57 +01:00
) ;
}
}
2018-01-10 03:25:38 +01:00
const mapStateToProps = state => {
return {
2018-01-17 18:46:16 +01:00
loggedInChannelName : state . loggedInChannel . name ,
publishInChannel : state . publishInChannel ,
2018-01-10 03:25:38 +01:00
} ;
} ;
const mapDispatchToProps = dispatch => {
return {
2018-01-10 03:49:26 +01:00
onPublishInChannelChange : ( value ) => {
2018-01-10 03:25:38 +01:00
dispatch ( setPublishInChannel ( value ) ) ;
} ,
} ;
}
2018-01-17 00:55:29 +01:00
export default connect ( mapStateToProps , mapDispatchToProps ) ( channelSelect ) ;