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';
|
2018-10-23 21:39:48 +00:00
|
|
|
import SafeAreaView from 'react-native-safe-area-view';
|
2018-12-21 10:55:35 +00:00
|
|
|
import equal from 'deep-equal';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
|
|
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';
|
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';
|
2018-11-14 21:42:03 +00:00
|
|
|
import { DEFAULT_HEADER } from '../../constants/headerOptions';
|
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 : '',
|
|
|
|
customEmojis: state.customEmojis,
|
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
|
|
|
}))
|
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 {
|
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
|
|
|
title: {
|
2018-11-14 21:42:03 +00:00
|
|
|
...DEFAULT_HEADER.topBar.title,
|
2018-10-23 21:39:48 +00:00
|
|
|
text: I18n.t('Search')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
static propTypes = {
|
2018-07-10 13:40:32 +00:00
|
|
|
rid: PropTypes.string,
|
|
|
|
user: PropTypes.object,
|
2018-12-12 15:15:10 +00:00
|
|
|
baseUrl: PropTypes.string,
|
|
|
|
customEmojis: PropTypes.object
|
2018-07-10 13:40:32 +00:00
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super('SearchMessagesView', props);
|
|
|
|
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
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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) => {
|
2018-12-21 10:55:35 +00:00
|
|
|
const { rid } = this.props;
|
2018-12-12 15:15:10 +00:00
|
|
|
this.setState({ searchText, loading: true, messages: [] });
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
try {
|
2018-12-21 10:55:35 +00:00
|
|
|
const result = await RocketChat.searchMessages(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 });
|
|
|
|
console.log('SearchMessagesView -> search -> catch -> error', 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}>
|
|
|
|
<Text>{I18n.t('No_results_found')}</Text>
|
|
|
|
</View>
|
|
|
|
)
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
renderItem = ({ item }) => {
|
2018-12-12 15:15:10 +00:00
|
|
|
const { user, customEmojis, baseUrl } = this.props;
|
2018-09-25 19:28:42 +00:00
|
|
|
return (
|
|
|
|
<Message
|
|
|
|
style={styles.message}
|
2018-12-12 15:15:10 +00:00
|
|
|
customEmojis={customEmojis}
|
|
|
|
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'
|
|
|
|
edited={!!item.editedAt}
|
|
|
|
header
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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' }}>
|
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
|
|
|
/>
|
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>
|
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
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|