2017-12-08 19:13:21 +00:00
|
|
|
import React from 'react';
|
|
|
|
import { Text, View, Platform, TouchableOpacity, TextInput } from 'react-native';
|
|
|
|
import Icon from 'react-native-vector-icons/Ionicons';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import Modal from 'react-native-modal';
|
2018-05-07 20:41:36 +00:00
|
|
|
import FastImage from 'react-native-fast-image';
|
2017-12-08 19:13:21 +00:00
|
|
|
import { HeaderBackButton } from 'react-navigation';
|
|
|
|
|
|
|
|
import Avatar from '../../../containers/Avatar';
|
2018-04-24 19:34:03 +00:00
|
|
|
import Status from '../../../containers/status';
|
2017-12-08 19:13:21 +00:00
|
|
|
import RocketChat from '../../../lib/rocketchat';
|
|
|
|
import { STATUS_COLORS } from '../../../constants/colors';
|
|
|
|
import { setSearch } from '../../../actions/rooms';
|
|
|
|
import styles from './styles';
|
2018-05-18 17:55:08 +00:00
|
|
|
import log from '../../../utils/log';
|
2017-12-08 19:13:21 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
const title = (offline, connecting, authenticating, logged) => {
|
|
|
|
if (offline) {
|
|
|
|
return 'offline...';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (connecting) {
|
|
|
|
return 'Connecting...';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (authenticating) {
|
|
|
|
return 'Authenticating...';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (logged) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'Not logged...';
|
|
|
|
};
|
|
|
|
|
2017-12-08 19:13:21 +00:00
|
|
|
@connect(state => ({
|
|
|
|
user: state.login.user,
|
2017-12-20 19:20:06 +00:00
|
|
|
connected: state.meteor.connected,
|
2018-04-24 19:34:03 +00:00
|
|
|
baseUrl: state.settings.Site_Url || state.server ? state.server.server : '',
|
|
|
|
|
|
|
|
connecting: state.meteor.connecting,
|
|
|
|
authenticating: state.login.isFetching,
|
|
|
|
offline: !state.meteor.connected,
|
|
|
|
logged: !!state.login.token
|
2017-12-08 19:13:21 +00:00
|
|
|
}), dispatch => ({
|
|
|
|
setSearch: searchText => dispatch(setSearch(searchText))
|
|
|
|
}))
|
2018-03-02 21:31:44 +00:00
|
|
|
|
|
|
|
export default class RoomsListHeaderView extends React.PureComponent {
|
2017-12-08 19:13:21 +00:00
|
|
|
static propTypes = {
|
|
|
|
navigation: PropTypes.object.isRequired,
|
|
|
|
user: PropTypes.object.isRequired,
|
2017-12-20 19:20:06 +00:00
|
|
|
connected: PropTypes.bool,
|
2017-12-08 19:13:21 +00:00
|
|
|
baseUrl: PropTypes.string,
|
|
|
|
setSearch: PropTypes.func
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
isModalVisible: false,
|
|
|
|
searching: false
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
onPressModalButton(status) {
|
2018-05-18 17:55:08 +00:00
|
|
|
try {
|
|
|
|
RocketChat.setUserPresenceDefaultStatus(status);
|
|
|
|
} catch (e) {
|
|
|
|
log('onPressModalButton', e);
|
|
|
|
}
|
2017-12-08 19:13:21 +00:00
|
|
|
this.hideModal();
|
|
|
|
}
|
|
|
|
|
|
|
|
onSearchChangeText(text) {
|
|
|
|
this.props.setSearch(text.trim());
|
|
|
|
}
|
|
|
|
|
|
|
|
onPressCancelSearchButton() {
|
|
|
|
this.setState({ searching: false });
|
|
|
|
this.props.setSearch('');
|
|
|
|
}
|
|
|
|
|
|
|
|
onPressSearchButton() {
|
|
|
|
this.setState({ searching: true });
|
|
|
|
requestAnimationFrame(() => {
|
|
|
|
this.inputSearch.focus();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-11 20:37:33 +00:00
|
|
|
getUserStatus() {
|
2017-12-20 19:20:06 +00:00
|
|
|
return (this.props.connected && this.props.user.status) || 'offline';
|
2017-12-11 20:37:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getUserStatusLabel() {
|
|
|
|
const status = this.getUserStatus();
|
|
|
|
return status.charAt(0).toUpperCase() + status.slice(1);
|
|
|
|
}
|
|
|
|
|
2017-12-08 19:13:21 +00:00
|
|
|
showModal() {
|
|
|
|
this.setState({ isModalVisible: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
hideModal() {
|
|
|
|
this.setState({ isModalVisible: false });
|
|
|
|
}
|
|
|
|
|
|
|
|
createChannel() {
|
2018-05-18 17:55:08 +00:00
|
|
|
this.props.navigation.navigate({
|
|
|
|
key: 'SelectedUsers',
|
|
|
|
routeName: 'SelectedUsers',
|
|
|
|
params: { nextAction: () => this.props.navigation.navigate('CreateChannel') }
|
|
|
|
});
|
2017-12-08 19:13:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
renderLeft() {
|
2017-12-11 20:37:33 +00:00
|
|
|
if (this.state.searching) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-12-08 19:13:21 +00:00
|
|
|
return (
|
2018-05-23 13:39:18 +00:00
|
|
|
<View
|
|
|
|
style={styles.left}
|
|
|
|
accessible
|
|
|
|
accessibilityLabel={`Connected to ${ this.props.baseUrl }. Tap to view servers list.`}
|
|
|
|
accessibilityTraits='button'
|
|
|
|
testID='rooms-list-view-sidebar'
|
|
|
|
>
|
2017-12-08 19:13:21 +00:00
|
|
|
<TouchableOpacity
|
|
|
|
style={styles.headerButton}
|
2018-05-21 18:39:26 +00:00
|
|
|
onPress={() => this.props.navigation.openDrawer()}
|
2017-12-08 19:13:21 +00:00
|
|
|
>
|
2018-05-07 20:41:36 +00:00
|
|
|
<FastImage
|
2017-12-08 19:13:21 +00:00
|
|
|
style={styles.serverImage}
|
|
|
|
source={{ uri: encodeURI(`${ this.props.baseUrl }/assets/favicon_32.png`) }}
|
|
|
|
/>
|
|
|
|
</TouchableOpacity>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
renderCenter() {
|
|
|
|
const {
|
|
|
|
offline, connecting, authenticating, logged, user
|
|
|
|
} = this.props;
|
|
|
|
|
2017-12-11 20:37:33 +00:00
|
|
|
if (this.state.searching) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
if (!user.username) {
|
2017-12-08 19:13:21 +00:00
|
|
|
return null;
|
|
|
|
}
|
2017-12-11 20:37:33 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
const t = title(offline, connecting, authenticating, logged);
|
2017-12-11 20:37:33 +00:00
|
|
|
|
2018-05-23 13:39:18 +00:00
|
|
|
const accessibilityLabel = `${ user.username }, ${ this.getUserStatusLabel() }, tap to change status`;
|
2017-12-08 19:13:21 +00:00
|
|
|
return (
|
2018-05-23 13:39:18 +00:00
|
|
|
<TouchableOpacity
|
|
|
|
style={styles.titleContainer}
|
|
|
|
onPress={() => this.showModal()}
|
|
|
|
accessibilityLabel={accessibilityLabel}
|
|
|
|
accessibilityTraits='header'
|
|
|
|
testID='rooms-list-view-user'
|
|
|
|
>
|
2017-12-08 19:13:21 +00:00
|
|
|
<Avatar
|
2018-04-24 19:34:03 +00:00
|
|
|
text={user.username}
|
2017-12-08 19:13:21 +00:00
|
|
|
size={24}
|
2018-04-24 19:34:03 +00:00
|
|
|
>
|
|
|
|
<Status style={[styles.status, styles.user_status]} id={user.id} />
|
|
|
|
</Avatar>
|
|
|
|
<View style={styles.rows}>
|
|
|
|
<Text accessible={false} style={styles.title} ellipsizeMode='tail' numberOfLines={1} allowFontScaling={false}>{this.props.user.username}</Text>
|
|
|
|
{ t && <Text accessible={false} style={styles.status_text} ellipsizeMode='tail' numberOfLines={1} allowFontScaling={false}>{t}</Text>}
|
|
|
|
</View>
|
2017-12-08 19:13:21 +00:00
|
|
|
</TouchableOpacity>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderRight() {
|
2017-12-11 20:37:33 +00:00
|
|
|
if (this.state.searching) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-12-08 19:13:21 +00:00
|
|
|
return (
|
|
|
|
<View style={styles.right}>
|
|
|
|
{Platform.OS === 'android' ?
|
|
|
|
<TouchableOpacity
|
|
|
|
style={styles.headerButton}
|
|
|
|
onPress={() => this.onPressSearchButton()}
|
2017-12-11 20:37:33 +00:00
|
|
|
accessibilityLabel='Search'
|
|
|
|
accessibilityTraits='button'
|
2017-12-08 19:13:21 +00:00
|
|
|
>
|
|
|
|
<Icon
|
|
|
|
name='md-search'
|
|
|
|
color='#292E35'
|
|
|
|
size={24}
|
|
|
|
backgroundColor='transparent'
|
|
|
|
/>
|
|
|
|
</TouchableOpacity> : null}
|
|
|
|
{Platform.OS === 'ios' ?
|
|
|
|
<TouchableOpacity
|
|
|
|
style={styles.headerButton}
|
|
|
|
onPress={() => this.createChannel()}
|
2017-12-11 20:37:33 +00:00
|
|
|
accessibilityLabel='Create channel'
|
|
|
|
accessibilityTraits='button'
|
2018-05-23 13:39:18 +00:00
|
|
|
testID='rooms-list-view-create-channel'
|
2017-12-08 19:13:21 +00:00
|
|
|
>
|
|
|
|
<Icon
|
|
|
|
name='ios-add'
|
|
|
|
color='#292E35'
|
|
|
|
size={24}
|
|
|
|
backgroundColor='transparent'
|
|
|
|
/>
|
2018-04-10 13:03:54 +00:00
|
|
|
</TouchableOpacity> : null
|
|
|
|
}
|
2017-12-08 19:13:21 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderModalButton = (status, text) => {
|
2018-04-24 19:34:03 +00:00
|
|
|
const statusStyle = [styles.status, { marginRight: 10, backgroundColor: STATUS_COLORS[status] }];
|
2017-12-08 19:13:21 +00:00
|
|
|
const textStyle = { flex: 1, fontWeight: this.props.user.status === status ? 'bold' : 'normal' };
|
|
|
|
return (
|
|
|
|
<TouchableOpacity
|
|
|
|
style={styles.modalButton}
|
|
|
|
onPress={() => this.onPressModalButton(status)}
|
2018-05-23 13:39:18 +00:00
|
|
|
testID={`rooms-list-view-user-presence-${ status }`}
|
2017-12-08 19:13:21 +00:00
|
|
|
>
|
|
|
|
<View style={statusStyle} />
|
|
|
|
<Text style={textStyle}>
|
|
|
|
{text || status.charAt(0).toUpperCase() + status.slice(1)}
|
|
|
|
</Text>
|
|
|
|
</TouchableOpacity>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
renderSearch() {
|
|
|
|
if (!this.state.searching) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<View style={styles.header}>
|
|
|
|
<View style={styles.left}>
|
|
|
|
<HeaderBackButton onPress={() => this.onPressCancelSearchButton()} />
|
|
|
|
</View>
|
|
|
|
<TextInput
|
|
|
|
ref={inputSearch => this.inputSearch = inputSearch}
|
|
|
|
underlineColorAndroid='transparent'
|
|
|
|
style={styles.inputSearch}
|
|
|
|
onChangeText={text => this.onSearchChangeText(text)}
|
|
|
|
returnKeyType='search'
|
|
|
|
placeholder='Search'
|
|
|
|
clearButtonMode='while-editing'
|
|
|
|
blurOnSubmit
|
2018-05-18 17:55:08 +00:00
|
|
|
autoCorrect={false}
|
|
|
|
autoCapitalize='none'
|
2017-12-08 19:13:21 +00:00
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2018-05-23 13:39:18 +00:00
|
|
|
<View style={styles.header} testID='rooms-list-view-header'>
|
2017-12-11 20:37:33 +00:00
|
|
|
{this.renderLeft()}
|
2018-04-24 19:34:03 +00:00
|
|
|
{this.renderCenter()}
|
2017-12-11 20:37:33 +00:00
|
|
|
{this.renderRight()}
|
2017-12-08 19:13:21 +00:00
|
|
|
{this.renderSearch()}
|
|
|
|
<Modal
|
|
|
|
isVisible={this.state.isModalVisible}
|
|
|
|
supportedOrientations={['portrait', 'landscape']}
|
|
|
|
style={{ alignItems: 'center' }}
|
|
|
|
onModalHide={() => this.hideModal()}
|
|
|
|
onBackdropPress={() => this.hideModal()}
|
2018-05-23 13:39:18 +00:00
|
|
|
testID='rooms-list-view-user-presence-modal'
|
2017-12-08 19:13:21 +00:00
|
|
|
>
|
|
|
|
<View style={styles.modal}>
|
|
|
|
{this.renderModalButton('online')}
|
|
|
|
{this.renderModalButton('busy')}
|
|
|
|
{this.renderModalButton('away')}
|
|
|
|
{this.renderModalButton('offline', 'Invisible')}
|
|
|
|
</View>
|
|
|
|
</Modal>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|