vn-verdnaturachat/app/views/roomsList.js

179 lines
3.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-09 16:19:17 +00:00
import { Text, View, FlatList, StyleSheet } from 'react-native';
import Meteor from 'react-native-meteor';
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-03 18:23:43 +00:00
2017-08-09 01:40:55 +00:00
import RoomItem from '../components/RoomItem';
2017-08-03 18:23:43 +00:00
const styles = StyleSheet.create({
container: {
2017-08-09 16:19:17 +00:00
flex: 1,
2017-08-09 19:11:00 +00:00
alignItems: 'stretch',
2017-08-09 16:19:17 +00:00
justifyContent: 'center'
2017-08-03 18:23:43 +00:00
},
separator: {
height: 1,
backgroundColor: '#CED0CE'
2017-08-09 16:19:17 +00:00
},
list: {
width: '100%'
},
2017-08-09 20:08:50 +00:00
emptyView: {
flexGrow: 1,
alignItems: 'stretch',
justifyContent: 'center'
},
2017-08-09 16:19:17 +00:00
emptyText: {
textAlign: 'center',
fontSize: 18,
color: '#ccc'
2017-08-09 19:11:00 +00:00
},
bannerContainer: {
backgroundColor: '#ddd'
},
bannerText: {
2017-08-09 20:08:50 +00:00
textAlign: 'center',
margin: 5
2017-08-03 18:23:43 +00:00
}
});
2017-08-09 16:19:17 +00:00
let navigation;
Meteor.getData().on('loggingIn', () => {
setTimeout(() => {
if (Meteor._isLoggingIn === false && Meteor.userId() == null) {
console.log('loggingIn', Meteor.userId());
navigation.navigate('Login');
}
}, 100);
});
Meteor.Accounts.onLogin(() => {
console.log('onLogin');
});
2017-08-09 01:40:55 +00:00
export default class RoomsListView extends React.Component {
2017-08-05 18:16:32 +00:00
static propTypes = {
navigation: PropTypes.object.isRequired
2017-08-03 18:23:43 +00:00
}
2017-08-10 14:59:07 +00:00
static navigationOptions = () => {
const server = RocketChat.currentServer ? RocketChat.currentServer.replace(/^https?:\/\//, '') : '';
return {
title: <View style={{ height: 10, width: 200, top: -20 }}>
<Text style={{ textAlign: 'center', fontSize: 16, fontWeight: '600', top: -5 }}>Channels</Text>
<Text style={{ textAlign: 'center', fontSize: 10, top: -5 }}>{server}</Text>
</View>
};
}
2017-08-09 16:19:17 +00:00
2017-08-03 18:23:43 +00:00
constructor(props) {
super(props);
2017-08-09 16:19:17 +00:00
this.state = this.getState();
}
componentWillMount() {
realm.addListener('change', this.updateState);
navigation = this.props.navigation;
2017-08-09 18:01:54 +00:00
if (RocketChat.currentServer) {
2017-08-09 20:18:00 +00:00
RocketChat.connect();
2017-08-09 16:19:17 +00:00
} else {
navigation.navigate('ListServerModal');
}
}
componentWillUnmount() {
realm.removeListener('change', this.updateState);
}
getState = () => ({
2017-08-09 18:01:54 +00:00
dataSource: realm.objects('subscriptions').filtered('_server.id = $0', RocketChat.currentServer).sorted('name').slice()
.sort((a, b) => {
if (a.unread < b.unread) {
return 1;
}
if (a.unread > b.unread) {
return -1;
}
return 0;
})
2017-08-09 16:19:17 +00:00
})
updateState = () => {
this.setState(this.getState());
2017-08-07 18:42:02 +00:00
}
2017-08-03 18:23:43 +00:00
2017-08-05 18:16:32 +00:00
_onPressItem = (id) => {
const { navigate } = this.props.navigation;
navigate('Room', { sid: id });
}
2017-08-03 18:23:43 +00:00
2017-08-09 19:11:00 +00:00
renderBanner = () => {
2017-08-09 20:08:50 +00:00
const status = Meteor.getData() && Meteor.getData().ddp && Meteor.getData().ddp.status;
if (status === 'disconnected') {
2017-08-09 19:11:00 +00:00
return (
2017-08-09 20:08:50 +00:00
<View style={[styles.bannerContainer, { backgroundColor: '#0d0' }]}>
<Text style={[styles.bannerText, { color: '#fff' }]}>Connecting...</Text>
2017-08-09 19:11:00 +00:00
</View>
);
}
2017-08-09 20:08:50 +00:00
if (status === 'connected' && Meteor._isLoggingIn) {
2017-08-09 19:11:00 +00:00
return (
2017-08-09 20:08:50 +00:00
<View style={[styles.bannerContainer, { backgroundColor: 'orange' }]}>
<Text style={[styles.bannerText, { color: '#a00' }]}>Authenticating...</Text>
2017-08-09 19:11:00 +00:00
</View>
2017-08-09 20:18:00 +00:00
);
2017-08-09 19:11:00 +00:00
}
}
2017-08-05 18:16:32 +00:00
renderItem = ({ item }) => (
<RoomItem
id={item._id}
onPressItem={this._onPressItem}
2017-08-09 18:01:54 +00:00
item={item}
2017-08-05 18:16:32 +00:00
/>
);
renderSeparator = () => (
<View style={styles.separator} />
);
2017-08-03 18:23:43 +00:00
2017-08-09 16:19:17 +00:00
renderList = () => {
if (this.state.dataSource.length) {
return (
2017-08-03 18:23:43 +00:00
<FlatList
style={styles.list}
data={this.state.dataSource}
renderItem={this.renderItem}
keyExtractor={item => item._id}
ItemSeparatorComponent={this.renderSeparator}
/>
2017-08-09 16:19:17 +00:00
);
}
return (
2017-08-09 20:08:50 +00:00
<View style={styles.emptyView}>
<Text style={styles.emptyText}>No rooms</Text>
</View>
2017-08-09 16:19:17 +00:00
);
}
render() {
return (
<View style={styles.container}>
2017-08-09 19:11:00 +00:00
{this.renderBanner()}
2017-08-09 16:19:17 +00:00
{this.renderList()}
2017-08-03 18:23:43 +00:00
</View>
);
}
}