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-09-25 19:28:42 +00:00
|
|
|
import {
|
2018-12-21 10:55:35 +00:00
|
|
|
Text, ScrollView, Keyboard, Image, StyleSheet, TouchableOpacity
|
2018-09-25 19:28:42 +00:00
|
|
|
} from 'react-native';
|
2017-09-01 19:42:50 +00:00
|
|
|
import { connect } from 'react-redux';
|
2018-10-23 21:39:48 +00:00
|
|
|
import SafeAreaView from 'react-native-safe-area-view';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2019-01-31 16:08:38 +00:00
|
|
|
import Navigation from '../lib/Navigation';
|
2018-09-19 14:18:32 +00:00
|
|
|
import { serverRequest } from '../actions/server';
|
2018-08-10 17:26:36 +00:00
|
|
|
import sharedStyles 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';
|
2018-04-26 17:33:43 +00:00
|
|
|
import LoggedView from './View';
|
2018-06-01 17:38:13 +00:00
|
|
|
import I18n from '../i18n';
|
2018-11-14 21:42:03 +00:00
|
|
|
import { verticalScale, moderateScale } from '../utils/scaling';
|
2018-08-10 17:26:36 +00:00
|
|
|
import KeyboardView from '../presentation/KeyboardView';
|
2019-01-29 19:52:56 +00:00
|
|
|
import { isIOS, isNotch } from '../utils/deviceInfo';
|
2018-11-14 21:42:03 +00:00
|
|
|
import { LIGHT_HEADER } from '../constants/headerOptions';
|
2019-03-01 16:49:11 +00:00
|
|
|
import { CustomIcon } from '../lib/Icons';
|
2018-08-10 17:26:36 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
image: {
|
|
|
|
alignSelf: 'center',
|
2018-09-19 14:18:32 +00:00
|
|
|
marginVertical: verticalScale(20),
|
|
|
|
width: 210,
|
|
|
|
height: 171
|
2018-08-10 17:26:36 +00:00
|
|
|
},
|
|
|
|
title: {
|
2018-11-14 21:42:03 +00:00
|
|
|
...sharedStyles.textBold,
|
2018-08-10 17:26:36 +00:00
|
|
|
fontSize: moderateScale(22),
|
2018-11-14 21:42:03 +00:00
|
|
|
letterSpacing: 0,
|
|
|
|
color: '#2F343D',
|
|
|
|
alignSelf: 'center'
|
2018-08-10 17:26:36 +00:00
|
|
|
},
|
|
|
|
inputContainer: {
|
2018-11-14 21:42:03 +00:00
|
|
|
marginTop: 25,
|
|
|
|
marginBottom: 15
|
2018-08-10 17:26:36 +00:00
|
|
|
},
|
|
|
|
input: {
|
2018-11-14 21:42:03 +00:00
|
|
|
...sharedStyles.textRegular,
|
2018-09-11 16:32:52 +00:00
|
|
|
fontSize: 17,
|
2018-11-14 21:42:03 +00:00
|
|
|
letterSpacing: 0,
|
|
|
|
color: '#9EA2A8',
|
2018-09-11 16:32:52 +00:00
|
|
|
paddingTop: 14,
|
|
|
|
paddingBottom: 14,
|
2018-11-14 21:42:03 +00:00
|
|
|
paddingLeft: 16,
|
|
|
|
paddingRight: 16
|
2018-09-19 14:18:32 +00:00
|
|
|
},
|
|
|
|
backButton: {
|
|
|
|
position: 'absolute',
|
|
|
|
paddingHorizontal: 9,
|
|
|
|
left: 15
|
2018-08-10 17:26:36 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const defaultServer = 'https://open.rocket.chat';
|
2017-08-09 13:12:00 +00:00
|
|
|
|
2017-09-01 19:42:50 +00:00
|
|
|
@connect(state => ({
|
2018-12-21 10:55:35 +00:00
|
|
|
connecting: state.server.connecting
|
2017-09-01 19:42:50 +00:00
|
|
|
}), dispatch => ({
|
2018-09-19 14:18:32 +00:00
|
|
|
connectServer: server => dispatch(serverRequest(server))
|
2017-09-01 19:42:50 +00:00
|
|
|
}))
|
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 {
|
2018-10-23 21:39:48 +00:00
|
|
|
static options() {
|
|
|
|
return {
|
2018-11-14 21:42:03 +00:00
|
|
|
...LIGHT_HEADER,
|
2018-10-23 21:39:48 +00:00
|
|
|
topBar: {
|
|
|
|
visible: false,
|
|
|
|
drawBehind: true
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-08-05 18:16:32 +00:00
|
|
|
static propTypes = {
|
2018-10-23 21:39:48 +00:00
|
|
|
componentId: PropTypes.string,
|
2018-08-10 17:26:36 +00:00
|
|
|
server: PropTypes.string,
|
|
|
|
connecting: PropTypes.bool.isRequired,
|
2018-09-19 14:18:32 +00:00
|
|
|
connectServer: PropTypes.func.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-08-10 17:26:36 +00:00
|
|
|
text: ''
|
2017-08-03 18:23:43 +00:00
|
|
|
};
|
2018-10-23 21:39:48 +00:00
|
|
|
Navigation.events().bindComponent(this);
|
2018-08-10 17:26:36 +00:00
|
|
|
}
|
|
|
|
|
2018-08-01 19:35:06 +00:00
|
|
|
componentDidMount() {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { server, connectServer } = this.props;
|
2018-08-10 17:26:36 +00:00
|
|
|
if (server) {
|
2018-09-25 19:28:42 +00:00
|
|
|
connectServer(server);
|
2018-08-10 17:26:36 +00:00
|
|
|
this.setState({ text: server });
|
|
|
|
} else {
|
2018-11-14 21:42:03 +00:00
|
|
|
this.timeout = setTimeout(() => {
|
2018-08-10 17:26:36 +00:00
|
|
|
this.input.focus();
|
|
|
|
}, 600);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-21 10:55:35 +00:00
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
|
|
|
const { text } = this.state;
|
|
|
|
const { connecting } = this.props;
|
|
|
|
if (nextState.text !== text) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextProps.connecting !== connecting) {
|
|
|
|
return true;
|
2018-08-10 17:26:36 +00:00
|
|
|
}
|
2018-12-21 10:55:35 +00:00
|
|
|
return false;
|
2018-08-10 17:26:36 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 21:42:03 +00:00
|
|
|
componentWillUnmount() {
|
|
|
|
if (this.timeout) {
|
|
|
|
clearTimeout(this.timeout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-11 15:47:18 +00:00
|
|
|
onChangeText = (text) => {
|
|
|
|
this.setState({ text });
|
|
|
|
}
|
2017-09-21 17:08:00 +00:00
|
|
|
|
|
|
|
submit = () => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { text } = this.state;
|
|
|
|
const { connectServer } = this.props;
|
|
|
|
|
|
|
|
if (text) {
|
2018-05-30 16:51:30 +00:00
|
|
|
Keyboard.dismiss();
|
2018-09-25 19:28:42 +00:00
|
|
|
connectServer(this.completeUrl(text));
|
2018-05-30 16:51:30 +00:00
|
|
|
}
|
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();
|
|
|
|
|
2018-09-25 19:28:42 +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
|
|
|
}
|
|
|
|
|
2018-09-19 14:18:32 +00:00
|
|
|
renderBack = () => {
|
2018-10-23 21:39:48 +00:00
|
|
|
const { componentId } = this.props;
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2018-09-19 14:18:32 +00:00
|
|
|
let top = 15;
|
2019-01-29 19:52:56 +00:00
|
|
|
if (isIOS) {
|
|
|
|
top = isNotch ? 45 : 30;
|
2018-09-19 14:18:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<TouchableOpacity
|
|
|
|
style={[styles.backButton, { top }]}
|
2018-10-23 21:39:48 +00:00
|
|
|
onPress={() => Navigation.pop(componentId)}
|
2018-09-19 14:18:32 +00:00
|
|
|
>
|
2019-03-01 16:49:11 +00:00
|
|
|
<CustomIcon
|
|
|
|
name='back'
|
2018-09-19 14:18:32 +00:00
|
|
|
size={30}
|
|
|
|
color='#1D74F5'
|
|
|
|
/>
|
|
|
|
</TouchableOpacity>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-08-03 18:23:43 +00:00
|
|
|
render() {
|
2018-08-10 17:26:36 +00:00
|
|
|
const { connecting } = this.props;
|
|
|
|
const { text } = this.state;
|
2017-08-03 18:23:43 +00:00
|
|
|
return (
|
2017-11-07 16:28:02 +00:00
|
|
|
<KeyboardView
|
2018-08-10 17:26:36 +00:00
|
|
|
contentContainerStyle={sharedStyles.container}
|
2017-11-07 16:28:02 +00:00
|
|
|
keyboardVerticalOffset={128}
|
2018-08-10 17:26:36 +00:00
|
|
|
key='login-view'
|
2017-11-07 16:28:02 +00:00
|
|
|
>
|
2018-08-10 17:26:36 +00:00
|
|
|
<ScrollView {...scrollPersistTaps} contentContainerStyle={sharedStyles.containerScrollView}>
|
2018-10-23 21:39:48 +00:00
|
|
|
<SafeAreaView style={sharedStyles.container} testID='new-server-view' forceInset={{ bottom: 'never' }}>
|
2018-09-19 14:18:32 +00:00
|
|
|
<Image style={styles.image} source={{ uri: 'new_server' }} />
|
2018-08-10 17:26:36 +00:00
|
|
|
<Text style={styles.title}>{I18n.t('Sign_in_your_server')}</Text>
|
2018-04-24 19:34:03 +00:00
|
|
|
<TextInput
|
|
|
|
inputRef={e => this.input = e}
|
2018-08-10 17:26:36 +00:00
|
|
|
containerStyle={styles.inputContainer}
|
|
|
|
placeholder={defaultServer}
|
|
|
|
value={text}
|
2018-11-14 21:42:03 +00:00
|
|
|
returnKeyType='send'
|
2018-04-24 19:34:03 +00:00
|
|
|
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-08-10 17:26:36 +00:00
|
|
|
clearButtonMode='while-editing'
|
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
title={I18n.t('Connect')}
|
|
|
|
type='primary'
|
|
|
|
onPress={this.submit}
|
|
|
|
disabled={text.length === 0}
|
|
|
|
loading={connecting}
|
|
|
|
testID='new-server-view-button'
|
2018-04-24 19:34:03 +00:00
|
|
|
/>
|
2018-08-01 19:35:06 +00:00
|
|
|
</SafeAreaView>
|
2017-12-20 19:20:06 +00:00
|
|
|
</ScrollView>
|
2018-09-19 14:18:32 +00:00
|
|
|
{this.renderBack()}
|
2017-08-09 13:12:00 +00:00
|
|
|
</KeyboardView>
|
2017-08-03 18:23:43 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|