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';
|
2019-03-12 16:23:06 +00:00
|
|
|
import { SafeAreaView } from 'react-navigation';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
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-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';
|
2019-03-01 16:49:11 +00:00
|
|
|
import { CustomIcon } from '../lib/Icons';
|
2019-03-12 16:23:06 +00:00
|
|
|
import StatusBar from '../containers/StatusBar';
|
2019-03-29 19:36:07 +00:00
|
|
|
import { COLOR_PRIMARY } from '../constants/colors';
|
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,
|
2019-03-29 19:36:07 +00:00
|
|
|
...sharedStyles.textColorNormal,
|
2018-08-10 17:26:36 +00:00
|
|
|
fontSize: moderateScale(22),
|
2018-11-14 21:42:03 +00:00
|
|
|
letterSpacing: 0,
|
|
|
|
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
|
|
|
},
|
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
|
|
|
|
2019-08-07 13:51:34 +00:00
|
|
|
class NewServerView extends React.Component {
|
2019-03-12 16:23:06 +00:00
|
|
|
static navigationOptions = () => ({
|
|
|
|
header: null
|
|
|
|
})
|
2018-10-23 21:39:48 +00:00
|
|
|
|
2017-08-05 18:16:32 +00:00
|
|
|
static propTypes = {
|
2019-03-12 16:23:06 +00:00
|
|
|
navigation: PropTypes.object,
|
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
|
|
|
}
|
|
|
|
|
2019-08-07 19:20:16 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
const server = props.navigation.getParam('server');
|
|
|
|
this.state = {
|
|
|
|
text: server || '',
|
|
|
|
autoFocus: !server
|
|
|
|
};
|
2018-08-10 17:26:36 +00:00
|
|
|
}
|
|
|
|
|
2018-08-01 19:35:06 +00:00
|
|
|
componentDidMount() {
|
2019-08-07 19:20:16 +00:00
|
|
|
const { text } = this.state;
|
|
|
|
const { connectServer } = this.props;
|
|
|
|
if (text) {
|
|
|
|
connectServer(text);
|
2018-08-10 17:26:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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 = () => {
|
2019-03-12 16:23:06 +00:00
|
|
|
const { navigation } = 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 }]}
|
2019-03-12 16:23:06 +00:00
|
|
|
onPress={() => navigation.pop()}
|
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}
|
2019-03-29 19:36:07 +00:00
|
|
|
color={COLOR_PRIMARY}
|
2018-09-19 14:18:32 +00:00
|
|
|
/>
|
|
|
|
</TouchableOpacity>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-08-03 18:23:43 +00:00
|
|
|
render() {
|
2018-08-10 17:26:36 +00:00
|
|
|
const { connecting } = this.props;
|
2019-08-07 19:20:16 +00:00
|
|
|
const { text, autoFocus } = 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
|
|
|
>
|
2019-03-12 16:23:06 +00:00
|
|
|
<StatusBar light />
|
2018-08-10 17:26:36 +00:00
|
|
|
<ScrollView {...scrollPersistTaps} contentContainerStyle={sharedStyles.containerScrollView}>
|
2019-08-07 13:51:34 +00:00
|
|
|
<SafeAreaView style={sharedStyles.container} testID='new-server-view' forceInset={{ vertical: '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
|
2019-08-07 19:20:16 +00:00
|
|
|
autoFocus={autoFocus}
|
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}
|
2019-08-07 19:20:16 +00:00
|
|
|
disabled={!text}
|
2018-08-10 17:26:36 +00:00
|
|
|
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
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-08-07 13:51:34 +00:00
|
|
|
|
|
|
|
const mapStateToProps = state => ({
|
|
|
|
connecting: state.server.connecting
|
|
|
|
});
|
|
|
|
|
|
|
|
const mapDispatchToProps = dispatch => ({
|
|
|
|
connectServer: server => dispatch(serverRequest(server))
|
|
|
|
});
|
|
|
|
|
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(NewServerView);
|