Add room search (#16)
This commit is contained in:
parent
a154656fa4
commit
b7be727d0e
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"expoServerPort": null,
|
||||
"packagerPort": null,
|
||||
"packagerPid": null
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"hostType": "tunnel",
|
||||
"lanType": "ip",
|
||||
"dev": true,
|
||||
"strict": false,
|
||||
"minify": false,
|
||||
"urlType": "exp",
|
||||
"urlRandomness": null
|
||||
}
|
|
@ -34,7 +34,7 @@ export default class RoomItem extends React.PureComponent {
|
|||
}
|
||||
|
||||
_onPress = () => {
|
||||
this.props.onPressItem(this.props.id);
|
||||
this.props.onPressItem(this.props.id, this.props.item);
|
||||
};
|
||||
|
||||
renderNumber = (item) => {
|
||||
|
|
|
@ -147,6 +147,39 @@ const RocketChat = {
|
|||
return resolve(result);
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
spotlight(search, usernames) {
|
||||
return new Promise((resolve, reject) => {
|
||||
Meteor.call('spotlight', search, usernames, (error, result) => {
|
||||
if (error) {
|
||||
return reject(error);
|
||||
}
|
||||
return resolve(result);
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
createDirectMessage(username) {
|
||||
return new Promise((resolve, reject) => {
|
||||
Meteor.call('createDirectMessage', username, (error, result) => {
|
||||
if (error) {
|
||||
return reject(error);
|
||||
}
|
||||
return resolve(result);
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
joinRoom(rid) {
|
||||
return new Promise((resolve, reject) => {
|
||||
Meteor.call('joinRoom', rid, (error, result) => {
|
||||
if (error) {
|
||||
return reject(error);
|
||||
}
|
||||
return resolve(result);
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import React from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
import { Text, View, FlatList, StyleSheet } from 'react-native';
|
||||
import { Text, View, FlatList, StyleSheet, Button } from 'react-native';
|
||||
import realm from '../lib/realm';
|
||||
import RocketChat from '../lib/rocketchat';
|
||||
|
||||
|
@ -36,17 +36,18 @@ export default class RoomView extends React.Component {
|
|||
}
|
||||
|
||||
static navigationOptions = ({ navigation }) => ({
|
||||
title: realm.objectForPrimaryKey('subscriptions', navigation.state.params.sid).name
|
||||
title: navigation.state.params.name || realm.objectForPrimaryKey('subscriptions', navigation.state.params.sid).name
|
||||
});
|
||||
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.rid = realm.objectForPrimaryKey('subscriptions', props.navigation.state.params.sid).rid;
|
||||
this.rid = props.navigation.state.params.rid || realm.objectForPrimaryKey('subscriptions', props.navigation.state.params.sid).rid;
|
||||
// this.rid = 'GENERAL';
|
||||
|
||||
this.state = {
|
||||
dataSource: this.getMessages(),
|
||||
loaded: false
|
||||
loaded: false,
|
||||
joined: typeof props.navigation.state.params.rid === 'undefined'
|
||||
};
|
||||
|
||||
this.url = realm.objectForPrimaryKey('settings', 'Site_Url').value;
|
||||
|
@ -77,13 +78,14 @@ export default class RoomView extends React.Component {
|
|||
|
||||
sendMessage = message => RocketChat.sendMessage(this.rid, message);
|
||||
|
||||
renderItem = ({ item }) => (
|
||||
<Message
|
||||
id={item._id}
|
||||
item={item}
|
||||
baseUrl={this.url}
|
||||
/>
|
||||
);
|
||||
joinRoom = () => {
|
||||
RocketChat.joinRoom(this.props.navigation.state.params.rid)
|
||||
.then(() => {
|
||||
this.setState({
|
||||
joined: true
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
renderBanner = () => {
|
||||
if (this.state.loaded === false) {
|
||||
|
@ -93,6 +95,34 @@ export default class RoomView extends React.Component {
|
|||
</View>
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
renderItem = ({ item }) => (
|
||||
<Message
|
||||
id={item._id}
|
||||
item={item}
|
||||
baseUrl={this.url}
|
||||
/>
|
||||
);
|
||||
|
||||
renderSeparator = () => (
|
||||
<View style={styles.separator} />
|
||||
);
|
||||
|
||||
renderFooter = () => {
|
||||
if (!this.state.joined) {
|
||||
return (
|
||||
<View>
|
||||
<Text>You are in preview mode.</Text>
|
||||
<Button title='Join' onPress={this.joinRoom} />
|
||||
</View>
|
||||
);
|
||||
}
|
||||
return (
|
||||
<MessageBox
|
||||
onSubmit={this.sendMessage}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
render() {
|
||||
|
@ -107,9 +137,7 @@ export default class RoomView extends React.Component {
|
|||
renderItem={this.renderItem}
|
||||
keyExtractor={item => item._id}
|
||||
/>
|
||||
<MessageBox
|
||||
onSubmit={this.sendMessage}
|
||||
/>
|
||||
{this.renderFooter()}
|
||||
</KeyboardView>
|
||||
);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import React from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
import { Text, View, FlatList, StyleSheet, Platform } from 'react-native';
|
||||
import { Text, View, FlatList, StyleSheet, Platform, TextInput } from 'react-native';
|
||||
import Meteor from 'react-native-meteor';
|
||||
import realm from '../lib/realm';
|
||||
import RocketChat from '../lib/rocketchat';
|
||||
|
@ -75,7 +75,12 @@ export default class RoomsListView extends React.Component {
|
|||
constructor(props) {
|
||||
super(props);
|
||||
|
||||
this.state = this.getState();
|
||||
this.state = {
|
||||
dataSource: this.getSubscriptions(),
|
||||
searching: false,
|
||||
searchDataSource: [],
|
||||
searchText: ''
|
||||
};
|
||||
}
|
||||
|
||||
componentWillMount() {
|
||||
|
@ -94,8 +99,52 @@ export default class RoomsListView extends React.Component {
|
|||
realm.removeListener('change', this.updateState);
|
||||
}
|
||||
|
||||
getState = () => ({
|
||||
dataSource: realm.objects('subscriptions').filtered('_server.id = $0', RocketChat.currentServer).sorted('name').slice()
|
||||
onSearchChangeText = (text) => {
|
||||
const searchText = text.trim();
|
||||
this.setState({
|
||||
searchText: text,
|
||||
searching: searchText !== ''
|
||||
});
|
||||
|
||||
if (searchText !== '') {
|
||||
const dataSource = [];
|
||||
const usernames = [];
|
||||
realm.objects('subscriptions').filtered('_server.id = $0 AND name CONTAINS[c] $1', RocketChat.currentServer, searchText).forEach((sub) => {
|
||||
dataSource.push(sub);
|
||||
|
||||
if (sub.t === 'd') {
|
||||
usernames.push(sub.name);
|
||||
}
|
||||
});
|
||||
|
||||
if (dataSource.length < 5) {
|
||||
RocketChat.spotlight(searchText, usernames)
|
||||
.then((results) => {
|
||||
results.users.forEach((user) => {
|
||||
dataSource.push({
|
||||
...user,
|
||||
name: user.username,
|
||||
t: 'd',
|
||||
search: true
|
||||
});
|
||||
});
|
||||
|
||||
results.rooms.forEach((room) => {
|
||||
dataSource.push({
|
||||
...room,
|
||||
search: true
|
||||
});
|
||||
});
|
||||
|
||||
this.setState({
|
||||
searchDataSource: dataSource
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
getSubscriptions = () => realm.objects('subscriptions').filtered('_server.id = $0', RocketChat.currentServer).sorted('name').slice()
|
||||
.sort((a, b) => {
|
||||
if (a.unread < b.unread) {
|
||||
return 1;
|
||||
|
@ -106,16 +155,41 @@ export default class RoomsListView extends React.Component {
|
|||
}
|
||||
|
||||
return 0;
|
||||
})
|
||||
})
|
||||
});
|
||||
|
||||
updateState = () => {
|
||||
this.setState(this.getState());
|
||||
this.setState({
|
||||
dataSource: this.getSubscriptions()
|
||||
});
|
||||
}
|
||||
|
||||
_onPressItem = (id) => {
|
||||
_onPressItem = (id, item) => {
|
||||
const { navigate } = this.props.navigation;
|
||||
|
||||
const clearSearch = () => {
|
||||
this.setState({
|
||||
searchText: '',
|
||||
searching: false,
|
||||
searchDataSource: []
|
||||
});
|
||||
};
|
||||
|
||||
// if user is using the search we need first to join/create room
|
||||
if (item.search) {
|
||||
if (item.t === 'd') {
|
||||
RocketChat.createDirectMessage(item.username)
|
||||
.then(room => realm.objects('subscriptions').filtered('_server.id = $0 AND rid = $1', RocketChat.currentServer, room.rid))
|
||||
.then(subs => navigate('Room', { sid: subs[0]._id }))
|
||||
.then(() => clearSearch());
|
||||
} else {
|
||||
navigate('Room', { rid: item._id, name: item.name });
|
||||
clearSearch();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
navigate('Room', { sid: id });
|
||||
clearSearch();
|
||||
}
|
||||
|
||||
renderBanner = () => {
|
||||
|
@ -150,19 +224,18 @@ export default class RoomsListView extends React.Component {
|
|||
<View style={styles.separator} />
|
||||
);
|
||||
|
||||
renderList = () => {
|
||||
if (this.state.dataSource.length) {
|
||||
return (
|
||||
<FlatList
|
||||
style={styles.list}
|
||||
data={this.state.dataSource}
|
||||
renderItem={this.renderItem}
|
||||
keyExtractor={item => item._id}
|
||||
ItemSeparatorComponent={this.renderSeparator}
|
||||
renderSearchBar = () => (
|
||||
<TextInput
|
||||
style={styles.searchBox}
|
||||
value={this.state.searchText}
|
||||
onChangeText={this.onSearchChangeText}
|
||||
returnKeyType='search'
|
||||
placeholder='Search'
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
renderList = () => {
|
||||
if (!this.state.searching && !this.state.dataSource.length) {
|
||||
return (
|
||||
<View style={styles.emptyView}>
|
||||
<Text style={styles.emptyText}>No rooms</Text>
|
||||
|
@ -170,10 +243,22 @@ export default class RoomsListView extends React.Component {
|
|||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<FlatList
|
||||
style={styles.list}
|
||||
data={this.state.searching ? this.state.searchDataSource : this.state.dataSource}
|
||||
renderItem={this.renderItem}
|
||||
keyExtractor={item => item._id}
|
||||
ItemSeparatorComponent={this.renderSeparator}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
render() {
|
||||
return (
|
||||
<View style={styles.container}>
|
||||
{this.renderBanner()}
|
||||
{this.renderSearchBar()}
|
||||
{this.renderList()}
|
||||
</View>
|
||||
);
|
||||
|
|
Loading…
Reference in New Issue