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-09-25 19:28:42 +00:00
|
|
|
import {
|
|
|
|
View, Text, Switch, SafeAreaView, ScrollView, TextInput, StyleSheet, FlatList, Platform
|
|
|
|
} from 'react-native';
|
2018-04-03 16:24:59 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
import Loading from '../containers/Loading';
|
2018-04-03 16:24:59 +00:00
|
|
|
import LoggedView from './View';
|
2018-09-25 19:28:42 +00:00
|
|
|
import { createChannelRequest as createChannelRequestAction } from '../actions/createChannel';
|
|
|
|
import { removeUser as removeUserAction } from '../actions/selectedUsers';
|
2018-08-31 18:13:30 +00:00
|
|
|
import sharedStyles 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-06-01 17:38:13 +00:00
|
|
|
import I18n from '../i18n';
|
2018-08-31 18:13:30 +00:00
|
|
|
import UserItem from '../presentation/UserItem';
|
|
|
|
import { showErrorAlert } from '../utils/info';
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
container: {
|
2018-09-11 16:32:52 +00:00
|
|
|
backgroundColor: '#f7f8fa',
|
|
|
|
flex: 1
|
2018-08-31 18:13:30 +00:00
|
|
|
},
|
|
|
|
list: {
|
|
|
|
width: '100%',
|
|
|
|
backgroundColor: '#FFFFFF'
|
|
|
|
},
|
|
|
|
separator: {
|
|
|
|
marginLeft: 60
|
|
|
|
},
|
|
|
|
formSeparator: {
|
|
|
|
marginLeft: 15
|
|
|
|
},
|
|
|
|
input: {
|
|
|
|
height: 54,
|
|
|
|
paddingHorizontal: 18,
|
|
|
|
color: '#9EA2A8',
|
|
|
|
backgroundColor: '#fff',
|
|
|
|
fontSize: 18
|
|
|
|
},
|
|
|
|
swithContainer: {
|
|
|
|
height: 54,
|
|
|
|
backgroundColor: '#fff',
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'space-between',
|
|
|
|
flexDirection: 'row',
|
|
|
|
paddingHorizontal: 18
|
|
|
|
},
|
|
|
|
label: {
|
|
|
|
color: '#0C0D0F',
|
|
|
|
fontSize: 18,
|
|
|
|
fontWeight: '500'
|
|
|
|
},
|
|
|
|
invitedHeader: {
|
|
|
|
marginTop: 18,
|
|
|
|
marginHorizontal: 15,
|
|
|
|
flexDirection: 'row',
|
|
|
|
justifyContent: 'space-between',
|
|
|
|
alignItems: 'center'
|
|
|
|
},
|
|
|
|
invitedTitle: {
|
|
|
|
color: '#2F343D',
|
|
|
|
fontSize: 22,
|
|
|
|
fontWeight: 'bold',
|
|
|
|
lineHeight: 41
|
|
|
|
},
|
|
|
|
invitedCount: {
|
|
|
|
color: '#9EA2A8',
|
|
|
|
fontSize: 15
|
|
|
|
}
|
|
|
|
});
|
2017-08-10 16:25:50 +00:00
|
|
|
|
2018-07-10 13:40:32 +00:00
|
|
|
@connect(state => ({
|
2018-09-11 16:32:52 +00:00
|
|
|
baseUrl: state.settings.Site_Url || state.server ? state.server.server : '',
|
2018-07-10 13:40:32 +00:00
|
|
|
createChannel: state.createChannel,
|
|
|
|
users: state.selectedUsers.users
|
|
|
|
}), dispatch => ({
|
2018-09-25 19:28:42 +00:00
|
|
|
create: data => dispatch(createChannelRequestAction(data)),
|
|
|
|
removeUser: user => dispatch(removeUserAction(user))
|
2018-07-10 13:40:32 +00:00
|
|
|
}))
|
|
|
|
/** @extends React.Component */
|
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-07-10 13:40:32 +00:00
|
|
|
navigator: PropTypes.object,
|
2018-09-11 16:32:52 +00:00
|
|
|
baseUrl: PropTypes.string,
|
2018-04-10 13:03:54 +00:00
|
|
|
create: PropTypes.func.isRequired,
|
2018-08-31 18:13:30 +00:00
|
|
|
removeUser: PropTypes.func.isRequired,
|
2018-04-10 13:03:54 +00:00
|
|
|
createChannel: PropTypes.object.isRequired,
|
2018-07-10 13:40:32 +00:00
|
|
|
users: PropTypes.array.isRequired
|
2017-09-25 13:15:28 +00:00
|
|
|
};
|
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
|
|
|
};
|
2018-08-31 18:13:30 +00:00
|
|
|
props.navigator.setOnNavigatorEvent(this.onNavigatorEvent.bind(this));
|
2017-09-25 13:15:28 +00:00
|
|
|
}
|
|
|
|
|
2018-08-01 19:35:06 +00:00
|
|
|
componentDidMount() {
|
|
|
|
setTimeout(() => {
|
|
|
|
this.channelNameRef.focus();
|
|
|
|
}, 600);
|
|
|
|
}
|
|
|
|
|
2018-08-31 18:13:30 +00:00
|
|
|
componentDidUpdate(prevProps) {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { createChannel } = this.props;
|
|
|
|
|
|
|
|
if (createChannel.error && prevProps.createChannel.error !== createChannel.error) {
|
2018-08-31 18:13:30 +00:00
|
|
|
setTimeout(() => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const msg = createChannel.error.reason || I18n.t('There_was_an_error_while_action', { action: I18n.t('creating_channel') });
|
2018-08-31 18:13:30 +00:00
|
|
|
showErrorAlert(msg);
|
|
|
|
}, 300);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onChangeText = (channelName) => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { navigator } = this.props;
|
|
|
|
|
2018-08-31 18:13:30 +00:00
|
|
|
const rightButtons = [];
|
|
|
|
if (channelName.trim().length > 0) {
|
|
|
|
rightButtons.push({
|
|
|
|
id: 'create',
|
|
|
|
title: 'Create',
|
|
|
|
testID: 'create-channel-submit'
|
|
|
|
});
|
|
|
|
}
|
2018-09-25 19:28:42 +00:00
|
|
|
navigator.setButtons({ rightButtons });
|
2018-08-31 18:13:30 +00:00
|
|
|
this.setState({ channelName });
|
|
|
|
}
|
|
|
|
|
|
|
|
async onNavigatorEvent(event) {
|
|
|
|
if (event.type === 'NavBarButtonPress') {
|
|
|
|
if (event.id === 'create') {
|
|
|
|
this.submit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-24 20:17:45 +00:00
|
|
|
submit = () => {
|
|
|
|
const {
|
|
|
|
channelName, type, readOnly, broadcast
|
|
|
|
} = this.state;
|
2018-09-25 19:28:42 +00:00
|
|
|
const { users: usersProps, createChannel, create } = this.props;
|
|
|
|
|
|
|
|
if (!channelName.trim() || createChannel.isFetching) {
|
|
|
|
return;
|
|
|
|
}
|
2017-09-25 13:15:28 +00:00
|
|
|
|
|
|
|
// transform users object into array of usernames
|
2018-09-25 19:28:42 +00:00
|
|
|
const users = usersProps.map(user => user.name);
|
2017-09-25 13:15:28 +00:00
|
|
|
|
|
|
|
// create channel
|
2018-09-25 19:28:42 +00:00
|
|
|
create({
|
2018-05-24 20:17:45 +00:00
|
|
|
name: channelName, users, type, readOnly, broadcast
|
|
|
|
});
|
2017-08-10 16:25:50 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 18:13:30 +00:00
|
|
|
removeUser = (user) => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { users, removeUser } = this.props;
|
|
|
|
if (users.length === 1) {
|
2018-08-31 18:13:30 +00:00
|
|
|
return;
|
2017-09-21 17:08:00 +00:00
|
|
|
}
|
2018-09-25 19:28:42 +00:00
|
|
|
removeUser(user);
|
2017-09-21 17:08:00 +00:00
|
|
|
}
|
|
|
|
|
2018-05-24 20:17:45 +00:00
|
|
|
renderSwitch = ({
|
2018-08-31 18:13:30 +00:00
|
|
|
id, value, label, onValueChange, disabled = false
|
2018-05-24 20:17:45 +00:00
|
|
|
}) => (
|
2018-08-31 18:13:30 +00:00
|
|
|
<View style={styles.swithContainer}>
|
|
|
|
<Text style={styles.label}>{I18n.t(label)}</Text>
|
|
|
|
<Switch
|
|
|
|
value={value}
|
|
|
|
onValueChange={onValueChange}
|
|
|
|
testID={`create-channel-${ id }`}
|
|
|
|
onTintColor='#2de0a5'
|
|
|
|
tintColor={Platform.OS === 'android' ? '#f5455c' : null}
|
|
|
|
disabled={disabled}
|
|
|
|
/>
|
2018-05-24 20:17:45 +00:00
|
|
|
</View>
|
2018-08-31 18:13:30 +00:00
|
|
|
)
|
2018-05-24 20:17:45 +00:00
|
|
|
|
|
|
|
renderType() {
|
|
|
|
const { type } = this.state;
|
|
|
|
return this.renderSwitch({
|
|
|
|
id: 'type',
|
|
|
|
value: type,
|
2018-08-31 18:13:30 +00:00
|
|
|
label: 'Private_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-08-31 18:13:30 +00:00
|
|
|
label: 'Read_Only_Channel',
|
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-08-31 18:13:30 +00:00
|
|
|
label: 'Broadcast_Channel',
|
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
|
|
|
}
|
|
|
|
|
2018-08-31 18:13:30 +00:00
|
|
|
renderSeparator = () => <View style={[sharedStyles.separator, styles.separator]} />
|
|
|
|
|
|
|
|
renderFormSeparator = () => <View style={[sharedStyles.separator, styles.formSeparator]} />
|
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
renderItem = ({ item }) => {
|
|
|
|
const { baseUrl } = this.props;
|
2018-08-31 18:13:30 +00:00
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
return (
|
|
|
|
<UserItem
|
|
|
|
name={item.fname}
|
|
|
|
username={item.name}
|
|
|
|
onPress={() => this.removeUser(item)}
|
|
|
|
testID={`create-channel-view-item-${ item.name }`}
|
|
|
|
baseUrl={baseUrl}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderInvitedList = () => {
|
|
|
|
const { users } = this.props;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<FlatList
|
|
|
|
data={users}
|
|
|
|
extraData={users}
|
|
|
|
keyExtractor={item => item._id}
|
|
|
|
style={[styles.list, sharedStyles.separatorVertical]}
|
|
|
|
renderItem={this.renderItem}
|
|
|
|
ItemSeparatorComponent={this.renderSeparator}
|
|
|
|
enableEmptySections
|
|
|
|
keyboardShouldPersistTaps='always'
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2018-08-31 18:13:30 +00:00
|
|
|
|
2017-08-10 16:25:50 +00:00
|
|
|
render() {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { channelName } = this.state;
|
|
|
|
const { users, createChannel } = this.props;
|
|
|
|
const userCount = users.length;
|
|
|
|
|
2017-08-10 16:25:50 +00:00
|
|
|
return (
|
2017-11-07 16:28:02 +00:00
|
|
|
<KeyboardView
|
2018-08-31 18:13:30 +00:00
|
|
|
contentContainerStyle={[sharedStyles.container, styles.container]}
|
2018-04-24 19:34:03 +00:00
|
|
|
keyboardVerticalOffset={128}
|
2017-11-07 16:28:02 +00:00
|
|
|
>
|
2018-09-11 16:32:52 +00:00
|
|
|
<SafeAreaView testID='create-channel-view' style={styles.container}>
|
2018-08-31 18:13:30 +00:00
|
|
|
<ScrollView {...scrollPersistTaps}>
|
|
|
|
<View style={sharedStyles.separatorVertical}>
|
|
|
|
<TextInput
|
|
|
|
ref={ref => this.channelNameRef = ref}
|
|
|
|
style={styles.input}
|
|
|
|
label={I18n.t('Channel_Name')}
|
2018-09-25 19:28:42 +00:00
|
|
|
value={channelName}
|
2018-08-31 18:13:30 +00:00
|
|
|
onChangeText={this.onChangeText}
|
|
|
|
placeholder={I18n.t('Channel_Name')}
|
|
|
|
returnKeyType='done'
|
|
|
|
testID='create-channel-name'
|
|
|
|
autoCorrect={false}
|
|
|
|
autoCapitalize='none'
|
|
|
|
underlineColorAndroid='transparent'
|
2018-05-24 20:17:45 +00:00
|
|
|
/>
|
2018-08-31 18:13:30 +00:00
|
|
|
{this.renderFormSeparator()}
|
|
|
|
{this.renderType()}
|
|
|
|
{this.renderFormSeparator()}
|
|
|
|
{this.renderReadOnly()}
|
|
|
|
{this.renderFormSeparator()}
|
|
|
|
{this.renderBroadcast()}
|
|
|
|
</View>
|
|
|
|
<View style={styles.invitedHeader}>
|
|
|
|
<Text style={styles.invitedTitle}>{I18n.t('Invite')}</Text>
|
|
|
|
<Text style={styles.invitedCount}>{userCount === 1 ? I18n.t('1_user') : I18n.t('N_users', { n: userCount })}</Text>
|
2018-05-24 20:17:45 +00:00
|
|
|
</View>
|
2018-08-31 18:13:30 +00:00
|
|
|
{this.renderInvitedList()}
|
2018-09-25 19:28:42 +00:00
|
|
|
<Loading visible={createChannel.isFetching} />
|
2018-08-31 18:13:30 +00:00
|
|
|
</ScrollView>
|
|
|
|
</SafeAreaView>
|
2017-09-01 19:42:50 +00:00
|
|
|
</KeyboardView>
|
2017-08-10 16:25:50 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|