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-12-20 19:20:06 +00:00
|
|
|
import { Text, TouchableOpacity, ScrollView, TextInput } from 'react-native';
|
2017-09-01 19:42:50 +00:00
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { serverRequest, addServer } from '../actions/server';
|
2017-09-01 19:17:53 +00:00
|
|
|
import KeyboardView from '../presentation/KeyboardView';
|
2017-12-20 19:20:06 +00:00
|
|
|
import styles from './Styles';
|
2017-08-09 13:12:00 +00:00
|
|
|
|
2017-09-01 19:42:50 +00:00
|
|
|
@connect(state => ({
|
2017-12-20 19:20:06 +00:00
|
|
|
validInstance: !state.server.failure && !state.server.connecting,
|
2017-09-01 19:42:50 +00:00
|
|
|
validating: state.server.connecting
|
|
|
|
}), dispatch => ({
|
|
|
|
validateServer: url => dispatch(serverRequest(url)),
|
|
|
|
addServer: url => dispatch(addServer(url))
|
|
|
|
}))
|
2017-08-05 18:16:32 +00:00
|
|
|
export default class NewServerView extends React.Component {
|
|
|
|
static propTypes = {
|
2017-09-01 19:42:50 +00:00
|
|
|
validateServer: PropTypes.func.isRequired,
|
|
|
|
addServer: PropTypes.func.isRequired,
|
|
|
|
validating: PropTypes.bool.isRequired,
|
2017-09-21 17:08:00 +00:00
|
|
|
validInstance: PropTypes.bool.isRequired,
|
|
|
|
navigation: PropTypes.object.isRequired
|
2017-08-05 18:16:32 +00:00
|
|
|
}
|
|
|
|
|
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-11-07 20:25:04 +00:00
|
|
|
defaultServer: 'https://open.rocket.chat',
|
2017-08-11 15:47:18 +00:00
|
|
|
editable: true,
|
2017-08-03 18:23:43 +00:00
|
|
|
text: ''
|
|
|
|
};
|
2017-09-21 17:08:00 +00:00
|
|
|
this.adding = false;
|
|
|
|
this.props.validateServer(this.state.defaultServer); // Need to call because in case of submit with empty field
|
2017-08-11 15:47:18 +00:00
|
|
|
}
|
|
|
|
|
2017-09-21 17:08:00 +00:00
|
|
|
componentDidUpdate() {
|
|
|
|
if (this.adding) {
|
|
|
|
if (!this.props.validInstance) {
|
|
|
|
/* eslint-disable react/no-did-update-set-state */
|
|
|
|
this.setState({ editable: true });
|
|
|
|
this.adding = false;
|
|
|
|
}
|
2017-12-20 19:20:06 +00:00
|
|
|
if (this.props.validInstance) {
|
2017-09-21 17:08:00 +00:00
|
|
|
this.props.navigation.goBack();
|
|
|
|
this.adding = false;
|
2017-08-12 20:52:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-11 15:47:18 +00:00
|
|
|
onChangeText = (text) => {
|
|
|
|
this.setState({ text });
|
2017-09-01 19:42:50 +00:00
|
|
|
this.props.validateServer(this.completeUrl(text));
|
2017-08-11 15:47:18 +00:00
|
|
|
}
|
2017-09-21 17:08:00 +00:00
|
|
|
|
|
|
|
submit = () => {
|
|
|
|
this.setState({ editable: false });
|
|
|
|
this.adding = true;
|
|
|
|
this.props.addServer(this.completeUrl(this.state.text.trim() || this.state.defaultServer));
|
|
|
|
}
|
|
|
|
|
2017-08-11 15:47:18 +00:00
|
|
|
completeUrl = (url) => {
|
|
|
|
url = url.trim();
|
|
|
|
|
2017-09-21 17:08:00 +00:00
|
|
|
if (/^(\w|[0-9-_]){3,}$/.test(url) &&
|
|
|
|
/^(htt(ps?)?)|(loca((l)?|(lh)?|(lho)?|(lhos)?|(lhost:?\d*)?)$)/.test(url) === false) {
|
2017-08-11 15:47:18 +00:00
|
|
|
url = `${ url }.rocket.chat`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (/^(https?:\/\/)?(((\w|[0-9])+(\.(\w|[0-9-_])+)+)|localhost)(:\d+)?$/.test(url)) {
|
|
|
|
if (/^localhost(:\d+)?/.test(url)) {
|
|
|
|
url = `http://${ url }`;
|
|
|
|
} else if (/^https?:\/\//.test(url) === false) {
|
2017-08-07 00:34:35 +00:00
|
|
|
url = `https://${ url }`;
|
|
|
|
}
|
2017-08-11 15:47:18 +00:00
|
|
|
}
|
2017-08-03 18:23:43 +00:00
|
|
|
|
2017-08-15 19:28:46 +00:00
|
|
|
return url.replace(/\/+$/, '');
|
2017-08-11 15:47:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
renderValidation = () => {
|
2017-09-01 19:42:50 +00:00
|
|
|
if (!this.state.text.trim()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (this.props.validating) {
|
2017-08-11 15:47:18 +00:00
|
|
|
return (
|
|
|
|
<Text style={[styles.validateText, styles.validatingText]}>
|
2017-12-20 19:20:06 +00:00
|
|
|
Validating {this.state.text} ...
|
2017-08-11 15:47:18 +00:00
|
|
|
</Text>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-09-01 19:42:50 +00:00
|
|
|
if (this.props.validInstance) {
|
2017-08-11 15:47:18 +00:00
|
|
|
return (
|
|
|
|
<Text style={[styles.validateText, styles.validText]}>
|
|
|
|
{this.state.url} is a valid Rocket.Chat instance
|
|
|
|
</Text>
|
|
|
|
);
|
|
|
|
}
|
2017-09-01 19:42:50 +00:00
|
|
|
return (
|
|
|
|
<Text style={[styles.validateText, styles.invalidText]}>
|
|
|
|
{this.state.url} is not a valid Rocket.Chat instance
|
|
|
|
</Text>
|
|
|
|
);
|
2017-08-03 18:23:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2017-11-07 16:28:02 +00:00
|
|
|
<KeyboardView
|
2017-12-20 19:20:06 +00:00
|
|
|
contentContainerStyle={styles.container}
|
2017-11-07 16:28:02 +00:00
|
|
|
keyboardVerticalOffset={128}
|
|
|
|
>
|
2017-12-20 19:20:06 +00:00
|
|
|
<ScrollView
|
|
|
|
style={styles.loginView}
|
|
|
|
keyboardDismissMode='interactive'
|
|
|
|
keyboardShouldPersistTaps='always'
|
|
|
|
>
|
|
|
|
<TextInput
|
|
|
|
ref={ref => this.inputElement = ref}
|
|
|
|
style={styles.input_white}
|
|
|
|
onChangeText={this.onChangeText}
|
|
|
|
keyboardType='url'
|
|
|
|
autoCorrect={false}
|
|
|
|
returnKeyType='done'
|
|
|
|
autoCapitalize='none'
|
|
|
|
autoFocus
|
|
|
|
editable={this.state.editable}
|
|
|
|
placeholder={this.state.defaultServer}
|
|
|
|
underlineColorAndroid='transparent'
|
|
|
|
/>
|
|
|
|
<TouchableOpacity
|
|
|
|
disabled={!this.props.validInstance}
|
|
|
|
style={[styles.buttonContainer, this.props.validInstance ? null
|
|
|
|
: styles.disabledButton]}
|
|
|
|
onPress={this.submit}
|
|
|
|
>
|
|
|
|
<Text style={styles.button} accessibilityTraits='button'>Add</Text>
|
|
|
|
</TouchableOpacity>
|
2017-08-11 15:47:18 +00:00
|
|
|
{this.renderValidation()}
|
2017-12-20 19:20:06 +00:00
|
|
|
</ScrollView>
|
2017-08-09 13:12:00 +00:00
|
|
|
</KeyboardView>
|
2017-08-03 18:23:43 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|