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';
import { CachedImage } from 'react-native-img-cache';
import { HeaderBackButton } from 'react-navigation';
import Avatar from '../../../containers/Avatar';
import RocketChat from '../../../lib/rocketchat';
import { STATUS_COLORS } from '../../../constants/colors';
import { setSearch } from '../../../actions/rooms';
import styles from './styles';
@connect(state => ({
user: state.login.user,
connected: state.meteor.connected,
baseUrl: state.settings.Site_Url || state.server ? state.server.server : ''
}), dispatch => ({
setSearch: searchText => dispatch(setSearch(searchText))
}))
export default class RoomsListHeaderView extends React.PureComponent {
static propTypes = {
navigation: PropTypes.object.isRequired,
user: PropTypes.object.isRequired,
connected: PropTypes.bool,
baseUrl: PropTypes.string,
setSearch: PropTypes.func
}
constructor(props) {
super(props);
this.state = {
isModalVisible: false,
searching: false
};
}
onPressModalButton(status) {
RocketChat.setUserPresenceDefaultStatus(status);
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();
});
}
getUserStatus() {
return (this.props.connected && this.props.user.status) || 'offline';
}
getUserStatusLabel() {
const status = this.getUserStatus();
return status.charAt(0).toUpperCase() + status.slice(1);
}
showModal() {
this.setState({ isModalVisible: true });
}
hideModal() {
this.setState({ isModalVisible: false });
}
createChannel() {
this.props.navigation.navigate('SelectUsers');
}
renderLeft() {
if (this.state.searching) {
return null;
}
return (
this.props.navigation.navigate('DrawerOpen')}
>
);
}
renderTitle() {
if (this.state.searching) {
return null;
}
if (!this.props.user.username) {
return null;
}
const accessibilityLabel = `${ this.props.user.username }, ${ this.getUserStatusLabel() }, double tap to change status`;
return (
this.showModal()} accessibilityLabel={accessibilityLabel} accessibilityTraits='header'>
{this.props.user.username}
);
}
renderRight() {
if (this.state.searching) {
return null;
}
return (
{Platform.OS === 'android' ?
this.onPressSearchButton()}
accessibilityLabel='Search'
accessibilityTraits='button'
>
: null}
{Platform.OS === 'ios' ?
this.createChannel()}
accessibilityLabel='Create channel'
accessibilityTraits='button'
>
: null}
);
}
renderModalButton = (status, text) => {
const statusStyle = [styles.status, { backgroundColor: STATUS_COLORS[status] }];
const textStyle = { flex: 1, fontWeight: this.props.user.status === status ? 'bold' : 'normal' };
return (
this.onPressModalButton(status)}
>
{text || status.charAt(0).toUpperCase() + status.slice(1)}
);
};
renderSearch() {
if (!this.state.searching) {
return null;
}
return (
this.onPressCancelSearchButton()} />
this.inputSearch = inputSearch}
underlineColorAndroid='transparent'
style={styles.inputSearch}
onChangeText={text => this.onSearchChangeText(text)}
returnKeyType='search'
placeholder='Search'
clearButtonMode='while-editing'
blurOnSubmit
/>
);
}
render() {
return (
{this.renderLeft()}
{this.renderTitle()}
{this.renderRight()}
{this.renderSearch()}
this.hideModal()}
onBackdropPress={() => this.hideModal()}
>
{this.renderModalButton('online')}
{this.renderModalButton('busy')}
{this.renderModalButton('away')}
{this.renderModalButton('offline', 'Invisible')}
);
}
}