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';
|
2019-01-30 12:11:02 +00:00
|
|
|
import ActionSheet from 'react-native-action-sheet';
|
2018-10-23 21:39:48 +00:00
|
|
|
import SafeAreaView from 'react-native-safe-area-view';
|
2018-12-12 15:15:10 +00:00
|
|
|
import equal from 'deep-equal';
|
2018-02-19 21:19:39 +00:00
|
|
|
|
2018-04-03 16:24:59 +00:00
|
|
|
import LoggedView from '../View';
|
2018-02-19 21:19:39 +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';
|
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 => ({
|
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 StarredMessagesView extends LoggedView {
|
2018-10-23 21:39:48 +00:00
|
|
|
static options() {
|
|
|
|
return {
|
|
|
|
topBar: {
|
|
|
|
title: {
|
|
|
|
text: I18n.t('Starred')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-02-19 21:19:39 +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-02-19 21:19:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
2018-04-03 16:24:59 +00:00
|
|
|
super('StarredMessagesView', props);
|
2018-02-19 21:19:39 +00:00
|
|
|
this.state = {
|
2018-12-12 15:15:10 +00:00
|
|
|
loading: false,
|
|
|
|
messages: []
|
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.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-02-19 21:19:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onLongPress = (message) => {
|
|
|
|
this.setState({ message });
|
2019-01-30 12:11:02 +00:00
|
|
|
this.showActionSheet();
|
|
|
|
}
|
|
|
|
|
|
|
|
showActionSheet = () => {
|
|
|
|
ActionSheet.showActionSheetWithOptions({
|
|
|
|
options,
|
|
|
|
cancelButtonIndex: CANCEL_INDEX,
|
|
|
|
title: I18n.t('Actions')
|
|
|
|
}, (actionIndex) => {
|
|
|
|
this.handleActionPress(actionIndex);
|
|
|
|
});
|
2018-02-19 21:19:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleActionPress = (actionIndex) => {
|
|
|
|
switch (actionIndex) {
|
|
|
|
case STAR_INDEX:
|
2018-12-12 15:15:10 +00:00
|
|
|
this.unStar();
|
2018-02-19 21:19:39 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-12 15:15:10 +00:00
|
|
|
unStar = async() => {
|
|
|
|
const { message } = this.state;
|
|
|
|
try {
|
|
|
|
const result = await RocketChat.toggleStarMessage(message);
|
|
|
|
if (result.success) {
|
|
|
|
this.setState(prevState => ({
|
|
|
|
messages: prevState.messages.filter(item => item._id !== message._id)
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.log('StarredMessagesView -> unStar -> catch -> error', error);
|
|
|
|
}
|
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,
|
|
|
|
{ 'starred._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('StarredMessagesView -> load -> catch -> error', error);
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 }) => {
|
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
|
|
|
|
onLongPress={() => this.onLongPress(item)}
|
2018-09-25 19:28:42 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2018-02-19 21:19:39 +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-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-12-12 15:15:10 +00:00
|
|
|
onEndReached={this.load}
|
|
|
|
ListFooterComponent={loading ? <RCActivityIndicator /> : null}
|
2018-08-01 19:35:06 +00:00
|
|
|
/>
|
|
|
|
</SafeAreaView>
|
2018-02-19 21:19:39 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|