verdnatura-chat/app/views/room.js

117 lines
2.5 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';
2017-08-09 20:08:50 +00:00
import { Text, View, FlatList, StyleSheet } from 'react-native';
2017-08-09 01:40:55 +00:00
import realm from '../lib/realm';
2017-08-09 20:18:00 +00:00
import RocketChat from '../lib/rocketchat';
2017-08-04 00:34:37 +00:00
2017-08-09 01:40:55 +00:00
import Message from '../components/Message';
2017-08-09 13:12:00 +00:00
import MessageBox from '../components/MessageBox';
import KeyboardView from '../components/KeyboardView';
2017-08-04 00:34:37 +00:00
const styles = StyleSheet.create({
container: {
flex: 1
},
2017-08-07 00:34:35 +00:00
list: {
flex: 1,
transform: [{ scaleY: -1 }]
},
2017-08-04 00:34:37 +00:00
separator: {
height: 1,
backgroundColor: '#CED0CE'
2017-08-09 20:08:50 +00:00
},
bannerContainer: {
backgroundColor: 'orange'
},
bannerText: {
margin: 5,
textAlign: 'center',
color: '#a00'
2017-08-04 00:34:37 +00:00
}
});
2017-08-05 18:16:32 +00:00
export default class RoomView extends React.Component {
static propTypes = {
navigation: PropTypes.object.isRequired
}
2017-08-04 00:34:37 +00:00
static navigationOptions = ({ navigation }) => ({
title: realm.objectForPrimaryKey('subscriptions', navigation.state.params.sid).name
});
constructor(props) {
super(props);
this.rid = realm.objectForPrimaryKey('subscriptions', props.navigation.state.params.sid).rid;
// this.rid = 'GENERAL';
2017-08-07 18:42:02 +00:00
this.state = {
2017-08-09 20:08:50 +00:00
dataSource: this.getMessages(),
loaded: false
2017-08-07 18:42:02 +00:00
};
2017-08-07 00:34:35 +00:00
2017-08-07 18:42:02 +00:00
this.url = realm.objectForPrimaryKey('settings', 'Site_Url').value;
2017-08-07 00:34:35 +00:00
}
2017-08-04 00:34:37 +00:00
2017-08-07 18:42:02 +00:00
componentWillMount() {
2017-08-09 20:18:00 +00:00
RocketChat.loadMessagesForRoom(this.rid, () => {
2017-08-09 20:08:50 +00:00
this.setState({
...this.state,
loaded: true
});
});
2017-08-07 00:34:35 +00:00
realm.addListener('change', this.updateState);
}
2017-08-04 00:34:37 +00:00
2017-08-07 00:34:35 +00:00
componentWillUnmount() {
realm.removeListener('change', this.updateState);
2017-08-04 00:34:37 +00:00
}
2017-08-09 02:27:22 +00:00
getMessages = () => realm.objects('messages').filtered('_server.id = $0 AND rid = $1', RocketChat.currentServer, this.rid).sorted('ts', true)
2017-08-04 00:34:37 +00:00
2017-08-09 01:40:55 +00:00
updateState = () => {
this.setState({
2017-08-09 20:08:50 +00:00
...this.state,
2017-08-09 01:40:55 +00:00
dataSource: this.getMessages()
});
};
2017-08-04 00:34:37 +00:00
2017-08-09 20:18:00 +00:00
sendMessage = message => RocketChat.sendMessage(this.rid, message);
2017-08-09 01:40:55 +00:00
renderItem = ({ item }) => (
<Message
id={item._id}
item={item}
baseUrl={this.url}
/>
);
2017-08-07 00:34:35 +00:00
2017-08-09 20:08:50 +00:00
renderBanner = () => {
if (this.state.loaded === false) {
return (
<View style={styles.bannerContainer}>
<Text style={styles.bannerText}>Loading new messages...</Text>
</View>
);
}
}
2017-08-04 00:34:37 +00:00
render() {
return (
2017-08-09 13:12:00 +00:00
<KeyboardView style={styles.container} keyboardVerticalOffset={64}>
2017-08-09 20:08:50 +00:00
{this.renderBanner()}
2017-08-04 00:34:37 +00:00
<FlatList
2017-08-07 00:34:35 +00:00
ref={ref => this.listView = ref}
2017-08-04 00:34:37 +00:00
style={styles.list}
data={this.state.dataSource}
2017-08-07 00:34:35 +00:00
extraData={this.state}
2017-08-04 00:34:37 +00:00
renderItem={this.renderItem}
keyExtractor={item => item._id}
/>
2017-08-09 13:12:00 +00:00
<MessageBox
onSubmit={this.sendMessage}
/>
</KeyboardView>
2017-08-04 00:34:37 +00:00
);
}
}