vn-verdnaturachat/app/views/serverNew.js

91 lines
1.9 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 01:40:55 +00:00
import { TextInput, StyleSheet, KeyboardAvoidingView, Platform } from 'react-native';
2017-08-07 00:34:35 +00:00
2017-08-09 01:40:55 +00:00
import realm from '../lib/realm';
import { connect } from '../lib/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-05 18:16:32 +00:00
export default class NewServerView extends React.Component {
static propTypes = {
navigation: PropTypes.object.isRequired
}
2017-08-09 01:40:55 +00:00
static navigationOptions = () => ({
2017-08-07 00:34:35 +00:00
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-09 01:40:55 +00:00
navigate('ListServer', { newServer: url });
2017-08-03 18:23:43 +00:00
});
};
}
render() {
return (
2017-08-07 18:42:02 +00:00
<KeyboardAvoidingView style={styles.view} behavior={Platform.OS === 'ios' && '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
);
}
}