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 {
|
2019-09-19 13:32:24 +00:00
|
|
|
Text, ScrollView, Keyboard, Image, StyleSheet, TouchableOpacity, View, Alert
|
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-10-02 12:18:08 +00:00
|
|
|
import { SafeAreaView } from 'react-navigation';
|
2019-09-02 16:19:05 +00:00
|
|
|
import * as FileSystem from 'expo-file-system';
|
|
|
|
import DocumentPicker from 'react-native-document-picker';
|
|
|
|
import ActionSheet from 'react-native-action-sheet';
|
|
|
|
import isEqual from 'deep-equal';
|
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';
|
2019-09-02 16:19:05 +00:00
|
|
|
import log from '../utils/log';
|
2019-09-19 13:32:24 +00:00
|
|
|
import { animateNextTransition } from '../utils/layoutAnimation';
|
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
|
2019-09-02 16:19:05 +00:00
|
|
|
},
|
|
|
|
certificatePicker: {
|
|
|
|
flex: 1,
|
|
|
|
marginTop: 40,
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'center'
|
|
|
|
},
|
|
|
|
chooseCertificateTitle: {
|
|
|
|
fontSize: 15,
|
|
|
|
...sharedStyles.textRegular,
|
|
|
|
...sharedStyles.textColorDescription
|
|
|
|
},
|
|
|
|
chooseCertificate: {
|
|
|
|
fontSize: 15,
|
|
|
|
...sharedStyles.textSemibold,
|
|
|
|
...sharedStyles.textColorHeaderBack
|
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');
|
2019-09-02 16:19:05 +00:00
|
|
|
|
|
|
|
// Cancel
|
|
|
|
this.options = [I18n.t('Cancel')];
|
|
|
|
this.CANCEL_INDEX = 0;
|
|
|
|
|
|
|
|
// Delete
|
|
|
|
this.options.push(I18n.t('Delete'));
|
|
|
|
this.DELETE_INDEX = 1;
|
|
|
|
|
2019-08-07 19:20:16 +00:00
|
|
|
this.state = {
|
|
|
|
text: server || '',
|
2019-09-02 16:19:05 +00:00
|
|
|
autoFocus: !server,
|
|
|
|
certificate: null
|
2019-08-07 19:20:16 +00:00
|
|
|
};
|
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) {
|
2019-09-02 16:19:05 +00:00
|
|
|
const { text, certificate } = this.state;
|
2018-12-21 10:55:35 +00:00
|
|
|
const { connecting } = this.props;
|
|
|
|
if (nextState.text !== text) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-09-02 16:19:05 +00:00
|
|
|
if (!isEqual(nextState.certificate, certificate)) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-21 10:55:35 +00:00
|
|
|
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
|
|
|
|
2019-09-02 16:19:05 +00:00
|
|
|
submit = async() => {
|
|
|
|
const { text, certificate } = this.state;
|
2018-09-25 19:28:42 +00:00
|
|
|
const { connectServer } = this.props;
|
2019-09-02 16:19:05 +00:00
|
|
|
let cert = null;
|
|
|
|
|
|
|
|
if (certificate) {
|
|
|
|
const certificatePath = `${ FileSystem.documentDirectory }/${ certificate.name }`;
|
|
|
|
try {
|
|
|
|
await FileSystem.copyAsync({ from: certificate.path, to: certificatePath });
|
2019-09-02 16:59:41 +00:00
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
2019-09-02 16:19:05 +00:00
|
|
|
}
|
|
|
|
cert = {
|
|
|
|
path: this.uriToPath(certificatePath), // file:// isn't allowed by obj-C
|
|
|
|
password: certificate.password
|
|
|
|
};
|
|
|
|
}
|
2018-09-25 19:28:42 +00:00
|
|
|
|
|
|
|
if (text) {
|
2018-05-30 16:51:30 +00:00
|
|
|
Keyboard.dismiss();
|
2019-09-02 16:19:05 +00:00
|
|
|
connectServer(this.completeUrl(text), cert);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
chooseCertificate = async() => {
|
|
|
|
try {
|
|
|
|
const res = await DocumentPicker.pick({
|
|
|
|
type: ['com.rsa.pkcs-12']
|
|
|
|
});
|
|
|
|
const { uri: path, name } = res;
|
|
|
|
Alert.prompt(
|
|
|
|
I18n.t('Certificate_password'),
|
|
|
|
I18n.t('Whats_the_password_for_your_certificate'),
|
|
|
|
[
|
|
|
|
{
|
|
|
|
text: 'OK',
|
|
|
|
onPress: password => this.saveCertificate({ path, name, password })
|
|
|
|
}
|
|
|
|
],
|
2019-10-31 16:21:59 +00:00
|
|
|
'secure-text'
|
2019-09-02 16:19:05 +00:00
|
|
|
);
|
2019-09-02 16:59:41 +00:00
|
|
|
} catch (e) {
|
|
|
|
if (!DocumentPicker.isCancel(e)) {
|
|
|
|
log(e);
|
2019-09-02 16:19:05 +00:00
|
|
|
}
|
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`;
|
|
|
|
}
|
|
|
|
|
2019-09-16 21:04:20 +00:00
|
|
|
if (/^(https?:\/\/)?(((\w|[0-9-_])+(\.(\w|[0-9-_])+)+)|localhost)(:\d+)?$/.test(url)) {
|
2017-08-11 15:47:18 +00:00
|
|
|
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
|
|
|
|
2019-10-23 19:28:24 +00:00
|
|
|
return url.replace(/\/+$/, '').replace(/\\/g, '/');
|
2017-08-11 15:47:18 +00:00
|
|
|
}
|
|
|
|
|
2019-09-02 16:19:05 +00:00
|
|
|
uriToPath = uri => uri.replace('file://', '');
|
|
|
|
|
|
|
|
saveCertificate = (certificate) => {
|
2019-09-19 13:32:24 +00:00
|
|
|
animateNextTransition();
|
2019-09-02 16:19:05 +00:00
|
|
|
this.setState({ certificate });
|
|
|
|
}
|
|
|
|
|
|
|
|
handleDelete = () => this.setState({ certificate: null }); // We not need delete file from DocumentPicker because it is a temp file
|
|
|
|
|
|
|
|
showActionSheet = () => {
|
|
|
|
ActionSheet.showActionSheetWithOptions({
|
|
|
|
options: this.options,
|
|
|
|
cancelButtonIndex: this.CANCEL_INDEX,
|
|
|
|
destructiveButtonIndex: this.DELETE_INDEX
|
|
|
|
}, (actionIndex) => {
|
|
|
|
if (actionIndex === this.DELETE_INDEX) { this.handleDelete(); }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-09-02 16:19:05 +00:00
|
|
|
renderCertificatePicker = () => {
|
|
|
|
const { certificate } = this.state;
|
|
|
|
return (
|
|
|
|
<View style={styles.certificatePicker}>
|
|
|
|
<Text style={styles.chooseCertificateTitle}>{certificate ? I18n.t('Your_certificate') : I18n.t('Do_you_have_a_certificate')}</Text>
|
|
|
|
<TouchableOpacity onPress={certificate ? this.showActionSheet : this.chooseCertificate} testID='new-server-choose-certificate'>
|
|
|
|
<Text style={styles.chooseCertificate}>{certificate ? certificate.name : I18n.t('Apply_Your_Certificate')}</Text>
|
|
|
|
</TouchableOpacity>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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 />
|
2019-10-02 12:18:08 +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
|
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
|
|
|
/>
|
2019-10-02 12:18:08 +00:00
|
|
|
{ isIOS ? this.renderCertificatePicker() : null }
|
|
|
|
</SafeAreaView>
|
|
|
|
</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 => ({
|
2019-09-02 16:19:05 +00:00
|
|
|
connectServer: (server, certificate) => dispatch(serverRequest(server, certificate))
|
2019-08-07 13:51:34 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(NewServerView);
|