vn-verdnaturachat/app/servers/new.js

95 lines
2.0 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-07 00:34:35 +00:00
import Zeroconf from 'react-native-zeroconf';
import { View, Text, TextInput, Button, StyleSheet, KeyboardAvoidingView } from 'react-native';
import { NavigationActions } from 'react-navigation'
import realm from '../realm';
import { connect } from '../meteor';
2017-08-03 18:23:43 +00:00
const styles = StyleSheet.create({
view: {
flex: 1,
2017-08-07 00:34:35 +00:00
flexDirection: 'column',
2017-08-03 18:23:43 +00:00
justifyContent: 'center',
2017-08-07 00:34:35 +00:00
alignItems: 'stretch'
2017-08-03 18:23:43 +00:00
},
input: {
height: 40,
borderColor: '#aaa',
margin: 20,
padding: 5,
borderWidth: 0,
backgroundColor: '#f8f8f8'
2017-08-07 00:34:35 +00:00
},
text: {
textAlign: 'center',
color: '#888'
2017-08-03 18:23:43 +00:00
}
});
2017-08-07 00:34:35 +00:00
const zeroconf = new Zeroconf();
2017-08-03 18:23:43 +00:00
2017-08-05 18:16:32 +00:00
export default class NewServerView extends React.Component {
static propTypes = {
navigation: PropTypes.object.isRequired
}
2017-08-07 00:34:35 +00:00
static navigationOptions = ({navigation}) => ({
title: 'New Server Connection'
});
2017-08-03 18:23:43 +00:00
constructor(props) {
super(props);
this.state = {
2017-08-07 00:34:35 +00:00
defaultServer: 'https://demo.rocket.chat',
2017-08-03 18:23:43 +00:00
text: ''
};
const { navigate } = this.props.navigation;
this.submit = () => {
let url = this.state.text.trim();
if (!url) {
2017-08-07 00:34:35 +00:00
url = this.state.defaultServer;
2017-08-03 18:23:43 +00:00
}
// TODO: validate URL
2017-08-07 00:34:35 +00:00
if (url.indexOf('.') === -1) {
url = `https://${ url }.rocket.chat`;
}
if (/^https?:\/\//.test(url) === false) {
url = `https://${ url }`;
}
2017-08-03 18:23:43 +00:00
realm.write(() => {
2017-08-05 18:16:32 +00:00
realm.objects('servers').filtered('current = true').forEach(item => (item.current = false));
realm.create('servers', { id: url, current: true }, true);
2017-08-03 18:23:43 +00:00
});
connect(() => {
2017-08-07 00:34:35 +00:00
navigate('ListServer', {newServer: url});
2017-08-03 18:23:43 +00:00
});
};
}
render() {
return (
2017-08-07 00:34:35 +00:00
<KeyboardAvoidingView style={styles.view} behavior='padding'>
2017-08-03 18:23:43 +00:00
<TextInput
style={styles.input}
2017-08-05 18:16:32 +00:00
onChangeText={text => this.setState({ text })}
2017-08-03 18:23:43 +00:00
keyboardType='url'
autoCorrect={false}
returnKeyType='done'
autoCapitalize='none'
2017-08-05 18:16:32 +00:00
autoFocus
2017-08-03 18:23:43 +00:00
onSubmitEditing={this.submit}
2017-08-07 00:34:35 +00:00
placeholder={this.state.defaultServer}
2017-08-05 18:16:32 +00:00
/>
2017-08-07 00:34:35 +00:00
</KeyboardAvoidingView>
2017-08-03 18:23:43 +00:00
);
}
}