Rocket.Chat.ReactNative/app/views/RoomView/index.js

217 lines
6.0 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 LoggedView from '../View';
import { List } from './ListView';
import * as actions from '../../actions';
import { openRoom, setLastOpen } from '../../actions/room';
import { editCancel, toggleReactionPicker, actionsShow } 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 styles from './styles';
import log from '../../utils/log';
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
}),
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)),
actionsShow: actionMessage => dispatch(actionsShow(actionMessage))
})
)
export default class RoomView extends LoggedView {
2017-08-05 18:16:32 +00:00
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,
actionsShow: PropTypes.func
};
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('RoomView', props);
this.rid =
props.rid ||
props.navigation.state.params.room.rid;
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
2018-05-23 13:39:18 +00:00
componentDidMount() {
this.updateRoom();
this.rooms.addListener(this.updateRoom);
}
shouldComponentUpdate(nextProps, nextState) {
return !(equal(this.props, nextProps) && equal(this.state, nextState) && this.state.room.ro === nextState.room.ro);
}
2017-08-07 00:34:35 +00:00
componentWillUnmount() {
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;
}
2017-11-21 16:55:32 +00:00
requestAnimationFrame(() => {
const lastRowData = data[data.length - 1];
if (!lastRowData) {
return;
}
Beta (#265) * Fabric iOS * Fabric configured on iOS and Android * - react-native-fabric configured - login tracked * README updated * Run scripts from README updated * README scripts * get rooms and messages by rest * user status * more improves * more improves * send pong on timeout * fix some methods * more tests * rest messages * Room actions (#266) * Toggle notifications * Search messages * Invite users * Mute/Unmute users in room * rocket.cat messages * Room topic layout fixed * Starred messages loading onEndReached * Room actions onEndReached * Unnecessary login request * Login loading * Login services fixed * User presence layout * ïmproves on room actions view * Removed unnecessary data from SelectedUsersView * load few messages on open room, search message improve * fix loading messages forever * Removed state from search * Custom message time format * secureTextEntry layout * Reduce android app size * Roles subscription fix * Public routes navigation * fix reconnect * - New login/register, login, register * proguard * Login flux * App init/restore * Android layout fixes * Multiple meteor connection requests fixed * Nested attachments * Nested attachments * fix check status * New login layout (#269) * Public routes navigation * New login/register, login, register * Multiple meteor connection requests fixed * Nested attachments * Button component * TextInput android layout fixed * Register fixed * Thinner close modal button * Requests /me after login only one time * Static images moved * fix reconnect * fix ddp * fix custom emoji * New message layout (#273) * Grouping messages * Message layout * Users typing animation * Image attachment layout
2018-04-24 19:34:03 +00:00
// TODO: fix
RocketChat.loadMessagesForRoom({ rid: this.rid, t: this.state.room.t, latest: lastRowData.ts }, ({ end }) => end && this.setState({
end
}));
2017-08-09 01:40:55 +00:00
});
}
onMessageLongPress = (message) => {
this.props.actionsShow(message);
}
onReactionPress = (shortname, messageId) => {
try {
if (!messageId) {
RocketChat.setReaction(shortname, this.props.actionMessage._id);
return this.props.toggleReactionPicker();
}
RocketChat.setReaction(shortname, messageId);
} catch (e) {
log('RoomView.onReactionPress', e);
}
};
2017-08-04 00:34:37 +00:00
updateRoom = async() => {
if (this.rooms.length > 0) {
const { room: prevRoom } = this.state;
await this.setState({ room: JSON.parse(JSON.stringify(this.rooms[0])) });
if (!prevRoom.rid) {
await this.props.openRoom({
...this.state.room
});
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);
}
}
}
}
sendMessage = (message) => {
RocketChat.sendMessage(this.rid, message).then(() => {
this.props.setLastOpen(null);
});
};
2017-08-09 01:40:55 +00:00
joinRoom = async() => {
try {
await RocketChat.joinRoom(this.props.rid);
this.setState({
joined: true
});
} catch (e) {
log('joinRoom', e);
}
2017-08-10 16:16:32 +00:00
};
Beta (#265) * Fabric iOS * Fabric configured on iOS and Android * - react-native-fabric configured - login tracked * README updated * Run scripts from README updated * README scripts * get rooms and messages by rest * user status * more improves * more improves * send pong on timeout * fix some methods * more tests * rest messages * Room actions (#266) * Toggle notifications * Search messages * Invite users * Mute/Unmute users in room * rocket.cat messages * Room topic layout fixed * Starred messages loading onEndReached * Room actions onEndReached * Unnecessary login request * Login loading * Login services fixed * User presence layout * ïmproves on room actions view * Removed unnecessary data from SelectedUsersView * load few messages on open room, search message improve * fix loading messages forever * Removed state from search * Custom message time format * secureTextEntry layout * Reduce android app size * Roles subscription fix * Public routes navigation * fix reconnect * - New login/register, login, register * proguard * Login flux * App init/restore * Android layout fixes * Multiple meteor connection requests fixed * Nested attachments * Nested attachments * fix check status * New login layout (#269) * Public routes navigation * New login/register, login, register * Multiple meteor connection requests fixed * Nested attachments * Button component * TextInput android layout fixed * Register fixed * Thinner close modal button * Requests /me after login only one time * Static images moved * fix reconnect * fix ddp * fix custom emoji * New message layout (#273) * Grouping messages * Message layout * Users typing animation * Image attachment layout
2018-04-24 19:34:03 +00:00
renderItem = (item, previousItem) => (
2017-08-09 01:40:55 +00:00
<Message
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}
reactions={JSON.parse(JSON.stringify(item.reactions))}
user={this.props.user}
onReactionPress={this.onReactionPress}
onLongPress={this.onMessageLongPress}
archived={this.state.room.archived}
Beta (#265) * Fabric iOS * Fabric configured on iOS and Android * - react-native-fabric configured - login tracked * README updated * Run scripts from README updated * README scripts * get rooms and messages by rest * user status * more improves * more improves * send pong on timeout * fix some methods * more tests * rest messages * Room actions (#266) * Toggle notifications * Search messages * Invite users * Mute/Unmute users in room * rocket.cat messages * Room topic layout fixed * Starred messages loading onEndReached * Room actions onEndReached * Unnecessary login request * Login loading * Login services fixed * User presence layout * ïmproves on room actions view * Removed unnecessary data from SelectedUsersView * load few messages on open room, search message improve * fix loading messages forever * Removed state from search * Custom message time format * secureTextEntry layout * Reduce android app size * Roles subscription fix * Public routes navigation * fix reconnect * - New login/register, login, register * proguard * Login flux * App init/restore * Android layout fixes * Multiple meteor connection requests fixed * Nested attachments * Nested attachments * fix check status * New login layout (#269) * Public routes navigation * New login/register, login, register * Multiple meteor connection requests fixed * Nested attachments * Button component * TextInput android layout fixed * Register fixed * Thinner close modal button * Requests /me after login only one time * Static images moved * fix reconnect * fix ddp * fix custom emoji * New message layout (#273) * Grouping messages * Message layout * Users typing animation * Image attachment layout
2018-04-24 19:34:03 +00:00
previousItem={previousItem}
2017-08-09 01:40:55 +00:00
/>
);
2017-08-07 00:34:35 +00:00
Beta (#265) * Fabric iOS * Fabric configured on iOS and Android * - react-native-fabric configured - login tracked * README updated * Run scripts from README updated * README scripts * get rooms and messages by rest * user status * more improves * more improves * send pong on timeout * fix some methods * more tests * rest messages * Room actions (#266) * Toggle notifications * Search messages * Invite users * Mute/Unmute users in room * rocket.cat messages * Room topic layout fixed * Starred messages loading onEndReached * Room actions onEndReached * Unnecessary login request * Login loading * Login services fixed * User presence layout * ïmproves on room actions view * Removed unnecessary data from SelectedUsersView * load few messages on open room, search message improve * fix loading messages forever * Removed state from search * Custom message time format * secureTextEntry layout * Reduce android app size * Roles subscription fix * Public routes navigation * fix reconnect * - New login/register, login, register * proguard * Login flux * App init/restore * Android layout fixes * Multiple meteor connection requests fixed * Nested attachments * Nested attachments * fix check status * New login layout (#269) * Public routes navigation * New login/register, login, register * Multiple meteor connection requests fixed * Nested attachments * Button component * TextInput android layout fixed * Register fixed * Thinner close modal button * Requests /me after login only one time * Static images moved * fix reconnect * fix ddp * fix custom emoji * New message layout (#273) * Grouping messages * Message layout * Users typing animation * Image attachment layout
2018-04-24 19:34:03 +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 || this.state.room.archived) {
return (
<View style={styles.readOnly}>
<Text>This room is read only</Text>
</View>
);
}
return <MessageBox 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-05-23 13:39:18 +00:00
<View style={styles.container} testID='room-view'>
<List
key='room-view-messages'
end={this.state.end}
room={this.rid}
renderFooter={this.renderHeader}
onEndReached={this.onEndReached}
Beta (#265) * Fabric iOS * Fabric configured on iOS and Android * - react-native-fabric configured - login tracked * README updated * Run scripts from README updated * README scripts * get rooms and messages by rest * user status * more improves * more improves * send pong on timeout * fix some methods * more tests * rest messages * Room actions (#266) * Toggle notifications * Search messages * Invite users * Mute/Unmute users in room * rocket.cat messages * Room topic layout fixed * Starred messages loading onEndReached * Room actions onEndReached * Unnecessary login request * Login loading * Login services fixed * User presence layout * ïmproves on room actions view * Removed unnecessary data from SelectedUsersView * load few messages on open room, search message improve * fix loading messages forever * Removed state from search * Custom message time format * secureTextEntry layout * Reduce android app size * Roles subscription fix * Public routes navigation * fix reconnect * - New login/register, login, register * proguard * Login flux * App init/restore * Android layout fixes * Multiple meteor connection requests fixed * Nested attachments * Nested attachments * fix check status * New login layout (#269) * Public routes navigation * New login/register, login, register * Multiple meteor connection requests fixed * Nested attachments * Button component * TextInput android layout fixed * Register fixed * Thinner close modal button * Requests /me after login only one time * Static images moved * fix reconnect * fix ddp * fix custom emoji * New message layout (#273) * Grouping messages * Message layout * Users typing animation * Image attachment layout
2018-04-24 19:34:03 +00:00
renderRow={this.renderItem}
/>
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
);
}
}