2017-09-21 17:08:00 +00:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { ScrollView, Text, View, StyleSheet, FlatList, TouchableHighlight } from 'react-native';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
|
2017-12-27 15:22:06 +00:00
|
|
|
import database from '../lib/realm';
|
2018-05-18 17:55:08 +00:00
|
|
|
import { setServer } from '../actions/server';
|
2017-11-07 20:25:04 +00:00
|
|
|
import { logout } from '../actions/login';
|
2017-09-21 17:08:00 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
scrollView: {
|
|
|
|
paddingTop: 20
|
|
|
|
},
|
|
|
|
imageContainer: {
|
|
|
|
width: '100%',
|
|
|
|
alignItems: 'center'
|
|
|
|
},
|
|
|
|
image: {
|
|
|
|
width: 200,
|
|
|
|
height: 200,
|
|
|
|
borderRadius: 100
|
|
|
|
},
|
|
|
|
serverTitle: {
|
|
|
|
fontSize: 16,
|
|
|
|
color: 'grey',
|
|
|
|
padding: 10,
|
|
|
|
width: '100%'
|
|
|
|
},
|
|
|
|
serverItem: {
|
|
|
|
backgroundColor: 'white',
|
|
|
|
padding: 10,
|
|
|
|
flex: 1
|
|
|
|
},
|
|
|
|
selectedServer: {
|
|
|
|
backgroundColor: '#eeeeee'
|
|
|
|
}
|
|
|
|
});
|
2018-03-02 21:31:44 +00:00
|
|
|
const keyExtractor = item => item.id;
|
2017-09-21 17:08:00 +00:00
|
|
|
@connect(state => ({
|
|
|
|
server: state.server.server
|
|
|
|
}), dispatch => ({
|
2017-11-07 20:25:04 +00:00
|
|
|
selectServer: server => dispatch(setServer(server)),
|
2018-05-18 17:55:08 +00:00
|
|
|
logout: () => dispatch(logout())
|
2017-09-21 17:08:00 +00:00
|
|
|
}))
|
|
|
|
export default class Sidebar extends Component {
|
|
|
|
static propTypes = {
|
|
|
|
server: PropTypes.string.isRequired,
|
|
|
|
selectServer: PropTypes.func.isRequired,
|
2017-11-07 20:25:04 +00:00
|
|
|
navigation: PropTypes.object.isRequired,
|
2018-05-18 17:55:08 +00:00
|
|
|
logout: PropTypes.func.isRequired
|
2017-09-21 17:08:00 +00:00
|
|
|
}
|
|
|
|
|
2018-03-06 17:40:44 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = { servers: [] };
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
database.databases.serversDB.addListener('change', this.updateState);
|
|
|
|
this.setState(this.getState());
|
|
|
|
}
|
|
|
|
|
2017-09-21 17:08:00 +00:00
|
|
|
componentWillUnmount() {
|
2017-12-27 15:34:39 +00:00
|
|
|
database.databases.serversDB.removeListener('change', this.updateState);
|
2017-09-21 17:08:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onItemPress = ({ route, focused }) => {
|
2018-04-10 13:03:54 +00:00
|
|
|
this.props.navigation.navigate({ key: 'DrawerClose', routeName: 'DrawerClose' });
|
2017-09-21 17:08:00 +00:00
|
|
|
if (!focused) {
|
|
|
|
this.props.navigation.navigate(route.routeName, undefined);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onPressItem = (item) => {
|
|
|
|
this.props.selectServer(item.id);
|
2018-04-10 13:03:54 +00:00
|
|
|
this.props.navigation.navigate({ key: 'DrawerClose', routeName: 'DrawerClose' });
|
2017-09-21 17:08:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getState = () => ({
|
2017-12-27 15:34:39 +00:00
|
|
|
servers: database.databases.serversDB.objects('servers')
|
2017-09-21 17:08:00 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
updateState = () => {
|
|
|
|
this.setState(this.getState());
|
|
|
|
}
|
|
|
|
|
|
|
|
renderItem = ({ item, separators }) => (
|
|
|
|
|
|
|
|
<TouchableHighlight
|
|
|
|
onShowUnderlay={separators.highlight}
|
|
|
|
onHideUnderlay={separators.unhighlight}
|
|
|
|
onPress={() => { this.onPressItem(item); }}
|
|
|
|
>
|
|
|
|
<View style={[styles.serverItem, (item.id === this.props.server ? styles.selectedServer : null)]}>
|
|
|
|
<Text>
|
|
|
|
{item.id}
|
|
|
|
</Text>
|
|
|
|
</View>
|
|
|
|
</TouchableHighlight>
|
|
|
|
);
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<ScrollView style={styles.scrollView}>
|
|
|
|
<View style={{ paddingBottom: 20 }}>
|
|
|
|
<FlatList
|
|
|
|
data={this.state.servers}
|
|
|
|
renderItem={this.renderItem}
|
2018-03-02 21:31:44 +00:00
|
|
|
keyExtractor={keyExtractor}
|
2017-09-21 17:08:00 +00:00
|
|
|
/>
|
2017-11-07 20:25:04 +00:00
|
|
|
<TouchableHighlight
|
|
|
|
onPress={() => { this.props.logout(); }}
|
|
|
|
>
|
|
|
|
<View style={styles.serverItem}>
|
|
|
|
<Text>
|
|
|
|
Logout
|
|
|
|
</Text>
|
|
|
|
</View>
|
|
|
|
</TouchableHighlight>
|
2017-11-13 13:53:45 +00:00
|
|
|
<TouchableHighlight
|
2018-05-18 17:55:08 +00:00
|
|
|
onPress={() => { this.props.navigation.navigate({ key: 'AddServer', routeName: 'AddServer' }); }}
|
2017-11-13 13:53:45 +00:00
|
|
|
>
|
|
|
|
<View style={styles.serverItem}>
|
|
|
|
<Text>
|
|
|
|
Add Server
|
|
|
|
</Text>
|
|
|
|
</View>
|
|
|
|
</TouchableHighlight>
|
2017-09-21 17:08:00 +00:00
|
|
|
</View>
|
|
|
|
</ScrollView>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|