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';
|
2017-11-13 12:49:19 +00:00
|
|
|
import { TextInput, View, Text, Switch, TouchableOpacity, SafeAreaView } from 'react-native';
|
2018-04-10 13:03:54 +00:00
|
|
|
import Spinner from 'react-native-loading-spinner-overlay';
|
2018-04-03 16:24:59 +00:00
|
|
|
|
|
|
|
import LoggedView from './View';
|
2017-09-01 19:42:50 +00:00
|
|
|
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-25 13:15:28 +00:00
|
|
|
@connect(
|
|
|
|
state => ({
|
2018-04-10 13:03:54 +00:00
|
|
|
createChannel: state.createChannel,
|
2017-09-25 13:15:28 +00:00
|
|
|
users: state.createChannel.users
|
|
|
|
}),
|
|
|
|
dispatch => ({
|
2018-04-10 13:03:54 +00:00
|
|
|
create: data => dispatch(createChannelRequest(data))
|
2017-09-25 13:15:28 +00:00
|
|
|
})
|
|
|
|
)
|
2018-04-03 16:24:59 +00:00
|
|
|
export default class CreateChannelView extends LoggedView {
|
2017-08-10 16:25:50 +00:00
|
|
|
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 = {
|
2018-04-10 13:03:54 +00:00
|
|
|
create: PropTypes.func.isRequired,
|
|
|
|
createChannel: PropTypes.object.isRequired,
|
2017-09-25 13:15:28 +00:00
|
|
|
users: PropTypes.array.isRequired,
|
|
|
|
navigation: PropTypes.object.isRequired
|
|
|
|
};
|
2017-08-10 16:25:50 +00:00
|
|
|
|
|
|
|
constructor(props) {
|
2018-04-03 16:24:59 +00:00
|
|
|
super('CreateChannelView', props);
|
2018-04-10 13:03:54 +00:00
|
|
|
this.state = {
|
2017-08-10 16:25:50 +00:00
|
|
|
channelName: '',
|
|
|
|
type: true
|
|
|
|
};
|
2017-09-25 13:15:28 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 16:25:50 +00:00
|
|
|
submit() {
|
2018-04-10 13:03:54 +00:00
|
|
|
if (!this.state.channelName.trim() || this.props.createChannel.isFetching) {
|
2017-09-01 19:42:50 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-09-25 13:15:28 +00:00
|
|
|
const { channelName, type = true } = this.state;
|
|
|
|
let { users } = this.props;
|
|
|
|
|
|
|
|
// transform users object into array of usernames
|
|
|
|
users = users.map(user => user.name);
|
|
|
|
|
|
|
|
// create channel
|
2018-04-10 13:03:54 +00:00
|
|
|
this.props.create({ name: channelName, users, type });
|
2017-08-10 16:25:50 +00:00
|
|
|
}
|
|
|
|
|
2017-09-21 17:08:00 +00:00
|
|
|
renderChannelNameError() {
|
2017-09-25 13:15:28 +00:00
|
|
|
if (
|
2018-04-10 13:03:54 +00:00
|
|
|
!this.props.createChannel.failure ||
|
|
|
|
this.props.createChannel.error.error !== 'error-duplicate-channel-name'
|
2017-09-25 13:15:28 +00:00
|
|
|
) {
|
2017-09-21 17:08:00 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2018-04-10 13:03:54 +00:00
|
|
|
<Text style={[styles.label_white, styles.label_error]}>
|
|
|
|
{this.props.createChannel.error.reason}
|
|
|
|
</Text>
|
2017-09-21 17:08:00 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-11-07 16:28:02 +00:00
|
|
|
<KeyboardView
|
|
|
|
style={[styles.defaultViewBackground, { flex: 1 }]}
|
|
|
|
contentContainerStyle={styles.defaultView}
|
|
|
|
>
|
2017-11-13 12:49:19 +00:00
|
|
|
<SafeAreaView style={styles.formContainer}>
|
2017-11-07 16:28:02 +00:00
|
|
|
<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
|
|
|
|
placeholder='Type the channel name here'
|
|
|
|
/>
|
|
|
|
{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()}
|
2018-04-10 13:03:54 +00:00
|
|
|
style={[styles.buttonContainer_white, styles.enabledButton]}
|
2017-11-07 16:28:02 +00:00
|
|
|
>
|
2018-04-10 13:03:54 +00:00
|
|
|
<Text style={styles.button_white}>CREATE</Text>
|
2017-11-07 16:28:02 +00:00
|
|
|
</TouchableOpacity>
|
2017-11-13 12:49:19 +00:00
|
|
|
</SafeAreaView>
|
2018-04-10 13:03:54 +00:00
|
|
|
<Spinner
|
|
|
|
visible={this.props.createChannel.isFetching}
|
|
|
|
textContent='Loading...'
|
|
|
|
textStyle={{ color: '#FFF' }}
|
|
|
|
/>
|
2017-09-01 19:42:50 +00:00
|
|
|
</KeyboardView>
|
2017-08-10 16:25:50 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|