2018-04-24 19:34:03 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-08-01 19:35:06 +00:00
|
|
|
import { View, FlatList, SafeAreaView } from 'react-native';
|
2018-04-24 19:34:03 +00:00
|
|
|
import { connect } from 'react-redux';
|
|
|
|
|
|
|
|
import LoggedView from '../View';
|
|
|
|
import RCTextInput from '../../containers/TextInput';
|
|
|
|
import RCActivityIndicator from '../../containers/ActivityIndicator';
|
|
|
|
import styles from './styles';
|
|
|
|
import Markdown from '../../containers/message/Markdown';
|
|
|
|
import debounce from '../../utils/debounce';
|
|
|
|
import RocketChat from '../../lib/rocketchat';
|
|
|
|
import buildMessage from '../../lib/methods/helpers/buildMessage';
|
|
|
|
import Message from '../../containers/message';
|
|
|
|
import scrollPersistTaps from '../../utils/scrollPersistTaps';
|
2018-05-18 17:55:08 +00:00
|
|
|
import log from '../../utils/log';
|
2018-06-01 17:38:13 +00:00
|
|
|
import I18n from '../../i18n';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
|
|
@connect(state => ({
|
2018-07-10 13:40:32 +00:00
|
|
|
user: {
|
|
|
|
id: state.login.user && state.login.user.id,
|
|
|
|
username: state.login.user && state.login.user.username,
|
|
|
|
token: state.login.user && state.login.user.token
|
|
|
|
},
|
2018-04-24 19:34:03 +00:00
|
|
|
baseUrl: state.settings.Site_Url || state.server ? state.server.server : ''
|
|
|
|
}))
|
2018-07-10 13:40:32 +00:00
|
|
|
/** @extends React.Component */
|
2018-04-24 19:34:03 +00:00
|
|
|
export default class SearchMessagesView extends LoggedView {
|
|
|
|
static propTypes = {
|
2018-07-10 13:40:32 +00:00
|
|
|
rid: PropTypes.string,
|
|
|
|
navigator: PropTypes.object,
|
|
|
|
user: PropTypes.object,
|
|
|
|
baseUrl: PropTypes.string
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super('SearchMessagesView', props);
|
|
|
|
this.limit = 0;
|
|
|
|
this.state = {
|
|
|
|
messages: [],
|
|
|
|
searching: false,
|
|
|
|
loadingMore: false
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.name.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.onChangeSearch.stop();
|
|
|
|
}
|
|
|
|
|
2018-07-10 13:40:32 +00:00
|
|
|
onChangeSearch = debounce((search) => {
|
|
|
|
this.searchText = search;
|
|
|
|
this.limit = 0;
|
|
|
|
if (!this.state.searching) {
|
|
|
|
this.setState({ searching: true });
|
|
|
|
}
|
|
|
|
this.search();
|
|
|
|
}, 1000)
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
search = async() => {
|
|
|
|
if (this._cancel) {
|
|
|
|
this._cancel('cancel');
|
|
|
|
}
|
|
|
|
const cancel = new Promise((r, reject) => this._cancel = reject);
|
|
|
|
let messages = [];
|
|
|
|
try {
|
2018-07-10 13:40:32 +00:00
|
|
|
const result = await Promise.race([RocketChat.messageSearch(this.searchText, this.props.rid, this.limit), cancel]);
|
2018-05-18 17:55:08 +00:00
|
|
|
messages = result.message.docs.map(message => buildMessage(message));
|
2018-04-24 19:34:03 +00:00
|
|
|
this.setState({ messages, searching: false, loadingMore: false });
|
2018-05-18 17:55:08 +00:00
|
|
|
} catch (e) {
|
2018-04-24 19:34:03 +00:00
|
|
|
this._cancel = null;
|
2018-05-18 17:55:08 +00:00
|
|
|
if (e !== 'cancel') {
|
2018-04-24 19:34:03 +00:00
|
|
|
return this.setState({ searching: false, loadingMore: false });
|
|
|
|
}
|
2018-05-18 17:55:08 +00:00
|
|
|
log('SearchMessagesView.search', e);
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
moreData = () => {
|
|
|
|
const { loadingMore, messages } = this.state;
|
|
|
|
if (messages.length < this.limit) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this.searchText && !loadingMore) {
|
|
|
|
this.setState({ loadingMore: true });
|
|
|
|
this.limit += 20;
|
|
|
|
this.search();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
renderItem = ({ item }) => (
|
|
|
|
<Message
|
|
|
|
item={item}
|
|
|
|
style={styles.message}
|
|
|
|
reactions={item.reactions}
|
|
|
|
user={this.props.user}
|
|
|
|
customTimeFormat='MMMM Do YYYY, h:mm:ss a'
|
|
|
|
onReactionPress={async(emoji) => {
|
2018-05-18 17:55:08 +00:00
|
|
|
try {
|
|
|
|
await RocketChat.setReaction(emoji, item._id);
|
|
|
|
this.search();
|
|
|
|
this.forceUpdate();
|
|
|
|
} catch (e) {
|
|
|
|
log('SearchMessagesView.onReactionPress', e);
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { searching, loadingMore } = this.state;
|
|
|
|
return (
|
2018-08-01 19:35:06 +00:00
|
|
|
<SafeAreaView style={styles.container} testID='search-messages-view'>
|
2018-04-24 19:34:03 +00:00
|
|
|
<View style={styles.searchContainer}>
|
|
|
|
<RCTextInput
|
|
|
|
inputRef={(e) => { this.name = e; }}
|
2018-06-01 17:38:13 +00:00
|
|
|
label={I18n.t('Search')}
|
2018-04-24 19:34:03 +00:00
|
|
|
onChangeText={this.onChangeSearch}
|
2018-06-01 17:38:13 +00:00
|
|
|
placeholder={I18n.t('Search_Messages')}
|
2018-05-23 13:39:18 +00:00
|
|
|
testID='search-message-view-input'
|
2018-04-24 19:34:03 +00:00
|
|
|
/>
|
2018-09-11 16:32:52 +00:00
|
|
|
<Markdown msg={I18n.t('You_can_search_using_RegExp_eg')} username='' baseUrl='' customEmojis={{}} />
|
2018-04-24 19:34:03 +00:00
|
|
|
<View style={styles.divider} />
|
|
|
|
</View>
|
|
|
|
<FlatList
|
|
|
|
data={this.state.messages}
|
|
|
|
renderItem={this.renderItem}
|
|
|
|
style={styles.list}
|
|
|
|
keyExtractor={item => item._id}
|
|
|
|
onEndReached={this.moreData}
|
2018-06-13 01:33:00 +00:00
|
|
|
ListHeaderComponent={searching ? <RCActivityIndicator /> : null}
|
|
|
|
ListFooterComponent={loadingMore ? <RCActivityIndicator /> : null}
|
2018-04-24 19:34:03 +00:00
|
|
|
{...scrollPersistTaps}
|
|
|
|
/>
|
2018-08-01 19:35:06 +00:00
|
|
|
</SafeAreaView>
|
2018-04-24 19:34:03 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|