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-31 18:13:30 +00:00
|
|
|
import { Text, ScrollView, Keyboard, SafeAreaView, Image, Alert, StyleSheet } from 'react-native';
|
2017-09-01 19:42:50 +00:00
|
|
|
import { connect } from 'react-redux';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-08-31 18:13:30 +00:00
|
|
|
import { serverRequest, selectServerRequest, serverInitAdd, serverFinishAdd } 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';
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
image: {
|
|
|
|
alignSelf: 'center',
|
|
|
|
marginVertical: verticalScale(20)
|
|
|
|
},
|
|
|
|
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',
|
|
|
|
fontSize: moderateScale(17),
|
|
|
|
paddingTop: scale(14),
|
|
|
|
paddingBottom: scale(14),
|
|
|
|
paddingHorizontal: scale(16)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
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,
|
|
|
|
failure: state.server.failure,
|
2018-08-31 18:13:30 +00:00
|
|
|
currentServer: state.server.server,
|
|
|
|
adding: state.server.adding
|
2017-09-01 19:42:50 +00:00
|
|
|
}), dispatch => ({
|
2018-08-31 18:13:30 +00:00
|
|
|
initAdd: () => dispatch(serverInitAdd()),
|
|
|
|
finishAdd: () => dispatch(serverFinishAdd()),
|
|
|
|
connectServer: server => dispatch(serverRequest(server)),
|
|
|
|
selectServer: server => dispatch(selectServerRequest(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,
|
2018-08-31 18:13:30 +00:00
|
|
|
adding: PropTypes.bool,
|
2018-08-10 17:26:36 +00:00
|
|
|
failure: PropTypes.bool.isRequired,
|
|
|
|
connectServer: PropTypes.func.isRequired,
|
2018-08-31 18:13:30 +00:00
|
|
|
selectServer: PropTypes.func.isRequired,
|
2018-08-10 17:26:36 +00:00
|
|
|
previousServer: PropTypes.string,
|
2018-08-31 18:13:30 +00:00
|
|
|
currentServer: PropTypes.string,
|
|
|
|
initAdd: PropTypes.func,
|
|
|
|
finishAdd: PropTypes.func
|
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
|
|
|
props.navigator.setOnNavigatorEvent(this.onNavigatorEvent.bind(this));
|
|
|
|
}
|
|
|
|
|
2018-08-01 19:35:06 +00:00
|
|
|
componentDidMount() {
|
2018-08-31 18:13:30 +00:00
|
|
|
const { server, previousServer } = 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);
|
|
|
|
}
|
2018-08-31 18:13:30 +00:00
|
|
|
if (previousServer) {
|
|
|
|
this.props.initAdd();
|
|
|
|
}
|
2018-08-10 17:26:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
if (nextProps.failure && nextProps.failure !== this.props.failure) {
|
|
|
|
Alert.alert(I18n.t('Oops'), I18n.t('The_URL_is_invalid'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-31 18:13:30 +00:00
|
|
|
componentWillUnmount() {
|
|
|
|
const {
|
|
|
|
selectServer, previousServer, currentServer, adding, finishAdd
|
|
|
|
} = this.props;
|
|
|
|
if (adding) {
|
|
|
|
if (previousServer !== currentServer) {
|
|
|
|
selectServer(previousServer);
|
|
|
|
}
|
|
|
|
finishAdd();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-10 17:26:36 +00:00
|
|
|
onNavigatorEvent(event) {
|
|
|
|
if (event.type === 'NavBarButtonPress') {
|
2018-08-31 18:13:30 +00:00
|
|
|
if (event.id === 'cancel') {
|
|
|
|
this.props.navigator.dismissModal();
|
2018-08-10 17:26:36 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-01 19:35:06 +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-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
|
|
|
}
|
|
|
|
|
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'>
|
|
|
|
<Image style={styles.image} source={require('../static/images/server.png')} />
|
|
|
|
<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>
|
2017-08-09 13:12:00 +00:00
|
|
|
</KeyboardView>
|
2017-08-03 18:23:43 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|