2017-08-10 16:25:50 +00:00
|
|
|
import ActionButton from 'react-native-action-button';
|
2017-08-03 18:23:43 +00:00
|
|
|
import React from 'react';
|
2017-08-05 18:16:32 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2017-09-01 19:42:50 +00:00
|
|
|
import Icon from 'react-native-vector-icons/Ionicons';
|
2018-04-24 19:34:03 +00:00
|
|
|
import { Platform, View, TextInput, FlatList, LayoutAnimation } from 'react-native';
|
2017-08-13 01:35:09 +00:00
|
|
|
import { connect } from 'react-redux';
|
2018-05-18 17:55:08 +00:00
|
|
|
|
2017-12-27 15:22:06 +00:00
|
|
|
import database from '../../lib/realm';
|
2017-12-08 19:13:21 +00:00
|
|
|
import RocketChat from '../../lib/rocketchat';
|
|
|
|
import RoomItem from '../../presentation/RoomItem';
|
|
|
|
import { goRoom } from '../../containers/routes/NavigationService';
|
|
|
|
import Header from '../../containers/Header';
|
|
|
|
import RoomsListHeader from './Header';
|
|
|
|
import styles from './styles';
|
2018-04-24 19:34:03 +00:00
|
|
|
import throttle from '../../utils/throttle';
|
2018-04-26 17:33:43 +00:00
|
|
|
import LoggedView from '../View';
|
2018-05-18 17:55:08 +00:00
|
|
|
import log from '../../utils/log';
|
2017-08-03 18:23:43 +00:00
|
|
|
|
2017-08-13 01:35:09 +00:00
|
|
|
@connect(state => ({
|
2018-02-16 18:34:25 +00:00
|
|
|
user: state.login.user,
|
2017-09-01 19:42:50 +00:00
|
|
|
server: state.server.server,
|
2017-08-18 21:30:16 +00:00
|
|
|
Site_Url: state.settings.Site_Url,
|
2017-12-08 19:13:21 +00:00
|
|
|
searchText: state.rooms.searchText
|
2017-08-13 01:35:09 +00:00
|
|
|
}))
|
2018-04-26 17:33:43 +00:00
|
|
|
export default class RoomsListView extends LoggedView {
|
2017-08-05 18:16:32 +00:00
|
|
|
static propTypes = {
|
2017-09-21 17:08:00 +00:00
|
|
|
navigation: PropTypes.object.isRequired,
|
2018-02-16 18:34:25 +00:00
|
|
|
user: PropTypes.object,
|
2017-08-22 01:43:29 +00:00
|
|
|
Site_Url: PropTypes.string,
|
2017-12-08 19:13:21 +00:00
|
|
|
server: PropTypes.string,
|
|
|
|
searchText: PropTypes.string
|
2017-08-10 14:59:07 +00:00
|
|
|
}
|
2017-08-09 16:19:17 +00:00
|
|
|
|
2017-12-08 19:13:21 +00:00
|
|
|
static navigationOptions = ({ navigation }) => ({
|
|
|
|
header: <Header subview={<RoomsListHeader navigation={navigation} />} />
|
|
|
|
});
|
|
|
|
|
2017-08-03 18:23:43 +00:00
|
|
|
constructor(props) {
|
2018-04-26 17:33:43 +00:00
|
|
|
super('RoomsListView', props);
|
2017-09-21 17:08:00 +00:00
|
|
|
|
2017-08-10 16:16:32 +00:00
|
|
|
this.state = {
|
2018-04-24 19:34:03 +00:00
|
|
|
search: []
|
2017-08-10 16:16:32 +00:00
|
|
|
};
|
2018-02-19 21:15:31 +00:00
|
|
|
this._keyExtractor = this._keyExtractor.bind(this);
|
2018-04-24 19:34:03 +00:00
|
|
|
this.data = database.objects('subscriptions').filtered('archived != true && open == true').sorted('roomUpdatedAt', true);
|
2017-08-21 00:11:46 +00:00
|
|
|
}
|
2017-09-21 17:08:00 +00:00
|
|
|
|
2017-11-20 22:18:00 +00:00
|
|
|
componentDidMount() {
|
2017-09-21 17:08:00 +00:00
|
|
|
this.data.addListener(this.updateState);
|
2017-08-13 01:35:09 +00:00
|
|
|
}
|
2017-09-21 17:08:00 +00:00
|
|
|
|
2017-11-07 20:25:04 +00:00
|
|
|
componentWillReceiveProps(props) {
|
|
|
|
if (this.props.server !== props.server) {
|
|
|
|
this.data.removeListener(this.updateState);
|
2018-04-24 19:34:03 +00:00
|
|
|
this.data = database.objects('subscriptions').filtered('archived != true && open == true').sorted('roomUpdatedAt', true);
|
2017-11-07 20:25:04 +00:00
|
|
|
this.data.addListener(this.updateState);
|
2017-12-08 19:13:21 +00:00
|
|
|
} else if (this.props.searchText !== props.searchText) {
|
|
|
|
this.search(props.searchText);
|
2017-11-07 20:25:04 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-02 21:31:44 +00:00
|
|
|
|
2017-08-09 16:19:17 +00:00
|
|
|
componentWillUnmount() {
|
2018-03-02 21:31:44 +00:00
|
|
|
this.updateState.stop();
|
2017-09-21 17:08:00 +00:00
|
|
|
this.data.removeAllListeners();
|
2017-08-12 20:52:55 +00:00
|
|
|
}
|
|
|
|
|
2017-12-08 19:13:21 +00:00
|
|
|
onSearchChangeText(text) {
|
|
|
|
this.search(text);
|
|
|
|
}
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
updateState = throttle(() => {
|
|
|
|
LayoutAnimation.easeInEaseOut();
|
2018-03-02 21:31:44 +00:00
|
|
|
this.forceUpdate();
|
2018-04-24 19:34:03 +00:00
|
|
|
}, 1500);
|
2018-02-16 18:34:25 +00:00
|
|
|
|
2018-03-02 21:31:44 +00:00
|
|
|
async search(text) {
|
2017-08-10 16:16:32 +00:00
|
|
|
const searchText = text.trim();
|
2017-08-15 19:28:46 +00:00
|
|
|
if (searchText === '') {
|
2018-03-02 21:31:44 +00:00
|
|
|
delete this.oldPromise;
|
2017-08-15 19:28:46 +00:00
|
|
|
return this.setState({
|
2018-04-24 19:34:03 +00:00
|
|
|
search: []
|
2017-08-15 19:28:46 +00:00
|
|
|
});
|
|
|
|
}
|
2017-08-09 18:01:54 +00:00
|
|
|
|
2018-03-29 17:55:37 +00:00
|
|
|
let data = database.objects('subscriptions').filtered('name CONTAINS[c] $0', searchText).slice(0, 7);
|
2018-03-02 21:31:44 +00:00
|
|
|
|
2018-05-18 17:55:08 +00:00
|
|
|
const usernames = data.map(sub => sub.name);
|
2018-03-02 21:31:44 +00:00
|
|
|
try {
|
|
|
|
if (data.length < 7) {
|
|
|
|
if (this.oldPromise) {
|
|
|
|
this.oldPromise('cancel');
|
|
|
|
}
|
|
|
|
|
|
|
|
const { users, rooms } = await Promise.race([
|
|
|
|
RocketChat.spotlight(searchText, usernames, { users: true, rooms: true }),
|
|
|
|
new Promise((resolve, reject) => this.oldPromise = reject)
|
|
|
|
]);
|
|
|
|
|
|
|
|
data = data.concat(users.map(user => ({
|
|
|
|
...user,
|
|
|
|
rid: user.username,
|
|
|
|
name: user.username,
|
|
|
|
t: 'd',
|
|
|
|
search: true
|
|
|
|
})), rooms.map(room => ({
|
|
|
|
rid: room._id,
|
|
|
|
...room,
|
|
|
|
search: true
|
|
|
|
})));
|
|
|
|
|
|
|
|
delete this.oldPromise;
|
2017-08-15 19:28:46 +00:00
|
|
|
}
|
2017-08-10 16:16:32 +00:00
|
|
|
this.setState({
|
2018-03-02 21:31:44 +00:00
|
|
|
search: data
|
2017-08-10 16:16:32 +00:00
|
|
|
});
|
2018-03-02 21:31:44 +00:00
|
|
|
} catch (e) {
|
|
|
|
// alert(JSON.stringify(e));
|
|
|
|
}
|
|
|
|
}
|
2017-08-10 16:16:32 +00:00
|
|
|
|
2018-03-02 21:31:44 +00:00
|
|
|
_onPressItem = async(item = {}) => {
|
|
|
|
if (!item.search) {
|
2018-05-18 17:55:08 +00:00
|
|
|
return goRoom({ rid: item.rid, name: item.name });
|
2017-08-10 16:16:32 +00:00
|
|
|
}
|
2018-03-02 21:31:44 +00:00
|
|
|
if (item.t === 'd') {
|
2018-05-07 20:43:26 +00:00
|
|
|
// if user is using the search we need first to join/create room
|
|
|
|
try {
|
|
|
|
const sub = await RocketChat.createDirectMessage(item.username);
|
|
|
|
return goRoom(sub);
|
2018-05-18 17:55:08 +00:00
|
|
|
} catch (e) {
|
|
|
|
log('RoomsListView._onPressItem', e);
|
2018-05-07 20:43:26 +00:00
|
|
|
}
|
2018-03-02 21:31:44 +00:00
|
|
|
}
|
|
|
|
return goRoom(item);
|
2017-08-05 18:16:32 +00:00
|
|
|
}
|
2017-09-21 17:08:00 +00:00
|
|
|
|
2018-05-18 17:55:08 +00:00
|
|
|
createChannel() {
|
2018-04-24 19:34:03 +00:00
|
|
|
this.props.navigation.navigate({
|
|
|
|
key: 'SelectedUsers',
|
|
|
|
routeName: 'SelectedUsers',
|
|
|
|
params: { nextAction: () => this.props.navigation.navigate('CreateChannel') }
|
|
|
|
});
|
2017-09-01 19:42:50 +00:00
|
|
|
}
|
2017-09-21 17:08:00 +00:00
|
|
|
|
2018-02-19 21:15:31 +00:00
|
|
|
_keyExtractor(item) {
|
|
|
|
return item.rid.replace(this.props.user.id, '').trim();
|
|
|
|
}
|
|
|
|
|
2017-08-10 16:16:32 +00:00
|
|
|
renderSearchBar = () => (
|
2017-08-10 20:26:11 +00:00
|
|
|
<View style={styles.searchBoxView}>
|
|
|
|
<TextInput
|
2017-08-18 21:30:16 +00:00
|
|
|
underlineColorAndroid='transparent'
|
2017-08-10 20:26:11 +00:00
|
|
|
style={styles.searchBox}
|
2017-12-08 19:13:21 +00:00
|
|
|
onChangeText={text => this.onSearchChangeText(text)}
|
2017-08-10 20:26:11 +00:00
|
|
|
returnKeyType='search'
|
|
|
|
placeholder='Search'
|
2017-08-10 20:31:14 +00:00
|
|
|
clearButtonMode='while-editing'
|
|
|
|
blurOnSubmit
|
2018-05-18 17:55:08 +00:00
|
|
|
autoCorrect={false}
|
|
|
|
autoCapitalize='none'
|
2017-08-10 20:26:11 +00:00
|
|
|
/>
|
|
|
|
</View>
|
2017-08-10 16:16:32 +00:00
|
|
|
);
|
|
|
|
|
2018-02-19 21:15:31 +00:00
|
|
|
renderItem = ({ item }) => {
|
2018-02-16 18:34:25 +00:00
|
|
|
const id = item.rid.replace(this.props.user.id, '').trim();
|
|
|
|
return (<RoomItem
|
2017-11-22 16:40:59 +00:00
|
|
|
alert={item.alert}
|
2017-09-21 17:08:00 +00:00
|
|
|
unread={item.unread}
|
2017-11-22 16:40:59 +00:00
|
|
|
userMentions={item.userMentions}
|
|
|
|
favorite={item.f}
|
2018-02-16 18:34:25 +00:00
|
|
|
lastMessage={item.lastMessage}
|
2017-08-21 16:16:23 +00:00
|
|
|
name={item.name}
|
2017-11-28 11:01:18 +00:00
|
|
|
_updatedAt={item.roomUpdatedAt}
|
2017-09-21 17:08:00 +00:00
|
|
|
key={item._id}
|
2018-02-16 18:34:25 +00:00
|
|
|
id={id}
|
2017-08-21 16:16:23 +00:00
|
|
|
type={item.t}
|
2017-08-21 00:11:46 +00:00
|
|
|
baseUrl={this.props.Site_Url}
|
2017-11-28 17:47:56 +00:00
|
|
|
onPress={() => this._onPressItem(item)}
|
2018-02-16 18:34:25 +00:00
|
|
|
/>);
|
|
|
|
}
|
2017-09-21 17:08:00 +00:00
|
|
|
|
2017-08-11 18:18:09 +00:00
|
|
|
renderList = () => (
|
2018-02-19 21:15:31 +00:00
|
|
|
<FlatList
|
2018-04-24 19:34:03 +00:00
|
|
|
data={this.state.search.length > 0 ? this.state.search : this.data}
|
2018-02-19 21:15:31 +00:00
|
|
|
keyExtractor={this._keyExtractor}
|
2017-08-11 18:18:09 +00:00
|
|
|
style={styles.list}
|
2018-02-19 21:15:31 +00:00
|
|
|
renderItem={this.renderItem}
|
2018-02-23 20:29:06 +00:00
|
|
|
ListHeaderComponent={Platform.OS === 'ios' ? this.renderSearchBar : null}
|
2017-12-08 19:13:21 +00:00
|
|
|
contentOffset={Platform.OS === 'ios' ? { x: 0, y: 38 } : {}}
|
2017-08-12 20:52:55 +00:00
|
|
|
enableEmptySections
|
2018-04-24 19:34:03 +00:00
|
|
|
removeClippedSubviews
|
2017-08-14 18:02:53 +00:00
|
|
|
keyboardShouldPersistTaps='always'
|
2017-08-11 18:18:09 +00:00
|
|
|
/>
|
|
|
|
)
|
2017-09-21 17:08:00 +00:00
|
|
|
|
2017-08-18 21:30:16 +00:00
|
|
|
renderCreateButtons = () => (
|
2017-09-01 19:42:50 +00:00
|
|
|
<ActionButton buttonColor='rgba(231,76,60,1)'>
|
2018-05-18 17:55:08 +00:00
|
|
|
<ActionButton.Item buttonColor='#9b59b6' title='Create Channel' onPress={() => { this.createChannel(); }} >
|
2017-09-01 19:42:50 +00:00
|
|
|
<Icon name='md-chatbubbles' style={styles.actionButtonIcon} />
|
|
|
|
</ActionButton.Item>
|
2017-09-21 17:08:00 +00:00
|
|
|
</ActionButton>
|
|
|
|
);
|
|
|
|
|
|
|
|
render = () => (
|
2017-08-21 00:11:46 +00:00
|
|
|
<View style={styles.container}>
|
2018-04-10 13:03:54 +00:00
|
|
|
{this.renderList()}
|
|
|
|
{Platform.OS === 'android' && this.renderCreateButtons()}
|
2017-08-21 00:11:46 +00:00
|
|
|
</View>)
|
2017-08-03 18:23:43 +00:00
|
|
|
}
|