2018-03-02 15:11:34 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-10-23 21:39:48 +00:00
|
|
|
import { FlatList, View, Text } from 'react-native';
|
2018-03-02 15:11:34 +00:00
|
|
|
import { connect } from 'react-redux';
|
2019-03-12 16:23:06 +00:00
|
|
|
import { SafeAreaView } from 'react-navigation';
|
2018-12-12 15:15:10 +00:00
|
|
|
import equal from 'deep-equal';
|
2018-03-02 15:11:34 +00:00
|
|
|
|
2018-04-03 16:24:59 +00:00
|
|
|
import LoggedView from '../View';
|
2018-03-02 15:11:34 +00:00
|
|
|
import styles from './styles';
|
2018-12-12 15:15:10 +00:00
|
|
|
import Message from '../../containers/message/Message';
|
2018-04-24 19:34:03 +00:00
|
|
|
import RCActivityIndicator from '../../containers/ActivityIndicator';
|
2018-06-01 17:38:13 +00:00
|
|
|
import I18n from '../../i18n';
|
2018-12-12 15:15:10 +00:00
|
|
|
import RocketChat from '../../lib/rocketchat';
|
2019-03-12 16:23:06 +00:00
|
|
|
import StatusBar from '../../containers/StatusBar';
|
2018-03-02 15:11:34 +00:00
|
|
|
|
2018-07-10 13:40:32 +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-12-21 10:55:35 +00:00
|
|
|
room: state.room,
|
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-09-11 16:32:52 +00:00
|
|
|
}
|
2018-07-10 13:40:32 +00:00
|
|
|
}))
|
|
|
|
/** @extends React.Component */
|
2018-04-03 16:24:59 +00:00
|
|
|
export default class MentionedMessagesView extends LoggedView {
|
2019-03-12 16:23:06 +00:00
|
|
|
static navigationOptions = {
|
|
|
|
title: I18n.t('Mentions')
|
2018-10-23 21:39:48 +00:00
|
|
|
}
|
|
|
|
|
2018-03-02 15:11:34 +00:00
|
|
|
static propTypes = {
|
|
|
|
user: PropTypes.object,
|
2018-12-12 15:15:10 +00:00
|
|
|
baseUrl: PropTypes.string,
|
2018-12-21 10:55:35 +00:00
|
|
|
customEmojis: PropTypes.object,
|
|
|
|
room: PropTypes.object
|
2018-03-02 15:11:34 +00:00
|
|
|
}
|
|
|
|
|
2018-04-03 16:24:59 +00:00
|
|
|
constructor(props) {
|
2019-03-12 16:23:06 +00:00
|
|
|
super('MentionedMessagesView', props);
|
2018-04-24 19:34:03 +00:00
|
|
|
this.state = {
|
2018-12-12 15:15:10 +00:00
|
|
|
loading: false,
|
|
|
|
messages: []
|
2018-04-24 19:34:03 +00:00
|
|
|
};
|
2018-04-03 16:24:59 +00:00
|
|
|
}
|
|
|
|
|
2018-03-02 15:11:34 +00:00
|
|
|
componentDidMount() {
|
2018-04-24 19:34:03 +00:00
|
|
|
this.load();
|
|
|
|
}
|
|
|
|
|
2018-12-12 15:15:10 +00:00
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
2018-12-21 10:55:35 +00:00
|
|
|
const { loading, messages } = this.state;
|
|
|
|
if (nextState.loading !== loading) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!equal(nextState.messages, messages)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
|
2018-12-12 15:15:10 +00:00
|
|
|
load = async() => {
|
|
|
|
const {
|
2018-12-21 10:55:35 +00:00
|
|
|
messages, total, loading
|
2018-12-12 15:15:10 +00:00
|
|
|
} = this.state;
|
|
|
|
const { user } = this.props;
|
|
|
|
if (messages.length === total || loading) {
|
2018-04-24 19:34:03 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-12-12 15:15:10 +00:00
|
|
|
|
|
|
|
this.setState({ loading: true });
|
|
|
|
|
|
|
|
try {
|
2018-12-21 10:55:35 +00:00
|
|
|
const { room } = this.props;
|
2018-12-12 15:15:10 +00:00
|
|
|
const result = await RocketChat.getMessages(
|
|
|
|
room.rid,
|
|
|
|
room.t,
|
|
|
|
{ 'mentions._id': { $in: [user.id] } },
|
|
|
|
messages.length
|
|
|
|
);
|
|
|
|
if (result.success) {
|
|
|
|
this.setState(prevState => ({
|
|
|
|
messages: [...prevState.messages, ...result.messages],
|
|
|
|
total: result.total,
|
|
|
|
loading: false
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
this.setState({ loading: false });
|
|
|
|
console.log('MentionedMessagesView -> load -> catch -> error', error);
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-02 15:11:34 +00:00
|
|
|
renderEmpty = () => (
|
2018-05-23 13:39:18 +00:00
|
|
|
<View style={styles.listEmptyContainer} testID='mentioned-messages-view'>
|
2018-06-01 17:38:13 +00:00
|
|
|
<Text>{I18n.t('No_mentioned_messages')}</Text>
|
2018-03-02 15:11:34 +00:00
|
|
|
</View>
|
|
|
|
)
|
|
|
|
|
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
|
2018-09-25 19:28:42 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2018-03-02 15:11:34 +00:00
|
|
|
|
|
|
|
render() {
|
2018-12-12 15:15:10 +00:00
|
|
|
const { messages, loading } = this.state;
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-12-12 15:15:10 +00:00
|
|
|
if (!loading && messages.length === 0) {
|
2018-03-02 15:11:34 +00:00
|
|
|
return this.renderEmpty();
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-03-02 15:11:34 +00:00
|
|
|
return (
|
2018-10-23 21:39:48 +00:00
|
|
|
<SafeAreaView style={styles.list} testID='mentioned-messages-view' forceInset={{ bottom: 'never' }}>
|
2019-03-12 16:23:06 +00:00
|
|
|
<StatusBar />
|
2018-05-23 13:39:18 +00:00
|
|
|
<FlatList
|
|
|
|
data={messages}
|
|
|
|
renderItem={this.renderItem}
|
|
|
|
style={styles.list}
|
|
|
|
keyExtractor={item => item._id}
|
2018-12-12 15:15:10 +00:00
|
|
|
onEndReached={this.load}
|
|
|
|
ListFooterComponent={loading ? <RCActivityIndicator /> : null}
|
2018-05-23 13:39:18 +00:00
|
|
|
/>
|
2018-08-01 19:35:06 +00:00
|
|
|
</SafeAreaView>
|
2018-03-02 15:11:34 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|