Rocket.Chat.ReactNative/app/room.js

175 lines
3.6 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-07 00:34:35 +00:00
import { View, KeyboardAvoidingView, Text, TextInput, FlatList, StyleSheet, Image } from 'react-native';
2017-08-05 18:16:32 +00:00
// import Markdown from 'react-native-simple-markdown';
2017-08-04 00:34:37 +00:00
import realm from './realm';
2017-08-07 00:34:35 +00:00
import { loadMessagesForRoom, sendMessage } from './meteor';
2017-08-04 00:34:37 +00:00
const styles = StyleSheet.create({
roomItem: {
borderColor: '#aaa',
padding: 14,
2017-08-07 00:34:35 +00:00
flexDirection: 'row',
transform: [{ scaleY: -1 }]
2017-08-04 00:34:37 +00:00
},
avatar: {
backgroundColor: '#ccc',
width: 40,
height: 40,
marginRight: 10,
borderRadius: 5
},
username: {
fontWeight: 'bold',
marginBottom: 5
},
2017-08-07 00:34:35 +00:00
texts: {
flex: 1
},
msg: {
flex: 1
},
2017-08-04 00:34:37 +00:00
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,
// width: "86%",
backgroundColor: '#CED0CE'
// marginLeft: "14%"
2017-08-07 00:34:35 +00:00
},
textBox: {
paddingTop: 1,
backgroundColor: '#ccc'
},
textBoxInput: {
height: 40,
backgroundColor: '#fff',
paddingLeft: 15
2017-08-04 00:34:37 +00:00
}
});
class RoomItem extends React.PureComponent {
2017-08-05 18:16:32 +00:00
static propTypes = {
item: PropTypes.object.isRequired
}
2017-08-04 00:34:37 +00:00
render() {
2017-08-07 00:34:35 +00:00
const extraStyle = {};
if (this.props.item.temp) {
extraStyle.opacity = .3;
}
2017-08-04 00:34:37 +00:00
return (
2017-08-07 00:34:35 +00:00
<View style={[styles.roomItem, extraStyle]}>
2017-08-05 18:16:32 +00:00
<Image style={styles.avatar} source={{ uri: `http://localhost:3000/avatar/${ this.props.item.u.username }` }} />
2017-08-07 00:34:35 +00:00
<View style={styles.texts}>
2017-08-04 00:34:37 +00:00
<Text onPress={this._onPress} style={styles.username}>
{this.props.item.u.username}
</Text>
2017-08-07 00:34:35 +00:00
<Text style={styles.msg}>
2017-08-05 18:16:32 +00:00
{this.props.item.msg}
</Text>
{/* <Markdown whitelist={['link', 'url']}>
2017-08-04 00:34:37 +00:00
{this.props.item.msg}
2017-08-05 18:16:32 +00:00
</Markdown> */}
2017-08-04 00:34:37 +00:00
</View>
</View>
);
}
}
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 00:34:35 +00:00
this.state = this.getState();
2017-08-04 00:34:37 +00:00
loadMessagesForRoom(this.rid);
2017-08-07 00:34:35 +00:00
this.state = this.getState();
}
2017-08-04 00:34:37 +00:00
2017-08-07 00:34:35 +00:00
getState = () => ({
...this.state,
dataSource: realm.objects('messages').filtered('rid = $0', this.rid).sorted('ts', true)
});
updateState = () => (this.setState(this.getState()))
componentDidMount() {
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-05 18:16:32 +00:00
renderItem = ({ item }) => (
<RoomItem
id={item._id}
item={item}
/>
);
2017-08-04 00:34:37 +00:00
2017-08-05 18:16:32 +00:00
renderSeparator = () => (
<View style={styles.separator} />
);
2017-08-04 00:34:37 +00:00
2017-08-07 00:34:35 +00:00
submit = () => {
console.log(this.state.text);
if (this.state.text.trim() === '') {
return;
}
sendMessage(this.rid, this.state.text);
this.setState({
...this.state,
text: ''
});
}
2017-08-04 00:34:37 +00:00
render() {
return (
2017-08-07 00:34:35 +00:00
<KeyboardAvoidingView style={styles.container} behavior='padding' keyboardVerticalOffset={64}>
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}
ItemSeparatorComponent={this.renderSeparator}
/>
2017-08-07 00:34:35 +00:00
<View style={styles.textBox}>
<TextInput
style={styles.textBoxInput}
value={this.state.text}
onChangeText={text => this.setState({ text })}
returnKeyType='send'
onSubmitEditing={this.submit}
autoFocus
placeholder='New message'
></TextInput>
</View>
</KeyboardAvoidingView>
2017-08-04 00:34:37 +00:00
);
}
}