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';
|
2018-05-24 20:17:45 +00:00
|
|
|
import { View, Text, Switch, SafeAreaView, ScrollView, Platform } from 'react-native';
|
2018-04-03 16:24:59 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
import RCTextInput from '../containers/TextInput';
|
|
|
|
import Loading from '../containers/Loading';
|
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';
|
2018-04-24 19:34:03 +00:00
|
|
|
import scrollPersistTaps from '../utils/scrollPersistTaps';
|
2018-05-24 20:17:45 +00:00
|
|
|
import Button from '../containers/Button';
|
2018-06-01 17:38:13 +00:00
|
|
|
import I18n from '../i18n';
|
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,
|
2018-04-24 19:34:03 +00:00
|
|
|
users: state.selectedUsers.users
|
2017-09-25 13:15:28 +00:00
|
|
|
}),
|
|
|
|
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-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: '',
|
2018-05-24 20:17:45 +00:00
|
|
|
type: true,
|
|
|
|
readOnly: false,
|
|
|
|
broadcast: false
|
2017-08-10 16:25:50 +00:00
|
|
|
};
|
2017-09-25 13:15:28 +00:00
|
|
|
}
|
|
|
|
|
2018-05-24 20:17:45 +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;
|
|
|
|
}
|
2018-05-24 20:17:45 +00:00
|
|
|
const {
|
|
|
|
channelName, type, readOnly, broadcast
|
|
|
|
} = this.state;
|
2017-09-25 13:15:28 +00:00
|
|
|
let { users } = this.props;
|
|
|
|
|
|
|
|
// transform users object into array of usernames
|
|
|
|
users = users.map(user => user.name);
|
|
|
|
|
|
|
|
// create channel
|
2018-05-24 20:17:45 +00:00
|
|
|
this.props.create({
|
|
|
|
name: channelName, users, type, readOnly, broadcast
|
|
|
|
});
|
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-05-23 13:39:18 +00:00
|
|
|
<Text style={[styles.label_white, styles.label_error]} testID='create-channel-error'>
|
2018-04-10 13:03:54 +00:00
|
|
|
{this.props.createChannel.error.reason}
|
|
|
|
</Text>
|
2017-09-21 17:08:00 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-05-24 20:17:45 +00:00
|
|
|
renderSwitch = ({
|
|
|
|
id, value, label, description, onValueChange, disabled = false
|
|
|
|
}) => (
|
|
|
|
<View style={{ marginBottom: 15 }}>
|
|
|
|
<View style={styles.switchContainer}>
|
2017-09-21 17:08:00 +00:00
|
|
|
<Switch
|
2018-05-24 20:17:45 +00:00
|
|
|
value={value}
|
|
|
|
onValueChange={onValueChange}
|
|
|
|
testID={`create-channel-${ id }`}
|
|
|
|
onTintColor='#2de0a5'
|
|
|
|
tintColor={Platform.OS === 'android' ? '#f5455c' : null}
|
|
|
|
disabled={disabled}
|
2017-09-21 17:08:00 +00:00
|
|
|
/>
|
2018-05-24 20:17:45 +00:00
|
|
|
<Text style={styles.switchLabel}>{label}</Text>
|
2017-09-21 17:08:00 +00:00
|
|
|
</View>
|
2018-05-24 20:17:45 +00:00
|
|
|
<Text style={styles.switchDescription}>{description}</Text>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
|
|
|
|
renderType() {
|
|
|
|
const { type } = this.state;
|
|
|
|
return this.renderSwitch({
|
|
|
|
id: 'type',
|
|
|
|
value: type,
|
2018-06-01 17:38:13 +00:00
|
|
|
label: type ? I18n.t('Private_Channel') : I18n.t('Public_Channel'),
|
|
|
|
description: type ? I18n.t('Just_invited_people_can_access_this_channel') : I18n.t('Everyone_can_access_this_channel'),
|
2018-05-24 20:17:45 +00:00
|
|
|
onValueChange: value => this.setState({ type: value })
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
renderReadOnly() {
|
|
|
|
const { readOnly, broadcast } = this.state;
|
|
|
|
return this.renderSwitch({
|
|
|
|
id: 'readonly',
|
|
|
|
value: readOnly,
|
2018-06-01 17:38:13 +00:00
|
|
|
label: I18n.t('Read_Only_Channel'),
|
|
|
|
description: readOnly ? I18n.t('Only_authorized_users_can_write_new_messages') : I18n.t('All_users_in_the_channel_can_write_new_messages'),
|
2018-05-24 20:17:45 +00:00
|
|
|
onValueChange: value => this.setState({ readOnly: value }),
|
|
|
|
disabled: broadcast
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
renderBroadcast() {
|
|
|
|
const { broadcast, readOnly } = this.state;
|
|
|
|
return this.renderSwitch({
|
|
|
|
id: 'broadcast',
|
|
|
|
value: broadcast,
|
2018-06-01 17:38:13 +00:00
|
|
|
label: I18n.t('Broadcast_Channel'),
|
|
|
|
description: I18n.t('Broadcast_channel_Description'),
|
2018-05-24 20:17:45 +00:00
|
|
|
onValueChange: (value) => {
|
|
|
|
this.setState({
|
|
|
|
broadcast: value,
|
|
|
|
readOnly: value ? true : readOnly
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2017-09-21 17:08:00 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 16:25:50 +00:00
|
|
|
render() {
|
|
|
|
return (
|
2017-11-07 16:28:02 +00:00
|
|
|
<KeyboardView
|
2018-04-24 19:34:03 +00:00
|
|
|
contentContainerStyle={styles.container}
|
|
|
|
keyboardVerticalOffset={128}
|
2017-11-07 16:28:02 +00:00
|
|
|
>
|
2018-04-24 19:34:03 +00:00
|
|
|
<ScrollView {...scrollPersistTaps} contentContainerStyle={styles.containerScrollView}>
|
2018-05-23 13:39:18 +00:00
|
|
|
<SafeAreaView testID='create-channel-view'>
|
2018-04-24 19:34:03 +00:00
|
|
|
<RCTextInput
|
2018-06-01 17:38:13 +00:00
|
|
|
label={I18n.t('Channel_Name')}
|
2018-04-24 19:34:03 +00:00
|
|
|
value={this.state.channelName}
|
|
|
|
onChangeText={channelName => this.setState({ channelName })}
|
2018-06-01 17:38:13 +00:00
|
|
|
placeholder={I18n.t('Type_the_channel_name_here')}
|
2018-04-24 19:34:03 +00:00
|
|
|
returnKeyType='done'
|
|
|
|
autoFocus
|
2018-05-23 13:39:18 +00:00
|
|
|
testID='create-channel-name'
|
2018-04-24 19:34:03 +00:00
|
|
|
/>
|
|
|
|
{this.renderChannelNameError()}
|
2018-05-24 20:17:45 +00:00
|
|
|
{this.renderType()}
|
|
|
|
{this.renderReadOnly()}
|
|
|
|
{this.renderBroadcast()}
|
|
|
|
<View style={styles.alignItemsFlexStart}>
|
|
|
|
<Button
|
2018-06-01 17:38:13 +00:00
|
|
|
title={I18n.t('Create')}
|
2018-05-24 20:17:45 +00:00
|
|
|
type='primary'
|
|
|
|
onPress={this.submit}
|
|
|
|
disabled={this.state.channelName.length === 0 || this.props.createChannel.isFetching}
|
|
|
|
testID='create-channel-submit'
|
|
|
|
/>
|
|
|
|
</View>
|
2018-04-24 19:34:03 +00:00
|
|
|
<Loading visible={this.props.createChannel.isFetching} />
|
|
|
|
</SafeAreaView>
|
|
|
|
</ScrollView>
|
2017-09-01 19:42:50 +00:00
|
|
|
</KeyboardView>
|
2017-08-10 16:25:50 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|