2018-04-24 19:34:03 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-12-12 15:15:10 +00:00
|
|
|
import { View, FlatList, Text } from 'react-native';
|
2018-04-24 19:34:03 +00:00
|
|
|
import { connect } from 'react-redux';
|
2019-03-12 16:23:06 +00:00
|
|
|
import { SafeAreaView } from 'react-navigation';
|
2018-12-21 10:55:35 +00:00
|
|
|
import equal from 'deep-equal';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
|
|
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';
|
2018-12-12 15:15:10 +00:00
|
|
|
import Message from '../../containers/message/Message';
|
2018-04-24 19:34:03 +00:00
|
|
|
import scrollPersistTaps from '../../utils/scrollPersistTaps';
|
2018-06-01 17:38:13 +00:00
|
|
|
import I18n from '../../i18n';
|
2019-03-12 16:23:06 +00:00
|
|
|
import StatusBar from '../../containers/StatusBar';
|
2019-05-28 16:18:46 +00:00
|
|
|
import log from '../../utils/log';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
|
|
@connect(state => ({
|
2018-12-12 15:15:10 +00:00
|
|
|
baseUrl: state.settings.Site_Url || state.server ? state.server.server : '',
|
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-12-12 15:15:10 +00:00
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
}))
|
2019-05-28 13:03:08 +00:00
|
|
|
export default class SearchMessagesView extends React.Component {
|
2019-03-12 16:23:06 +00:00
|
|
|
static navigationOptions = {
|
|
|
|
title: I18n.t('Search')
|
2018-10-23 21:39:48 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
static propTypes = {
|
2019-03-12 16:23:06 +00:00
|
|
|
navigation: PropTypes.object,
|
2018-07-10 13:40:32 +00:00
|
|
|
user: PropTypes.object,
|
2019-05-20 20:43:50 +00:00
|
|
|
baseUrl: PropTypes.string
|
2018-07-10 13:40:32 +00:00
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
|
|
constructor(props) {
|
2019-05-28 13:03:08 +00:00
|
|
|
super(props);
|
2018-04-24 19:34:03 +00:00
|
|
|
this.state = {
|
2018-12-12 15:15:10 +00:00
|
|
|
loading: false,
|
2018-04-24 19:34:03 +00:00
|
|
|
messages: [],
|
2018-12-12 15:15:10 +00:00
|
|
|
searchText: ''
|
2018-04-24 19:34:03 +00:00
|
|
|
};
|
2019-04-08 12:35:28 +00:00
|
|
|
this.rid = props.navigation.getParam('rid');
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.name.focus();
|
|
|
|
}
|
|
|
|
|
2018-12-21 10:55:35 +00:00
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
|
|
|
const { loading, searchText, messages } = this.state;
|
|
|
|
if (nextState.loading !== loading) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextState.searchText !== searchText) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!equal(nextState.messages, messages)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
componentWillUnmount() {
|
2018-12-12 15:15:10 +00:00
|
|
|
this.search.stop();
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
|
2018-12-12 15:15:10 +00:00
|
|
|
// eslint-disable-next-line react/sort-comp
|
|
|
|
search = debounce(async(searchText) => {
|
|
|
|
this.setState({ searchText, loading: true, messages: [] });
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
try {
|
2019-04-08 12:35:28 +00:00
|
|
|
const result = await RocketChat.searchMessages(this.rid, searchText);
|
2018-12-12 15:15:10 +00:00
|
|
|
if (result.success) {
|
|
|
|
this.setState({
|
|
|
|
messages: result.messages || [],
|
|
|
|
loading: false
|
|
|
|
});
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
2018-12-12 15:15:10 +00:00
|
|
|
} catch (error) {
|
|
|
|
this.setState({ loading: false });
|
2019-05-28 16:18:46 +00:00
|
|
|
log('err_search_messages', error);
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
2018-12-12 15:15:10 +00:00
|
|
|
}, 1000)
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-12-12 15:15:10 +00:00
|
|
|
renderEmpty = () => (
|
|
|
|
<View style={styles.listEmptyContainer}>
|
2019-03-29 19:36:07 +00:00
|
|
|
<Text style={styles.noDataFound}>{I18n.t('No_results_found')}</Text>
|
2018-12-12 15:15:10 +00:00
|
|
|
</View>
|
|
|
|
)
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
renderItem = ({ item }) => {
|
2019-05-20 20:43:50 +00:00
|
|
|
const { user, baseUrl } = this.props;
|
2018-09-25 19:28:42 +00:00
|
|
|
return (
|
|
|
|
<Message
|
2018-12-12 15:15:10 +00:00
|
|
|
baseUrl={baseUrl}
|
2018-09-25 19:28:42 +00:00
|
|
|
user={user}
|
2018-12-12 15:15:10 +00:00
|
|
|
author={item.u}
|
|
|
|
ts={item.ts}
|
|
|
|
msg={item.msg}
|
|
|
|
attachments={item.attachments || []}
|
|
|
|
timeFormat='MMM Do YYYY, h:mm:ss a'
|
2019-05-20 20:43:50 +00:00
|
|
|
isEdited={!!item.editedAt}
|
|
|
|
isHeader
|
|
|
|
onOpenFileModal={() => {}}
|
2018-12-12 15:15:10 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderList = () => {
|
|
|
|
const { messages, loading, searchText } = this.state;
|
|
|
|
|
|
|
|
if (!loading && messages.length === 0 && searchText.length) {
|
|
|
|
return this.renderEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<FlatList
|
|
|
|
data={messages}
|
|
|
|
renderItem={this.renderItem}
|
|
|
|
style={styles.list}
|
|
|
|
keyExtractor={item => item._id}
|
|
|
|
onEndReached={this.load}
|
|
|
|
ListFooterComponent={loading ? <RCActivityIndicator /> : null}
|
|
|
|
{...scrollPersistTaps}
|
2018-09-25 19:28:42 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2018-10-23 21:39:48 +00:00
|
|
|
<SafeAreaView style={styles.container} testID='search-messages-view' forceInset={{ bottom: 'never' }}>
|
2019-03-12 16:23:06 +00:00
|
|
|
<StatusBar />
|
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-12-12 15:15:10 +00:00
|
|
|
onChangeText={this.search}
|
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
|
|
|
/>
|
2019-05-20 20:43:50 +00:00
|
|
|
<Markdown msg={I18n.t('You_can_search_using_RegExp_eg')} username='' baseUrl='' />
|
2018-04-24 19:34:03 +00:00
|
|
|
<View style={styles.divider} />
|
|
|
|
</View>
|
2018-12-12 15:15:10 +00:00
|
|
|
{this.renderList()}
|
2018-08-01 19:35:06 +00:00
|
|
|
</SafeAreaView>
|
2018-04-24 19:34:03 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|