2018-02-19 21:19:39 +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-02-19 21:19:39 +00:00
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import ActionSheet from 'react-native-actionsheet';
|
2018-10-23 21:39:48 +00:00
|
|
|
import SafeAreaView from 'react-native-safe-area-view';
|
2018-02-19 21:19:39 +00:00
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
import { openStarredMessages as openStarredMessagesAction, closeStarredMessages as closeStarredMessagesAction } from '../../actions/starredMessages';
|
|
|
|
import { toggleStarRequest as toggleStarRequestAction } from '../../actions/messages';
|
2018-04-03 16:24:59 +00:00
|
|
|
import LoggedView from '../View';
|
2018-02-19 21:19:39 +00:00
|
|
|
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-11-14 21:42:03 +00:00
|
|
|
import { DEFAULT_HEADER } from '../../constants/headerOptions';
|
2018-02-19 21:19:39 +00:00
|
|
|
|
|
|
|
const STAR_INDEX = 0;
|
|
|
|
const CANCEL_INDEX = 1;
|
2018-06-01 17:38:13 +00:00
|
|
|
const options = [I18n.t('Unstar'), I18n.t('Cancel')];
|
2018-02-19 21:19:39 +00:00
|
|
|
|
2018-07-10 13:40:32 +00:00
|
|
|
@connect(state => ({
|
|
|
|
messages: state.starredMessages.messages,
|
|
|
|
ready: state.starredMessages.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
|
2018-09-11 16:32:52 +00:00
|
|
|
}
|
2018-07-10 13:40:32 +00:00
|
|
|
}), dispatch => ({
|
2018-09-25 19:28:42 +00:00
|
|
|
openStarredMessages: (rid, limit) => dispatch(openStarredMessagesAction(rid, limit)),
|
|
|
|
closeStarredMessages: () => dispatch(closeStarredMessagesAction()),
|
|
|
|
toggleStarRequest: message => dispatch(toggleStarRequestAction(message))
|
2018-07-10 13:40:32 +00:00
|
|
|
}))
|
|
|
|
/** @extends React.Component */
|
2018-04-03 16:24:59 +00:00
|
|
|
export default class StarredMessagesView 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('Starred')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-02-19 21:19:39 +00:00
|
|
|
static propTypes = {
|
2018-07-10 13:40:32 +00:00
|
|
|
rid: PropTypes.string,
|
2018-02-19 21:19:39 +00:00
|
|
|
messages: PropTypes.array,
|
2018-04-24 19:34:03 +00:00
|
|
|
ready: PropTypes.bool,
|
2018-02-19 21:19:39 +00:00
|
|
|
user: PropTypes.object,
|
|
|
|
openStarredMessages: PropTypes.func,
|
|
|
|
closeStarredMessages: PropTypes.func,
|
|
|
|
toggleStarRequest: PropTypes.func
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
2018-04-03 16:24:59 +00:00
|
|
|
super('StarredMessagesView', props);
|
2018-02-19 21:19:39 +00:00
|
|
|
this.state = {
|
2018-04-24 19:34:03 +00:00
|
|
|
message: {},
|
|
|
|
loading: true,
|
|
|
|
loadingMore: false
|
2018-02-19 21:19:39 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-03-02 15:11:34 +00:00
|
|
|
componentDidMount() {
|
2018-04-24 19:34:03 +00:00
|
|
|
this.limit = 20;
|
|
|
|
this.load();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps(nextProps) {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { ready } = this.props;
|
|
|
|
if (nextProps.ready && nextProps.ready !== ready) {
|
2018-04-24 19:34:03 +00:00
|
|
|
this.setState({ loading: false, loadingMore: false });
|
|
|
|
}
|
2018-02-19 21:19:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { closeStarredMessages } = this.props;
|
|
|
|
closeStarredMessages();
|
2018-02-19 21:19:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onLongPress = (message) => {
|
|
|
|
this.setState({ message });
|
2018-08-01 19:35:06 +00:00
|
|
|
if (this.actionSheet && this.actionSheet.show) {
|
|
|
|
this.actionSheet.show();
|
|
|
|
}
|
2018-02-19 21:19:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleActionPress = (actionIndex) => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { message } = this.state;
|
|
|
|
const { toggleStarRequest } = this.props;
|
|
|
|
|
2018-02-19 21:19:39 +00:00
|
|
|
switch (actionIndex) {
|
|
|
|
case STAR_INDEX:
|
2018-09-25 19:28:42 +00:00
|
|
|
toggleStarRequest(message);
|
2018-02-19 21:19:39 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
load = () => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { rid, openStarredMessages } = this.props;
|
|
|
|
openStarredMessages(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-02-19 21:19:39 +00:00
|
|
|
renderEmpty = () => (
|
2018-05-23 13:39:18 +00:00
|
|
|
<View style={styles.listEmptyContainer} testID='starred-messages-view'>
|
2018-06-01 17:38:13 +00:00
|
|
|
<Text>{I18n.t('No_starred_messages')}</Text>
|
2018-02-19 21:19:39 +00:00
|
|
|
</View>
|
|
|
|
)
|
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
renderItem = ({ item }) => {
|
|
|
|
const { user } = this.props;
|
|
|
|
return (
|
|
|
|
<Message
|
|
|
|
item={item}
|
|
|
|
style={styles.message}
|
|
|
|
reactions={item.reactions}
|
|
|
|
user={user}
|
|
|
|
customTimeFormat='MMMM Do YYYY, h:mm:ss a'
|
|
|
|
onLongPress={this.onLongPress}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2018-02-19 21:19:39 +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-02-19 21:19:39 +00:00
|
|
|
return this.renderEmpty();
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-02-19 21:19:39 +00:00
|
|
|
return (
|
2018-10-23 21:39:48 +00:00
|
|
|
<SafeAreaView style={styles.list} testID='starred-messages-view' forceInset={{ bottom: 'never' }}>
|
2018-02-19 21:19:39 +00:00
|
|
|
<FlatList
|
2018-04-24 19:34:03 +00:00
|
|
|
data={messages}
|
2018-02-19 21:19:39 +00:00
|
|
|
renderItem={this.renderItem}
|
|
|
|
style={styles.list}
|
|
|
|
keyExtractor={item => item._id}
|
2018-04-24 19:34:03 +00:00
|
|
|
onEndReached={this.moreData}
|
2018-06-13 01:33:00 +00:00
|
|
|
ListHeaderComponent={loading ? <RCActivityIndicator /> : null}
|
|
|
|
ListFooterComponent={loadingMore ? <RCActivityIndicator /> : null}
|
2018-08-01 19:35:06 +00:00
|
|
|
/>
|
2018-02-19 21:19:39 +00:00
|
|
|
<ActionSheet
|
|
|
|
ref={o => this.actionSheet = o}
|
2018-06-01 17:38:13 +00:00
|
|
|
title={I18n.t('Actions')}
|
2018-02-19 21:19:39 +00:00
|
|
|
options={options}
|
|
|
|
cancelButtonIndex={CANCEL_INDEX}
|
|
|
|
onPress={this.handleActionPress}
|
|
|
|
/>
|
2018-08-01 19:35:06 +00:00
|
|
|
</SafeAreaView>
|
2018-02-19 21:19:39 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|