2017-08-04 00:34:37 +00:00
|
|
|
import React from 'react';
|
2017-08-05 18:16:32 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2018-02-16 18:34:25 +00:00
|
|
|
import { Text, View, Button } from 'react-native';
|
2017-08-13 01:35:09 +00:00
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { bindActionCreators } from 'redux';
|
2018-01-09 17:12:55 +00:00
|
|
|
import equal from 'deep-equal';
|
2017-08-13 01:35:09 +00:00
|
|
|
|
2018-04-03 16:24:59 +00:00
|
|
|
import LoggedView from '../View';
|
2018-01-30 19:48:26 +00:00
|
|
|
import { List } from './ListView';
|
2017-12-08 19:13:21 +00:00
|
|
|
import * as actions from '../../actions';
|
2018-01-16 20:27:57 +00:00
|
|
|
import { openRoom, setLastOpen } from '../../actions/room';
|
2018-02-19 21:19:39 +00:00
|
|
|
import { editCancel, toggleReactionPicker, actionsShow } from '../../actions/messages';
|
2017-12-27 15:22:06 +00:00
|
|
|
import database from '../../lib/realm';
|
2017-12-08 19:13:21 +00:00
|
|
|
import RocketChat from '../../lib/rocketchat';
|
|
|
|
import Message from '../../containers/message';
|
|
|
|
import MessageActions from '../../containers/MessageActions';
|
2017-12-13 15:00:26 +00:00
|
|
|
import MessageErrorActions from '../../containers/MessageErrorActions';
|
2017-12-08 19:13:21 +00:00
|
|
|
import MessageBox from '../../containers/MessageBox';
|
|
|
|
import Header from '../../containers/Header';
|
|
|
|
import RoomsHeader from './Header';
|
2018-01-30 19:48:26 +00:00
|
|
|
import ReactionPicker from './ReactionPicker';
|
2018-01-09 17:12:55 +00:00
|
|
|
import Banner from './banner';
|
2017-12-08 19:13:21 +00:00
|
|
|
import styles from './styles';
|
2017-08-14 14:15:37 +00:00
|
|
|
|
2017-09-25 13:15:28 +00:00
|
|
|
@connect(
|
|
|
|
state => ({
|
2017-12-27 15:22:06 +00:00
|
|
|
Site_Url: state.settings.Site_Url || state.server ? state.server.server : '',
|
2017-09-25 13:15:28 +00:00
|
|
|
Message_TimeFormat: state.settings.Message_TimeFormat,
|
2017-12-02 13:19:58 +00:00
|
|
|
loading: state.messages.isFetching,
|
2018-01-30 19:48:26 +00:00
|
|
|
user: state.login.user,
|
2018-02-08 14:08:50 +00:00
|
|
|
actionMessage: state.messages.actionMessage,
|
|
|
|
layoutAnimation: state.room.layoutAnimation
|
2017-09-25 13:15:28 +00:00
|
|
|
}),
|
|
|
|
dispatch => ({
|
|
|
|
actions: bindActionCreators(actions, dispatch),
|
2017-11-24 20:44:52 +00:00
|
|
|
openRoom: room => dispatch(openRoom(room)),
|
2018-01-16 20:27:57 +00:00
|
|
|
editCancel: () => dispatch(editCancel()),
|
2018-01-30 19:48:26 +00:00
|
|
|
setLastOpen: date => dispatch(setLastOpen(date)),
|
2018-02-19 21:19:39 +00:00
|
|
|
toggleReactionPicker: message => dispatch(toggleReactionPicker(message)),
|
|
|
|
actionsShow: actionMessage => dispatch(actionsShow(actionMessage))
|
2017-09-25 13:15:28 +00:00
|
|
|
})
|
|
|
|
)
|
2018-04-03 16:24:59 +00:00
|
|
|
export default class RoomView extends LoggedView {
|
2017-08-05 18:16:32 +00:00
|
|
|
static propTypes = {
|
2017-09-21 17:08:00 +00:00
|
|
|
navigation: PropTypes.object.isRequired,
|
2017-11-20 22:18:00 +00:00
|
|
|
openRoom: PropTypes.func.isRequired,
|
2018-01-16 20:27:57 +00:00
|
|
|
setLastOpen: PropTypes.func.isRequired,
|
2017-12-02 13:19:58 +00:00
|
|
|
user: PropTypes.object.isRequired,
|
2017-11-24 20:44:52 +00:00
|
|
|
editCancel: PropTypes.func,
|
2017-08-12 20:52:55 +00:00
|
|
|
rid: PropTypes.string,
|
2017-08-13 01:35:09 +00:00
|
|
|
name: PropTypes.string,
|
2017-08-14 00:03:33 +00:00
|
|
|
Site_Url: PropTypes.string,
|
2018-01-30 19:48:26 +00:00
|
|
|
Message_TimeFormat: PropTypes.string,
|
|
|
|
loading: PropTypes.bool,
|
|
|
|
actionMessage: PropTypes.object,
|
2018-02-19 21:19:39 +00:00
|
|
|
toggleReactionPicker: PropTypes.func.isRequired,
|
|
|
|
actionsShow: PropTypes.func
|
2017-09-25 13:15:28 +00:00
|
|
|
};
|
2017-08-05 18:16:32 +00:00
|
|
|
|
2017-12-08 19:13:21 +00:00
|
|
|
static navigationOptions = ({ navigation }) => ({
|
|
|
|
header: <Header subview={<RoomsHeader navigation={navigation} />} />
|
|
|
|
});
|
|
|
|
|
2017-08-04 00:34:37 +00:00
|
|
|
constructor(props) {
|
2018-04-03 16:24:59 +00:00
|
|
|
super('RoomView', props);
|
2017-09-25 13:15:28 +00:00
|
|
|
this.rid =
|
|
|
|
props.rid ||
|
2017-11-28 17:47:56 +00:00
|
|
|
props.navigation.state.params.room.rid;
|
2018-01-30 19:48:26 +00:00
|
|
|
this.name = props.name ||
|
|
|
|
props.navigation.state.params.name ||
|
|
|
|
props.navigation.state.params.room.name;
|
2018-01-17 16:42:30 +00:00
|
|
|
this.rooms = database.objects('subscriptions').filtered('rid = $0', this.rid);
|
2017-08-07 18:42:02 +00:00
|
|
|
this.state = {
|
2017-08-10 16:49:15 +00:00
|
|
|
loaded: true,
|
2018-01-17 16:42:30 +00:00
|
|
|
joined: typeof props.rid === 'undefined',
|
2018-04-10 13:03:54 +00:00
|
|
|
room: {}
|
2017-08-07 18:42:02 +00:00
|
|
|
};
|
2018-01-30 19:48:26 +00:00
|
|
|
this.onReactionPress = this.onReactionPress.bind(this);
|
2017-08-07 00:34:35 +00:00
|
|
|
}
|
2017-08-04 00:34:37 +00:00
|
|
|
|
2018-03-02 15:11:34 +00:00
|
|
|
async componentDidMount() {
|
2017-09-21 17:08:00 +00:00
|
|
|
this.props.navigation.setParams({
|
2017-12-01 15:06:56 +00:00
|
|
|
title: this.name
|
2017-09-21 17:08:00 +00:00
|
|
|
});
|
2018-01-17 16:42:30 +00:00
|
|
|
this.updateRoom();
|
2018-02-08 14:08:50 +00:00
|
|
|
await this.props.openRoom({ rid: this.rid, name: this.name, ls: this.state.room.ls });
|
2018-01-30 19:48:26 +00:00
|
|
|
if (this.state.room.alert || this.state.room.unread || this.state.room.userMentions) {
|
|
|
|
this.props.setLastOpen(this.state.room.ls);
|
2018-01-16 20:27:57 +00:00
|
|
|
} else {
|
|
|
|
this.props.setLastOpen(null);
|
|
|
|
}
|
2018-01-30 19:48:26 +00:00
|
|
|
|
2018-01-17 16:42:30 +00:00
|
|
|
this.rooms.addListener(this.updateRoom);
|
2018-02-08 14:08:50 +00:00
|
|
|
}
|
2018-01-09 17:12:55 +00:00
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
2018-03-23 16:49:51 +00:00
|
|
|
return !(equal(this.props, nextProps) && equal(this.state, nextState) && this.state.room.ro === nextState.room.ro);
|
2017-09-21 17:08:00 +00:00
|
|
|
}
|
2017-08-07 00:34:35 +00:00
|
|
|
componentWillUnmount() {
|
2018-01-30 19:48:26 +00:00
|
|
|
this.rooms.removeAllListeners();
|
2017-11-24 20:44:52 +00:00
|
|
|
this.props.editCancel();
|
2017-08-04 00:34:37 +00:00
|
|
|
}
|
|
|
|
|
2018-01-30 19:48:26 +00:00
|
|
|
onEndReached = (data) => {
|
|
|
|
if (this.props.loading || this.state.end) {
|
|
|
|
return;
|
|
|
|
}
|
2017-11-21 16:55:32 +00:00
|
|
|
|
2018-01-30 19:48:26 +00:00
|
|
|
requestAnimationFrame(() => {
|
|
|
|
const lastRowData = data[data.length - 1];
|
|
|
|
if (!lastRowData) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
RocketChat.loadMessagesForRoom(this.rid, lastRowData.ts, ({ end }) => end && this.setState({
|
|
|
|
end
|
|
|
|
}));
|
2017-08-09 01:40:55 +00:00
|
|
|
});
|
2018-01-30 19:48:26 +00:00
|
|
|
}
|
|
|
|
|
2018-02-19 21:19:39 +00:00
|
|
|
onMessageLongPress = (message) => {
|
|
|
|
this.props.actionsShow(message);
|
|
|
|
}
|
|
|
|
|
2018-01-30 19:48:26 +00:00
|
|
|
onReactionPress = (shortname, messageId) => {
|
|
|
|
if (!messageId) {
|
|
|
|
RocketChat.setReaction(shortname, this.props.actionMessage._id);
|
|
|
|
return this.props.toggleReactionPicker();
|
|
|
|
}
|
|
|
|
RocketChat.setReaction(shortname, messageId);
|
|
|
|
};
|
2017-08-04 00:34:37 +00:00
|
|
|
|
2018-01-17 16:42:30 +00:00
|
|
|
updateRoom = () => {
|
2018-01-30 19:48:26 +00:00
|
|
|
this.setState({ room: this.rooms[0] });
|
2018-03-23 16:49:51 +00:00
|
|
|
this.forceUpdate();
|
2018-01-17 16:42:30 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 14:08:50 +00:00
|
|
|
sendMessage = (message) => {
|
|
|
|
RocketChat.sendMessage(this.rid, message).then(() => {
|
|
|
|
this.props.setLastOpen(null);
|
|
|
|
});
|
|
|
|
};
|
2017-08-09 01:40:55 +00:00
|
|
|
|
2017-09-21 17:08:00 +00:00
|
|
|
joinRoom = async() => {
|
|
|
|
await RocketChat.joinRoom(this.props.rid);
|
|
|
|
this.setState({
|
|
|
|
joined: true
|
|
|
|
});
|
2017-08-10 16:16:32 +00:00
|
|
|
};
|
|
|
|
|
2018-01-09 17:12:55 +00:00
|
|
|
renderItem = item => (
|
2017-08-09 01:40:55 +00:00
|
|
|
<Message
|
2017-12-13 15:00:26 +00:00
|
|
|
key={item._id}
|
2017-08-09 01:40:55 +00:00
|
|
|
item={item}
|
2018-03-02 21:31:44 +00:00
|
|
|
_updatedAt={item._updatedAt}
|
|
|
|
status={item.status}
|
2018-01-30 19:48:26 +00:00
|
|
|
reactions={JSON.parse(JSON.stringify(item.reactions))}
|
2017-08-13 23:45:47 +00:00
|
|
|
baseUrl={this.props.Site_Url}
|
2017-08-14 00:03:33 +00:00
|
|
|
Message_TimeFormat={this.props.Message_TimeFormat}
|
2017-12-02 13:19:58 +00:00
|
|
|
user={this.props.user}
|
2018-01-30 19:48:26 +00:00
|
|
|
onReactionPress={this.onReactionPress}
|
2018-02-19 21:19:39 +00:00
|
|
|
onLongPress={this.onMessageLongPress}
|
2018-03-29 17:55:37 +00:00
|
|
|
archived={this.state.room.archived}
|
2017-08-09 01:40:55 +00:00
|
|
|
/>
|
|
|
|
);
|
2017-08-07 00:34:35 +00:00
|
|
|
|
2017-09-25 13:15:28 +00:00
|
|
|
renderSeparator = () => <View style={styles.separator} />;
|
2017-08-10 16:16:32 +00:00
|
|
|
|
|
|
|
renderFooter = () => {
|
|
|
|
if (!this.state.joined) {
|
2017-08-09 20:08:50 +00:00
|
|
|
return (
|
2017-08-10 16:16:32 +00:00
|
|
|
<View>
|
|
|
|
<Text>You are in preview mode.</Text>
|
|
|
|
<Button title='Join' onPress={this.joinRoom} />
|
2017-08-09 20:08:50 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
2018-03-29 17:55:37 +00:00
|
|
|
if (this.state.room.ro || this.state.room.archived) {
|
2018-01-17 16:42:30 +00:00
|
|
|
return (
|
|
|
|
<View style={styles.readOnly}>
|
|
|
|
<Text>This room is read only</Text>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
2018-04-10 13:03:54 +00:00
|
|
|
return <MessageBox onSubmit={this.sendMessage} rid={this.rid} />;
|
2017-09-25 13:15:28 +00:00
|
|
|
};
|
2017-08-09 20:08:50 +00:00
|
|
|
|
2017-08-10 23:21:46 +00:00
|
|
|
renderHeader = () => {
|
|
|
|
if (this.state.end) {
|
2017-11-19 02:29:46 +00:00
|
|
|
return <Text style={styles.loadingMore}>Start of conversation</Text>;
|
2017-08-10 23:21:46 +00:00
|
|
|
}
|
2018-01-30 19:48:26 +00:00
|
|
|
return <Text style={styles.loadingMore}>Loading more messages...</Text>;
|
2017-11-21 16:55:32 +00:00
|
|
|
}
|
2017-08-04 00:34:37 +00:00
|
|
|
render() {
|
|
|
|
return (
|
2018-01-25 14:04:20 +00:00
|
|
|
<View style={styles.container}>
|
2018-01-09 17:12:55 +00:00
|
|
|
<Banner />
|
2018-02-08 14:08:50 +00:00
|
|
|
<List
|
|
|
|
key='room-view-messages'
|
|
|
|
end={this.state.end}
|
|
|
|
room={this.rid}
|
|
|
|
renderFooter={this.renderHeader}
|
|
|
|
onEndReached={this.onEndReached}
|
|
|
|
renderRow={item => this.renderItem(item)}
|
|
|
|
/>
|
2017-08-10 16:16:32 +00:00
|
|
|
{this.renderFooter()}
|
2018-01-30 19:48:26 +00:00
|
|
|
{this.state.room._id ? <MessageActions room={this.state.room} /> : null}
|
2017-12-13 15:00:26 +00:00
|
|
|
<MessageErrorActions />
|
2018-01-30 19:48:26 +00:00
|
|
|
<ReactionPicker onEmojiSelected={this.onReactionPress} />
|
2018-01-25 14:04:20 +00:00
|
|
|
</View>
|
2017-08-04 00:34:37 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|