2018-03-23 16:49:51 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-08-01 19:35:06 +00:00
|
|
|
import { FlatList, View, Text, SafeAreaView } from 'react-native';
|
2018-03-23 16:49:51 +00:00
|
|
|
import { connect } from 'react-redux';
|
|
|
|
|
2018-04-03 16:24:59 +00:00
|
|
|
import LoggedView from '../View';
|
2018-03-23 16:49:51 +00:00
|
|
|
import { openSnippetedMessages, closeSnippetedMessages } from '../../actions/snippetedMessages';
|
|
|
|
import styles from './styles';
|
|
|
|
import Message from '../../containers/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-04-03 16:24:59 +00:00
|
|
|
|
2018-07-10 13:40:32 +00:00
|
|
|
@connect(state => ({
|
|
|
|
messages: state.snippetedMessages.messages,
|
|
|
|
ready: state.snippetedMessages.ready,
|
|
|
|
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
|
|
|
|
},
|
|
|
|
baseUrl: state.settings.Site_Url || state.server ? state.server.server : ''
|
|
|
|
}), dispatch => ({
|
|
|
|
openSnippetedMessages: (rid, limit) => dispatch(openSnippetedMessages(rid, limit)),
|
|
|
|
closeSnippetedMessages: () => dispatch(closeSnippetedMessages())
|
|
|
|
}))
|
|
|
|
/** @extends React.Component */
|
2018-04-03 16:24:59 +00:00
|
|
|
export default class SnippetedMessagesView extends LoggedView {
|
2018-03-23 16:49:51 +00:00
|
|
|
static propTypes = {
|
2018-07-10 13:40:32 +00:00
|
|
|
rid: PropTypes.string,
|
2018-03-23 16:49:51 +00:00
|
|
|
messages: PropTypes.array,
|
2018-04-24 19:34:03 +00:00
|
|
|
ready: PropTypes.bool,
|
2018-03-23 16:49:51 +00:00
|
|
|
user: PropTypes.object,
|
|
|
|
baseUrl: PropTypes.string,
|
|
|
|
openSnippetedMessages: PropTypes.func,
|
|
|
|
closeSnippetedMessages: PropTypes.func
|
|
|
|
}
|
|
|
|
|
2018-04-03 16:24:59 +00:00
|
|
|
constructor(props) {
|
|
|
|
super('SnippetedMessagesView', props);
|
2018-04-24 19:34:03 +00:00
|
|
|
this.state = {
|
|
|
|
loading: true,
|
|
|
|
loadingMore: false
|
|
|
|
};
|
2018-04-03 16:24:59 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 16:49:51 +00:00
|
|
|
componentDidMount() {
|
2018-04-24 19:34:03 +00:00
|
|
|
this.limit = 20;
|
|
|
|
this.load();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
if (nextProps.ready && nextProps.ready !== this.props.ready) {
|
|
|
|
this.setState({ loading: false, loadingMore: false });
|
|
|
|
}
|
2018-03-23 16:49:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.props.closeSnippetedMessages();
|
|
|
|
}
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
load() {
|
2018-07-10 13:40:32 +00:00
|
|
|
this.props.openSnippetedMessages(this.props.rid, this.limit);
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
moreData = () => {
|
|
|
|
const { loadingMore } = this.state;
|
|
|
|
const { messages } = this.props;
|
|
|
|
if (messages.length < this.limit) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!loadingMore) {
|
|
|
|
this.setState({ loadingMore: true });
|
|
|
|
this.limit += 20;
|
|
|
|
this.load();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-23 16:49:51 +00:00
|
|
|
renderEmpty = () => (
|
2018-05-23 13:39:18 +00:00
|
|
|
<View style={styles.listEmptyContainer} testID='snippeted-messages-view'>
|
2018-06-01 17:38:13 +00:00
|
|
|
<Text>{I18n.t('No_snippeted_messages')}</Text>
|
2018-03-23 16:49:51 +00:00
|
|
|
</View>
|
|
|
|
)
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
renderItem = ({ item }) => (
|
|
|
|
<Message
|
|
|
|
item={item}
|
|
|
|
style={styles.message}
|
|
|
|
reactions={item.reactions}
|
|
|
|
user={this.props.user}
|
|
|
|
baseUrl={this.props.baseUrl}
|
|
|
|
customTimeFormat='MMMM Do YYYY, h:mm:ss a'
|
|
|
|
onLongPress={() => {}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
2018-03-23 16:49:51 +00:00
|
|
|
render() {
|
2018-04-24 19:34:03 +00:00
|
|
|
const { loading, loadingMore } = this.state;
|
|
|
|
const { messages, ready } = this.props;
|
|
|
|
|
|
|
|
if (ready && messages.length === 0) {
|
2018-03-23 16:49:51 +00:00
|
|
|
return this.renderEmpty();
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-03-23 16:49:51 +00:00
|
|
|
return (
|
2018-08-01 19:35:06 +00:00
|
|
|
<SafeAreaView style={styles.list} testID='snippeted-messages-view'>
|
2018-05-23 13:39:18 +00:00
|
|
|
<FlatList
|
|
|
|
data={messages}
|
|
|
|
renderItem={this.renderItem}
|
|
|
|
style={styles.list}
|
|
|
|
keyExtractor={item => item._id}
|
|
|
|
onEndReached={this.moreData}
|
2018-06-13 01:33:00 +00:00
|
|
|
ListHeaderComponent={loading ? <RCActivityIndicator /> : null}
|
|
|
|
ListFooterComponent={loadingMore ? <RCActivityIndicator /> : null}
|
2018-05-23 13:39:18 +00:00
|
|
|
/>
|
2018-08-01 19:35:06 +00:00
|
|
|
</SafeAreaView>
|
2018-03-23 16:49:51 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|