verdnatura-chat/app/views/RoomView/index.js

211 lines
5.8 KiB
JavaScript
Raw Normal View History

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';
import { Text, View, Button } from 'react-native';
2017-08-13 01:35:09 +00:00
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import equal from 'deep-equal';
2017-08-13 01:35:09 +00:00
import { List } from './ListView';
import * as actions from '../../actions';
import { openRoom, setLastOpen } from '../../actions/room';
import { editCancel, toggleReactionPicker } from '../../actions/messages';
import database from '../../lib/realm';
import RocketChat from '../../lib/rocketchat';
import Message from '../../containers/message';
import MessageActions from '../../containers/MessageActions';
import MessageErrorActions from '../../containers/MessageErrorActions';
import MessageBox from '../../containers/MessageBox';
import Header from '../../containers/Header';
import RoomsHeader from './Header';
import ReactionPicker from './ReactionPicker';
import Banner from './banner';
import styles from './styles';
2017-08-14 14:15:37 +00:00
@connect(
state => ({
Site_Url: state.settings.Site_Url || state.server ? state.server.server : '',
Message_TimeFormat: state.settings.Message_TimeFormat,
loading: state.messages.isFetching,
user: state.login.user,
actionMessage: state.messages.actionMessage,
layoutAnimation: state.room.layoutAnimation
}),
dispatch => ({
actions: bindActionCreators(actions, dispatch),
2017-11-24 20:44:52 +00:00
openRoom: room => dispatch(openRoom(room)),
editCancel: () => dispatch(editCancel()),
setLastOpen: date => dispatch(setLastOpen(date)),
toggleReactionPicker: message => dispatch(toggleReactionPicker(message))
})
)
2017-08-05 18:16:32 +00:00
export default class RoomView extends React.Component {
static propTypes = {
navigation: PropTypes.object.isRequired,
2017-11-20 22:18:00 +00:00
openRoom: PropTypes.func.isRequired,
setLastOpen: PropTypes.func.isRequired,
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,
Site_Url: PropTypes.string,
Message_TimeFormat: PropTypes.string,
loading: PropTypes.bool,
actionMessage: PropTypes.object,
toggleReactionPicker: PropTypes.func.isRequired
// layoutAnimation: PropTypes.instanceOf(Date)
};
2017-08-05 18:16:32 +00:00
static navigationOptions = ({ navigation }) => ({
header: <Header subview={<RoomsHeader navigation={navigation} />} />
});
2017-08-04 00:34:37 +00:00
constructor(props) {
super(props);
this.rid =
props.rid ||
props.navigation.state.params.room.rid;
this.name = props.name ||
props.navigation.state.params.name ||
props.navigation.state.params.room.name;
this.rooms = database.objects('subscriptions').filtered('rid = $0', this.rid);
2017-08-07 18:42:02 +00:00
this.state = {
loaded: true,
joined: typeof props.rid === 'undefined',
room: {}
2017-08-07 18:42:02 +00:00
};
this.onReactionPress = this.onReactionPress.bind(this);
2017-08-07 00:34:35 +00:00
}
2017-08-04 00:34:37 +00:00
async componentWillMount() {
this.props.navigation.setParams({
title: this.name
});
this.updateRoom();
await this.props.openRoom({ rid: this.rid, name: this.name, ls: this.state.room.ls });
if (this.state.room.alert || this.state.room.unread || this.state.room.userMentions) {
this.props.setLastOpen(this.state.room.ls);
} else {
this.props.setLastOpen(null);
}
this.rooms.addListener(this.updateRoom);
}
// componentWillReceiveProps(nextProps) {
// // if (this.props.layoutAnimation !== nextProps.layoutAnimation) {
// // LayoutAnimation.spring();
// // }
// }
shouldComponentUpdate(nextProps, nextState) {
return !(equal(this.props, nextProps) && equal(this.state, nextState));
}
2017-08-07 00:34:35 +00:00
componentWillUnmount() {
clearTimeout(this.timer);
this.rooms.removeAllListeners();
2017-11-24 20:44:52 +00:00
this.props.editCancel();
2017-08-04 00:34:37 +00:00
}
onEndReached = (data) => {
if (this.props.loading || this.state.end) {
return;
}
if (!this.state.loaded) {
alert(2);
return;
2017-08-10 23:21:46 +00:00
}
2017-11-21 16:55:32 +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
});
}
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
updateRoom = () => {
this.setState({ room: this.rooms[0] });
}
sendMessage = (message) => {
RocketChat.sendMessage(this.rid, message).then(() => {
this.props.setLastOpen(null);
});
};
2017-08-09 01:40:55 +00:00
joinRoom = async() => {
await RocketChat.joinRoom(this.props.rid);
this.setState({
joined: true
});
2017-08-10 16:16:32 +00:00
};
renderItem = item => (
2017-08-09 01:40:55 +00:00
<Message
key={item._id}
2017-08-09 01:40:55 +00:00
item={item}
reactions={JSON.parse(JSON.stringify(item.reactions))}
2017-08-13 23:45:47 +00:00
baseUrl={this.props.Site_Url}
Message_TimeFormat={this.props.Message_TimeFormat}
user={this.props.user}
onReactionPress={this.onReactionPress}
2017-08-09 01:40:55 +00:00
/>
);
2017-08-07 00:34:35 +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>
);
}
if (this.state.room.ro) {
return (
<View style={styles.readOnly}>
<Text>This room is read only</Text>
</View>
);
}
return <MessageBox ref={box => (this.box = box)} onSubmit={this.sendMessage} rid={this.rid} />;
};
2017-08-09 20:08:50 +00:00
2017-08-10 23:21:46 +00:00
renderHeader = () => {
if (this.state.end) {
return <Text style={styles.loadingMore}>Start of conversation</Text>;
2017-08-10 23:21:46 +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}>
<Banner />
<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()}
{this.state.room._id ? <MessageActions room={this.state.room} /> : null}
<MessageErrorActions />
<ReactionPicker onEmojiSelected={this.onReactionPress} />
2018-01-25 14:04:20 +00:00
</View>
2017-08-04 00:34:37 +00:00
);
}
}