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';
|
2018-08-01 19:35:06 +00:00
|
|
|
import { Text, ScrollView, View, Keyboard, SafeAreaView } from 'react-native';
|
2017-09-01 19:42:50 +00:00
|
|
|
import { connect } from 'react-redux';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2017-09-01 19:42:50 +00:00
|
|
|
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';
|
2018-01-16 18:48:05 +00:00
|
|
|
import scrollPersistTaps from '../utils/scrollPersistTaps';
|
2018-04-24 19:34:03 +00:00
|
|
|
import Button from '../containers/Button';
|
|
|
|
import TextInput from '../containers/TextInput';
|
|
|
|
import Loading from '../containers/Loading';
|
2018-04-26 17:33:43 +00:00
|
|
|
import LoggedView from './View';
|
2018-06-01 17:38:13 +00:00
|
|
|
import I18n from '../i18n';
|
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,
|
2018-04-24 19:34:03 +00:00
|
|
|
validating: state.server.connecting,
|
|
|
|
addingServer: state.server.adding
|
2017-09-01 19:42:50 +00:00
|
|
|
}), dispatch => ({
|
|
|
|
validateServer: url => dispatch(serverRequest(url)),
|
|
|
|
addServer: url => dispatch(addServer(url))
|
|
|
|
}))
|
2018-07-10 13:40:32 +00:00
|
|
|
/** @extends React.Component */
|
2018-04-26 17:33:43 +00:00
|
|
|
export default class NewServerView extends LoggedView {
|
2017-08-05 18:16:32 +00:00
|
|
|
static propTypes = {
|
2018-07-10 13:40:32 +00:00
|
|
|
navigator: PropTypes.object,
|
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,
|
2018-07-10 13:40:32 +00:00
|
|
|
addingServer: PropTypes.bool.isRequired
|
2017-08-05 18:16:32 +00:00
|
|
|
}
|
|
|
|
|
2017-08-03 18:23:43 +00:00
|
|
|
constructor(props) {
|
2018-04-26 17:33:43 +00:00
|
|
|
super('NewServerView', props);
|
2017-08-03 18:23:43 +00:00
|
|
|
this.state = {
|
2018-04-24 19:34:03 +00:00
|
|
|
defaultServer: 'https://open.rocket.chat'
|
2017-08-03 18:23:43 +00:00
|
|
|
};
|
2018-07-10 13:40:32 +00:00
|
|
|
props.validateServer(this.state.defaultServer); // Need to call because in case of submit with empty field
|
2017-08-12 20:52:55 +00:00
|
|
|
}
|
|
|
|
|
2018-08-01 19:35:06 +00:00
|
|
|
componentDidMount() {
|
|
|
|
setTimeout(() => {
|
|
|
|
this.input.focus();
|
|
|
|
}, 600);
|
|
|
|
}
|
|
|
|
|
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 = () => {
|
2018-05-30 16:51:30 +00:00
|
|
|
if (this.props.validInstance) {
|
|
|
|
Keyboard.dismiss();
|
|
|
|
this.props.addServer(this.completeUrl(this.state.text));
|
|
|
|
}
|
2017-09-21 17:08:00 +00:00
|
|
|
}
|
|
|
|
|
2017-08-11 15:47:18 +00:00
|
|
|
completeUrl = (url) => {
|
2018-04-24 19:34:03 +00:00
|
|
|
url = url && url.trim();
|
|
|
|
|
|
|
|
if (!url) {
|
|
|
|
return this.state.defaultServer;
|
|
|
|
}
|
2017-08-11 15:47:18 +00:00
|
|
|
|
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.props.validating) {
|
2017-08-11 15:47:18 +00:00
|
|
|
return (
|
|
|
|
<Text style={[styles.validateText, styles.validatingText]}>
|
2018-06-01 17:38:13 +00:00
|
|
|
{I18n.t('Validating')} {this.state.text || 'open'} ...
|
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]}>
|
2018-06-01 17:38:13 +00:00
|
|
|
{this.state.url} {I18n.t('is_a_valid_RocketChat_instance')}
|
2017-08-11 15:47:18 +00:00
|
|
|
</Text>
|
|
|
|
);
|
|
|
|
}
|
2017-09-01 19:42:50 +00:00
|
|
|
return (
|
|
|
|
<Text style={[styles.validateText, styles.invalidText]}>
|
2018-06-01 17:38:13 +00:00
|
|
|
{this.state.url} {I18n.t('is_not_a_valid_RocketChat_instance')}
|
2017-09-01 19:42:50 +00:00
|
|
|
</Text>
|
|
|
|
);
|
2017-08-03 18:23:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2018-04-24 19:34:03 +00:00
|
|
|
const { validInstance } = this.props;
|
2017-08-03 18:23:43 +00:00
|
|
|
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}
|
|
|
|
>
|
2018-04-24 19:34:03 +00:00
|
|
|
<ScrollView {...scrollPersistTaps} contentContainerStyle={styles.containerScrollView}>
|
2018-08-01 19:35:06 +00:00
|
|
|
<SafeAreaView style={styles.container} testID='new-server-view'>
|
2018-06-01 17:38:13 +00:00
|
|
|
<Text style={[styles.loginText, styles.loginTitle]}>{I18n.t('Sign_in_your_server')}</Text>
|
2018-04-24 19:34:03 +00:00
|
|
|
<TextInput
|
|
|
|
inputRef={e => this.input = e}
|
|
|
|
containerStyle={{ marginBottom: 5 }}
|
2018-06-01 17:38:13 +00:00
|
|
|
label={I18n.t('Your_server')}
|
2018-04-24 19:34:03 +00:00
|
|
|
placeholder={this.state.defaultServer}
|
|
|
|
returnKeyType='done'
|
|
|
|
onChangeText={this.onChangeText}
|
2018-05-23 13:39:18 +00:00
|
|
|
testID='new-server-view-input'
|
2018-05-18 17:55:08 +00:00
|
|
|
onSubmitEditing={this.submit}
|
2018-04-24 19:34:03 +00:00
|
|
|
/>
|
|
|
|
{this.renderValidation()}
|
|
|
|
<View style={[styles.alignItemsFlexStart, { marginTop: 20 }]}>
|
|
|
|
<Button
|
2018-06-01 17:38:13 +00:00
|
|
|
title={I18n.t('Connect')}
|
2018-04-24 19:34:03 +00:00
|
|
|
type='primary'
|
|
|
|
onPress={this.submit}
|
|
|
|
disabled={!validInstance}
|
2018-05-23 13:39:18 +00:00
|
|
|
testID='new-server-view-button'
|
2018-04-24 19:34:03 +00:00
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
<Loading visible={this.props.addingServer} />
|
2018-08-01 19:35:06 +00:00
|
|
|
</SafeAreaView>
|
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
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|