2018-08-31 18:13:30 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-09-25 19:28:42 +00:00
|
|
|
import {
|
2018-10-23 21:39:48 +00:00
|
|
|
View, StyleSheet, FlatList, Text, Platform, Image, Dimensions
|
2018-09-25 19:28:42 +00:00
|
|
|
} from 'react-native';
|
2018-09-26 13:56:36 +00:00
|
|
|
import { connect, Provider } from 'react-redux';
|
|
|
|
import { Navigation } from 'react-native-navigation';
|
2018-10-23 21:39:48 +00:00
|
|
|
import SafeAreaView from 'react-native-safe-area-view';
|
2018-11-14 21:42:03 +00:00
|
|
|
import { gestureHandlerRootHOC } from 'react-native-gesture-handler';
|
2018-12-21 10:55:35 +00:00
|
|
|
import equal from 'deep-equal';
|
2018-08-31 18:13:30 +00:00
|
|
|
|
|
|
|
import database from '../lib/realm';
|
|
|
|
import RocketChat from '../lib/rocketchat';
|
|
|
|
import UserItem from '../presentation/UserItem';
|
|
|
|
import debounce from '../utils/debounce';
|
|
|
|
import LoggedView from './View';
|
|
|
|
import sharedStyles from './Styles';
|
|
|
|
import I18n from '../i18n';
|
|
|
|
import Touch from '../utils/touch';
|
|
|
|
import SearchBox from '../containers/SearchBox';
|
2018-09-26 13:56:36 +00:00
|
|
|
import store from '../lib/createStore';
|
2018-11-14 21:42:03 +00:00
|
|
|
import { DEFAULT_HEADER } from '../constants/headerOptions';
|
2018-08-31 18:13:30 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
safeAreaView: {
|
|
|
|
flex: 1,
|
|
|
|
backgroundColor: Platform.OS === 'ios' ? '#F7F8FA' : '#E1E5E8'
|
|
|
|
},
|
|
|
|
separator: {
|
|
|
|
marginLeft: 60
|
|
|
|
},
|
|
|
|
createChannelButton: {
|
|
|
|
marginVertical: 25
|
|
|
|
},
|
|
|
|
createChannelContainer: {
|
|
|
|
height: 47,
|
|
|
|
backgroundColor: '#fff',
|
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center'
|
|
|
|
},
|
|
|
|
createChannelIcon: {
|
|
|
|
width: 24,
|
|
|
|
height: 24,
|
|
|
|
marginHorizontal: 18
|
|
|
|
},
|
|
|
|
createChannelText: {
|
|
|
|
color: '#1D74F5',
|
|
|
|
fontSize: 18
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-09-26 13:56:36 +00:00
|
|
|
let SelectedUsersView = null;
|
|
|
|
|
2018-09-11 16:32:52 +00:00
|
|
|
@connect(state => ({
|
|
|
|
baseUrl: state.settings.Site_Url || state.server ? state.server.server : ''
|
|
|
|
}))
|
2018-08-31 18:13:30 +00:00
|
|
|
/** @extends React.Component */
|
2018-09-11 16:32:52 +00:00
|
|
|
export default class NewMessageView extends LoggedView {
|
2018-10-23 21:39:48 +00:00
|
|
|
static options() {
|
|
|
|
return {
|
2018-11-14 21:42:03 +00:00
|
|
|
...DEFAULT_HEADER,
|
2018-10-23 21:39:48 +00:00
|
|
|
topBar: {
|
2018-11-14 21:42:03 +00:00
|
|
|
...DEFAULT_HEADER.topBar,
|
2018-10-23 21:39:48 +00:00
|
|
|
leftButtons: [{
|
|
|
|
id: 'cancel',
|
|
|
|
icon: Platform.OS === 'android' ? { uri: 'back', scale: Dimensions.get('window').scale } : undefined,
|
|
|
|
text: Platform.OS === 'ios' ? I18n.t('Cancel') : undefined
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
};
|
2018-08-31 18:13:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static propTypes = {
|
2018-10-23 21:39:48 +00:00
|
|
|
componentId: PropTypes.string,
|
2018-09-11 16:32:52 +00:00
|
|
|
baseUrl: PropTypes.string,
|
2018-08-31 18:13:30 +00:00
|
|
|
onPressItem: PropTypes.func.isRequired
|
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super('NewMessageView', props);
|
|
|
|
this.data = database.objects('subscriptions').filtered('t = $0', 'd').sorted('roomUpdatedAt', true);
|
|
|
|
this.state = {
|
|
|
|
search: []
|
|
|
|
};
|
|
|
|
this.data.addListener(this.updateState);
|
2018-10-23 21:39:48 +00:00
|
|
|
Navigation.events().bindComponent(this);
|
2018-08-31 18:13:30 +00:00
|
|
|
}
|
|
|
|
|
2018-12-21 10:55:35 +00:00
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
|
|
|
const { search } = this.state;
|
|
|
|
if (!equal(nextState.search, search)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-31 18:13:30 +00:00
|
|
|
componentWillUnmount() {
|
|
|
|
this.updateState.stop();
|
|
|
|
this.data.removeAllListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
onSearchChangeText(text) {
|
|
|
|
this.search(text);
|
|
|
|
}
|
|
|
|
|
2018-12-21 10:55:35 +00:00
|
|
|
onPressItem = async(item) => {
|
2018-10-23 21:39:48 +00:00
|
|
|
const { onPressItem } = this.props;
|
2018-12-21 10:55:35 +00:00
|
|
|
await this.dismiss();
|
|
|
|
onPressItem(item);
|
2018-08-31 18:13:30 +00:00
|
|
|
}
|
|
|
|
|
2018-10-23 21:39:48 +00:00
|
|
|
navigationButtonPressed = ({ buttonId }) => {
|
|
|
|
if (buttonId === 'cancel') {
|
|
|
|
this.dismiss();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dismiss = () => {
|
|
|
|
const { componentId } = this.props;
|
2018-12-21 10:55:35 +00:00
|
|
|
return Navigation.dismissModal(componentId);
|
2018-10-23 21:39:48 +00:00
|
|
|
}
|
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
// eslint-disable-next-line react/sort-comp
|
2018-08-31 18:13:30 +00:00
|
|
|
updateState = debounce(() => {
|
|
|
|
this.forceUpdate();
|
|
|
|
}, 1000);
|
|
|
|
|
|
|
|
search = async(text) => {
|
|
|
|
const result = await RocketChat.search({ text, filterRooms: false });
|
|
|
|
this.setState({
|
|
|
|
search: result
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
createChannel = () => {
|
2018-09-26 13:56:36 +00:00
|
|
|
if (SelectedUsersView == null) {
|
|
|
|
SelectedUsersView = require('./SelectedUsersView').default;
|
2018-11-14 21:42:03 +00:00
|
|
|
Navigation.registerComponentWithRedux('SelectedUsersView', () => gestureHandlerRootHOC(SelectedUsersView), Provider, store);
|
2018-09-26 13:56:36 +00:00
|
|
|
}
|
|
|
|
|
2018-10-23 21:39:48 +00:00
|
|
|
const { componentId } = this.props;
|
|
|
|
Navigation.push(componentId, {
|
|
|
|
component: {
|
|
|
|
name: 'SelectedUsersView',
|
|
|
|
passProps: {
|
|
|
|
nextAction: 'CREATE_CHANNEL'
|
|
|
|
},
|
|
|
|
options: {
|
|
|
|
topBar: {
|
|
|
|
title: {
|
|
|
|
text: I18n.t('Select_Users')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-31 18:13:30 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
renderHeader = () => (
|
|
|
|
<View>
|
|
|
|
<SearchBox onChangeText={text => this.onSearchChangeText(text)} testID='new-message-view-search' />
|
|
|
|
<Touch onPress={this.createChannel} style={styles.createChannelButton} testID='new-message-view-create-channel'>
|
|
|
|
<View style={[sharedStyles.separatorVertical, styles.createChannelContainer]}>
|
|
|
|
<Image style={styles.createChannelIcon} source={{ uri: 'plus' }} />
|
|
|
|
<Text style={styles.createChannelText}>{I18n.t('Create_Channel')}</Text>
|
|
|
|
</View>
|
|
|
|
</Touch>
|
|
|
|
</View>
|
|
|
|
)
|
|
|
|
|
|
|
|
renderSeparator = () => <View style={[sharedStyles.separator, styles.separator]} />;
|
|
|
|
|
|
|
|
renderItem = ({ item, index }) => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { search } = this.state;
|
|
|
|
const { baseUrl } = this.props;
|
|
|
|
|
2018-08-31 18:13:30 +00:00
|
|
|
let style = {};
|
|
|
|
if (index === 0) {
|
|
|
|
style = { ...sharedStyles.separatorTop };
|
|
|
|
}
|
2018-09-25 19:28:42 +00:00
|
|
|
if (search.length > 0 && index === search.length - 1) {
|
2018-08-31 18:13:30 +00:00
|
|
|
style = { ...style, ...sharedStyles.separatorBottom };
|
|
|
|
}
|
2018-09-25 19:28:42 +00:00
|
|
|
if (search.length === 0 && index === this.data.length - 1) {
|
2018-08-31 18:13:30 +00:00
|
|
|
style = { ...style, ...sharedStyles.separatorBottom };
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<UserItem
|
|
|
|
name={item.search ? item.name : item.fname}
|
|
|
|
username={item.search ? item.username : item.name}
|
|
|
|
onPress={() => this.onPressItem(item)}
|
2018-09-25 19:28:42 +00:00
|
|
|
baseUrl={baseUrl}
|
2018-08-31 18:13:30 +00:00
|
|
|
testID={`new-message-view-item-${ item.name }`}
|
|
|
|
style={style}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
renderList = () => {
|
|
|
|
const { search } = this.state;
|
|
|
|
return (
|
|
|
|
<FlatList
|
|
|
|
data={search.length > 0 ? search : this.data}
|
|
|
|
extraData={this.state}
|
|
|
|
keyExtractor={item => item._id}
|
|
|
|
ListHeaderComponent={this.renderHeader}
|
|
|
|
renderItem={this.renderItem}
|
|
|
|
ItemSeparatorComponent={this.renderSeparator}
|
|
|
|
keyboardShouldPersistTaps='always'
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2018-08-31 18:13:30 +00:00
|
|
|
|
|
|
|
render = () => (
|
2018-10-23 21:39:48 +00:00
|
|
|
<SafeAreaView style={styles.safeAreaView} testID='new-message-view' forceInset={{ bottom: 'never' }}>
|
2018-08-31 18:13:30 +00:00
|
|
|
{this.renderList()}
|
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
}
|