import React from 'react'; 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'; import KeyboardView from '../presentation/KeyboardView'; @connect(state => ({ result: state.createChannel }), dispatch => ({ createChannel: data => dispatch(createChannelRequest(data)) })) export default class CreateChannelView extends React.Component { static navigationOptions = () => ({ title: 'Create a New Channel' }); static propTypes = { createChannel: PropTypes.func.isRequired, result: PropTypes.object.isRequired } constructor(props) { super(props); this.default = { channelName: '', type: true }; this.state = this.default; } submit() { if (!this.state.channelName.trim() || this.props.result.isFetching) { return; } const { channelName, users = [], type = true } = this.state; this.props.createChannel({ name: channelName, users, type }); } renderChannelNameError() { if (!this.props.result.failure || this.props.result.error.error !== 'error-duplicate-channel-name') { return null; } return ( {this.props.result.error.reason} ); } renderTypeSwitch() { return ( this.setState({ type })} /> {this.state.type ? 'Public' : 'Private'} ); } render() { return ( Channel Name this.setState({ channelName })} autoCorrect={false} returnKeyType='done' autoCapitalize='none' autoFocus // onSubmitEditing={() => this.textInput.focus()} placeholder='Type the channel name here' /> {this.renderChannelNameError()} {this.renderTypeSwitch()} {this.state.type ? 'Everyone can access this channel' : 'Just invited people can access this channel'} this.submit()} style={[ styles.buttonContainer_white, (this.state.channelName.length === 0 || this.props.result.isFetching) ? styles.disabledButton : styles.enabledButton ]} > {this.props.result.isFetching ? 'LOADING' : 'CREATE'}! ); } }