2017-09-01 19:42:50 +00:00
2017-08-10 16:25:50 +00:00
import React from 'react' ;
2017-09-01 19:42:50 +00:00
import { connect } from 'react-redux' ;
import PropTypes from 'prop-types' ;
import { TextInput , View , Text , Switch , TouchableOpacity , ScrollView } from 'react-native' ;
import { createChannelRequest } from '../actions/createChannel' ;
import styles from './Styles' ;
2017-09-01 20:00:31 +00:00
import KeyboardView from '../presentation/KeyboardView' ;
2017-08-10 16:25:50 +00:00
2017-09-01 19:42:50 +00:00
@ connect ( state => ( {
result : state . createChannel
} ) , dispatch => ( {
createChannel : data => dispatch ( createChannelRequest ( data ) )
} ) )
2017-08-10 16:25:50 +00:00
export default class CreateChannelView extends React . Component {
static navigationOptions = ( ) => ( {
2017-09-01 19:42:50 +00:00
title : 'Create a New Channel'
2017-08-10 16:25:50 +00:00
} ) ;
2017-09-01 19:42:50 +00:00
static propTypes = {
createChannel : PropTypes . func . isRequired ,
result : PropTypes . object . isRequired ,
navigator : PropTypes . object . isRequired
}
2017-08-10 16:25:50 +00:00
constructor ( props ) {
super ( props ) ;
2017-09-01 19:42:50 +00:00
this . default = {
2017-08-10 16:25:50 +00:00
channelName : '' ,
type : true
} ;
2017-09-01 19:42:50 +00:00
this . state = this . default ;
this . props . navigator . setTitle ( {
title : 'Create Channel'
} ) ;
// this.props.navigator.setSubTitle({
// subtitle: 'Channels are where your team communicate.'
// });
2017-08-10 16:25:50 +00:00
}
submit ( ) {
2017-09-01 19:42:50 +00:00
if ( ! this . state . channelName . trim ( ) || this . props . result . isFetching ) {
return ;
}
2017-08-10 16:25:50 +00:00
const { channelName , users = [ ] , type = true } = this . state ;
2017-09-01 19:42:50 +00:00
this . props . createChannel ( { name : channelName , users , type } ) ;
2017-08-10 16:25:50 +00:00
}
render ( ) {
return (
2017-09-01 19:42:50 +00:00
< KeyboardView style = { [ styles . view _white , { flex : 0 , justifyContent : 'flex-start' } ] } >
< ScrollView >
< View style = { styles . formContainer } >
< Text style = { styles . label _white } > Channel Name < / T e x t >
< TextInput
value = { this . state . channelName }
style = { styles . input _white }
onChangeText = { channelName => this . setState ( { channelName } ) }
autoCorrect = { false }
returnKeyType = 'done'
autoCapitalize = 'none'
autoFocus
// onSubmitEditing={() => this.textInput.focus()}
placeholder = 'Type the channel name here'
/ >
{ ( this . props . result . failure && this . props . result . error . error === 'error-duplicate-channel-name' ) ? < Text style = { [ styles . label _white , { color : 'red' , flexGrow : 1 , paddingHorizontal : 0 , marginBottom : 20 } ] } > { this . props . result . error . reason } < / T e x t > : n u l l }
< View style = { [ styles . view _white , { flexDirection : 'row' , justifyContent : 'flex-start' , alignItems : 'center' , paddingHorizontal : 0 } ] } >
< Switch
style = { [ { flexGrow : 0 , flexShrink : 1 } ] }
value = { this . state . type }
onValueChange = { type => this . setState ( { type } ) }
/ >
< Text style = { [ styles . label _white , { flexGrow : 1 , paddingHorizontal : 10 } ] } > { this . state . type ? 'Public' : 'Private' } < / T e x t >
< / V i e w >
< Text style = { [ styles . label _white , { color : '#9ea2a8' , flexGrow : 1 , paddingHorizontal : 0 , marginBottom : 20 } ] } > { this . state . type ? 'Everyone can access this channel' : 'Just invited people can access this channel' } < / T e x t >
< TouchableOpacity onPress = { ( ) => this . submit ( ) } style = { [ styles . buttonContainer _white , { backgroundColor : ( this . state . channelName . length === 0 || this . props . result . isFetching ) ? '#e1e5e8' : '#1d74f5' } ] } >
< Text style = { styles . button _white } > { this . props . result . isFetching ? 'LOADING' : 'CREATE' } ! < / T e x t >
< / T o u c h a b l e O p a c i t y >
< / V i e w >
< / S c r o l l V i e w >
< / K e y b o a r d V i e w >
2017-08-10 16:25:50 +00:00
) ;
}
}