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-19 14:18:32 +00:00
|
|
|
import { Text, ScrollView, Keyboard, SafeAreaView, Image, Alert, StyleSheet, TouchableOpacity } from 'react-native';
|
2017-09-01 19:42:50 +00:00
|
|
|
import { connect } from 'react-redux';
|
2018-09-19 14:18:32 +00:00
|
|
|
import Icon from 'react-native-vector-icons/Ionicons';
|
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-04-26 17:33:43 +00:00
|
|
|
import LoggedView from './View';
|
2018-06-01 17:38:13 +00:00
|
|
|
import I18n from '../i18n';
|
2018-08-10 17:26:36 +00:00
|
|
|
import { scale, verticalScale, moderateScale } from '../utils/scaling';
|
|
|
|
import KeyboardView from '../presentation/KeyboardView';
|
2018-09-19 14:18:32 +00:00
|
|
|
import DeviceInfo from '../utils/deviceInfo';
|
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: {
|
|
|
|
alignSelf: 'center',
|
|
|
|
color: '#2F343D',
|
|
|
|
fontSize: moderateScale(22),
|
|
|
|
fontWeight: 'bold',
|
|
|
|
height: verticalScale(28),
|
|
|
|
lineHeight: verticalScale(28)
|
|
|
|
},
|
|
|
|
inputContainer: {
|
|
|
|
marginTop: scale(20),
|
|
|
|
marginBottom: scale(20)
|
|
|
|
},
|
|
|
|
input: {
|
|
|
|
color: '#9EA2A8',
|
2018-09-11 16:32:52 +00:00
|
|
|
fontSize: 17,
|
|
|
|
paddingTop: 14,
|
|
|
|
paddingBottom: 14,
|
|
|
|
paddingHorizontal: 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-08-10 17:26:36 +00:00
|
|
|
connecting: state.server.connecting,
|
2018-09-19 14:18:32 +00:00
|
|
|
failure: state.server.failure
|
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 {
|
2017-08-05 18:16:32 +00:00
|
|
|
static propTypes = {
|
2018-07-10 13:40:32 +00:00
|
|
|
navigator: PropTypes.object,
|
2018-08-10 17:26:36 +00:00
|
|
|
server: PropTypes.string,
|
|
|
|
connecting: PropTypes.bool.isRequired,
|
|
|
|
failure: 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-08-10 17:26:36 +00:00
|
|
|
}
|
|
|
|
|
2018-08-01 19:35:06 +00:00
|
|
|
componentDidMount() {
|
2018-09-19 14:18:32 +00:00
|
|
|
const { server } = this.props;
|
2018-08-10 17:26:36 +00:00
|
|
|
if (server) {
|
|
|
|
this.props.connectServer(server);
|
|
|
|
this.setState({ text: server });
|
|
|
|
} else {
|
|
|
|
setTimeout(() => {
|
|
|
|
this.input.focus();
|
|
|
|
}, 600);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
if (nextProps.failure && nextProps.failure !== this.props.failure) {
|
|
|
|
Alert.alert(I18n.t('Oops'), I18n.t('The_URL_is_invalid'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-11 15:47:18 +00:00
|
|
|
onChangeText = (text) => {
|
|
|
|
this.setState({ text });
|
|
|
|
}
|
2017-09-21 17:08:00 +00:00
|
|
|
|
|
|
|
submit = () => {
|
2018-08-10 17:26:36 +00:00
|
|
|
if (this.state.text) {
|
2018-05-30 16:51:30 +00:00
|
|
|
Keyboard.dismiss();
|
2018-08-10 17:26:36 +00:00
|
|
|
this.props.connectServer(this.completeUrl(this.state.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();
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-09-19 14:18:32 +00:00
|
|
|
renderBack = () => {
|
|
|
|
let top = 15;
|
|
|
|
if (DeviceInfo.getBrand() === 'Apple') {
|
|
|
|
top = DeviceInfo.isNotch() ? 45 : 30;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<TouchableOpacity
|
|
|
|
style={[styles.backButton, { top }]}
|
|
|
|
onPress={() => this.props.navigator.pop()}
|
|
|
|
>
|
|
|
|
<Icon
|
|
|
|
name='ios-arrow-back'
|
|
|
|
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}>
|
|
|
|
<SafeAreaView style={sharedStyles.container} testID='new-server-view'>
|
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}
|
|
|
|
inputStyle={styles.input}
|
|
|
|
placeholder={defaultServer}
|
|
|
|
value={text}
|
2018-04-24 19:34:03 +00:00
|
|
|
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-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
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|