Rocket.Chat.ReactNative/app/rooms.js

93 lines
1.7 KiB
JavaScript
Raw Normal View History

2017-08-03 18:23:43 +00:00
import React from 'react';
2017-08-05 18:16:32 +00:00
import PropTypes from 'prop-types';
2017-08-03 18:23:43 +00:00
import { View, Text, FlatList, StyleSheet } from 'react-native';
import realm from './realm';
const styles = StyleSheet.create({
roomItem: {
lineHeight: 18,
borderTopWidth: 2,
borderColor: '#aaa',
padding: 14
},
container: {
flex: 1
},
separator: {
height: 1,
// width: "86%",
backgroundColor: '#CED0CE'
// marginLeft: "14%"
}
});
class RoomItem extends React.PureComponent {
2017-08-05 18:16:32 +00:00
static propTypes = {
onPressItem: PropTypes.func.isRequired,
title: PropTypes.string.isRequired,
id: PropTypes.string.isRequired
}
2017-08-03 18:23:43 +00:00
_onPress = () => {
this.props.onPressItem(this.props.id);
};
render() {
return (
2017-08-05 18:16:32 +00:00
<Text onPress={this._onPress} style={styles.roomItem}>{ this.props.title }</Text>
2017-08-03 18:23:43 +00:00
);
}
}
2017-08-05 18:16:32 +00:00
export default class RoomsView extends React.Component {
static propTypes = {
navigation: PropTypes.object.isRequired
2017-08-03 18:23:43 +00:00
}
constructor(props) {
super(props);
2017-08-05 18:16:32 +00:00
const getState = () => ({
selected: new Map(),
dataSource: realm.objects('subscriptions')
});
2017-08-03 18:23:43 +00:00
realm.addListener('change', () => this.setState(getState()));
this.state = getState();
}
2017-08-05 18:16:32 +00:00
_onPressItem = (id) => {
const { navigate } = this.props.navigation;
console.log('pressed', id);
navigate('Room', { sid: id });
}
2017-08-03 18:23:43 +00:00
2017-08-05 18:16:32 +00:00
renderItem = ({ item }) => (
<RoomItem
id={item._id}
onPressItem={this._onPressItem}
title={item.name}
/>
);
renderSeparator = () => (
<View style={styles.separator} />
);
2017-08-03 18:23:43 +00:00
render() {
return (
<View style={styles.container}>
<FlatList
style={styles.list}
data={this.state.dataSource}
renderItem={this.renderItem}
keyExtractor={item => item._id}
ItemSeparatorComponent={this.renderSeparator}
/>
</View>
);
}
}