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,
|
2017-09-21 17:08:00 +00:00
|
|
|
result: PropTypes.object.isRequired
|
2017-09-01 19:42:50 +00:00
|
|
|
}
|
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;
|
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
|
|
|
}
|
|
|
|
|
2017-09-21 17:08:00 +00:00
|
|
|
renderChannelNameError() {
|
|
|
|
if (!this.props.result.failure || this.props.result.error.error !== 'error-duplicate-channel-name') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Text style={[styles.label_white, styles.label_error]}>
|
|
|
|
{this.props.result.error.reason}
|
|
|
|
</Text>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderTypeSwitch() {
|
|
|
|
return (
|
|
|
|
<View style={[styles.view_white, styles.switchContainer]}>
|
|
|
|
<Switch
|
|
|
|
style={[{ flexGrow: 0, flexShrink: 1 }]}
|
|
|
|
value={this.state.type}
|
|
|
|
onValueChange={type => this.setState({ type })}
|
|
|
|
/>
|
|
|
|
<Text style={[styles.label_white, styles.switchLabel]}>
|
|
|
|
{this.state.type ? 'Public' : 'Private'}
|
|
|
|
</Text>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-08-10 16:25:50 +00:00
|
|
|
render() {
|
|
|
|
return (
|
2017-09-21 17:08:00 +00:00
|
|
|
<KeyboardView style={[styles.view_white, { flex: 1, justifyContent: 'flex-start' }]}>
|
2017-09-01 19:42:50 +00:00
|
|
|
<ScrollView>
|
|
|
|
<View style={styles.formContainer}>
|
|
|
|
<Text style={styles.label_white}>Channel Name</Text>
|
|
|
|
<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'
|
|
|
|
/>
|
2017-09-21 17:08:00 +00:00
|
|
|
{this.renderChannelNameError()}
|
|
|
|
{this.renderTypeSwitch()}
|
|
|
|
<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'}
|
|
|
|
</Text>
|
|
|
|
<TouchableOpacity
|
|
|
|
onPress={() => this.submit()}
|
|
|
|
style={[
|
|
|
|
styles.buttonContainer_white,
|
|
|
|
(this.state.channelName.length === 0 || this.props.result.isFetching) ?
|
|
|
|
styles.disabledButton : styles.enabledButton
|
|
|
|
]}
|
|
|
|
>
|
|
|
|
<Text style={styles.button_white}>
|
|
|
|
{this.props.result.isFetching ? 'LOADING' : 'CREATE'}!
|
|
|
|
</Text>
|
2017-09-01 19:42:50 +00:00
|
|
|
</TouchableOpacity>
|
|
|
|
</View>
|
|
|
|
</ScrollView>
|
|
|
|
</KeyboardView>
|
2017-08-10 16:25:50 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|