2018-03-23 16:49:51 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-04-24 19:34:03 +00:00
|
|
|
import { FlatList, View, Text } 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 { openRoomFiles, closeRoomFiles } from '../../actions/roomFiles';
|
|
|
|
import styles from './styles';
|
|
|
|
import Message from '../../containers/message';
|
2018-04-24 19:34:03 +00:00
|
|
|
import RCActivityIndicator from '../../containers/ActivityIndicator';
|
2018-03-23 16:49:51 +00:00
|
|
|
|
|
|
|
@connect(
|
|
|
|
state => ({
|
|
|
|
messages: state.roomFiles.messages,
|
2018-04-24 19:34:03 +00:00
|
|
|
ready: state.roomFiles.ready,
|
|
|
|
user: state.login.user
|
|
|
|
// baseUrl: state.settings.Site_Url || state.server ? state.server.server : ''
|
2018-03-23 16:49:51 +00:00
|
|
|
}),
|
|
|
|
dispatch => ({
|
2018-04-24 19:34:03 +00:00
|
|
|
openRoomFiles: (rid, limit) => dispatch(openRoomFiles(rid, limit)),
|
2018-03-23 16:49:51 +00:00
|
|
|
closeRoomFiles: () => dispatch(closeRoomFiles())
|
|
|
|
})
|
|
|
|
)
|
2018-04-03 16:24:59 +00:00
|
|
|
export default class RoomFilesView extends LoggedView {
|
2018-03-23 16:49:51 +00:00
|
|
|
static propTypes = {
|
|
|
|
navigation: PropTypes.object,
|
|
|
|
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,
|
|
|
|
openRoomFiles: PropTypes.func,
|
|
|
|
closeRoomFiles: PropTypes.func
|
|
|
|
}
|
|
|
|
|
2018-04-03 16:24:59 +00:00
|
|
|
constructor(props) {
|
|
|
|
super('RoomFilesView', 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.closeRoomFiles();
|
|
|
|
}
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
load = () => {
|
|
|
|
this.props.openRoomFiles(this.props.navigation.state.params.rid, this.limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 = () => (
|
|
|
|
<View style={styles.listEmptyContainer}>
|
|
|
|
<Text>No files</Text>
|
|
|
|
</View>
|
|
|
|
)
|
|
|
|
|
|
|
|
renderItem = ({ item }) => (
|
|
|
|
<Message
|
|
|
|
item={item}
|
|
|
|
style={styles.message}
|
2018-04-24 19:34:03 +00:00
|
|
|
reactions={item.reactions}
|
2018-03-23 16:49:51 +00:00
|
|
|
user={this.props.user}
|
2018-04-24 19:34:03 +00:00
|
|
|
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 { 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
|
|
|
|
|
|
|
const { loading, loadingMore } = this.state;
|
2018-03-23 16:49:51 +00:00
|
|
|
return (
|
|
|
|
<FlatList
|
|
|
|
key='room-files-view-list'
|
2018-04-24 19:34:03 +00:00
|
|
|
data={messages}
|
2018-03-23 16:49:51 +00:00
|
|
|
renderItem={this.renderItem}
|
|
|
|
style={styles.list}
|
|
|
|
keyExtractor={item => item._id}
|
2018-04-24 19:34:03 +00:00
|
|
|
onEndReached={this.moreData}
|
|
|
|
ListHeaderComponent={loading && <RCActivityIndicator />}
|
|
|
|
ListFooterComponent={loadingMore && <RCActivityIndicator />}
|
2018-03-23 16:49:51 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|