Rocket.Chat.ReactNative/app/views/OnboardingView/index.js

197 lines
5.2 KiB
JavaScript
Raw Normal View History

import React from 'react';
import {
View, Text, Image, TouchableOpacity, BackHandler
} from 'react-native';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { SafeAreaView } from 'react-navigation';
2019-03-12 16:23:06 +00:00
import Orientation from 'react-native-orientation-locker';
import { selectServerRequest, serverInitAdd, serverFinishAdd } from '../../actions/server';
import { appStart as appStartAction } from '../../actions';
import I18n from '../../i18n';
import openLink from '../../utils/openLink';
import Button from './Button';
import styles from './styles';
2019-11-25 20:01:17 +00:00
import { isIOS, isNotch, isTablet } from '../../utils/deviceInfo';
import EventEmitter from '../../utils/events';
import { CustomIcon } from '../../lib/Icons';
2019-03-12 16:23:06 +00:00
import StatusBar from '../../containers/StatusBar';
2019-12-04 16:39:53 +00:00
import { themes } from '../../constants/colors';
import { withTheme } from '../../theme';
2019-11-25 20:01:17 +00:00
import sharedStyles from '../Styles';
class OnboardingView extends React.Component {
2019-03-12 16:23:06 +00:00
static navigationOptions = () => ({
header: null
})
static propTypes = {
2019-03-12 16:23:06 +00:00
navigation: PropTypes.object,
adding: PropTypes.bool,
selectServer: PropTypes.func.isRequired,
currentServer: PropTypes.string,
initAdd: PropTypes.func,
finishAdd: PropTypes.func,
2019-12-04 16:39:53 +00:00
appStart: PropTypes.func,
theme: PropTypes.string
}
constructor(props) {
super(props);
BackHandler.addEventListener('hardwareBackPress', this.handleBackPress);
2019-03-12 16:23:06 +00:00
this.previousServer = props.navigation.getParam('previousServer');
2019-11-25 20:01:17 +00:00
if (!isTablet) {
Orientation.lockToPortrait();
}
}
componentDidMount() {
2019-03-12 16:23:06 +00:00
const { initAdd } = this.props;
if (this.previousServer) {
initAdd();
}
EventEmitter.addEventListener('NewServer', this.handleNewServerEvent);
}
2019-12-04 16:39:53 +00:00
shouldComponentUpdate(nextProps) {
const { theme } = this.props;
if (theme !== nextProps.theme) {
return true;
}
return false;
}
componentWillUnmount() {
const {
2019-03-12 16:23:06 +00:00
selectServer, currentServer, adding, finishAdd
} = this.props;
if (adding) {
2019-03-12 16:23:06 +00:00
if (this.previousServer !== currentServer) {
selectServer(this.previousServer);
}
finishAdd();
}
EventEmitter.removeListener('NewServer', this.handleNewServerEvent);
BackHandler.removeEventListener('hardwareBackPress', this.handleBackPress);
}
handleBackPress = () => {
const { appStart } = this.props;
appStart('background');
return false;
}
close = () => {
2019-03-12 16:23:06 +00:00
const { appStart } = this.props;
appStart('inside');
}
newServer = (server) => {
2019-03-12 16:23:06 +00:00
const { navigation } = this.props;
navigation.navigate('NewServerView', { server });
}
handleNewServerEvent = (event) => {
const { server } = event;
this.newServer(server);
}
connectServer = () => {
this.newServer();
}
joinCommunity = () => {
this.newServer('https://open.rocket.chat');
}
createWorkspace = () => {
2019-12-04 16:39:53 +00:00
const { theme } = this.props;
openLink('https://cloud.rocket.chat/trial', theme);
}
renderClose = () => {
2019-12-04 16:39:53 +00:00
const { theme } = this.props;
2019-03-12 16:23:06 +00:00
if (this.previousServer) {
let top = 15;
2019-01-29 19:52:56 +00:00
if (isIOS) {
top = isNotch ? 45 : 30;
}
return (
<TouchableOpacity
style={[styles.closeModal, { top }]}
onPress={this.close}
testID='onboarding-close'
>
<CustomIcon
name='cross'
size={30}
2019-12-04 16:39:53 +00:00
color={themes[theme].actionTintColor}
/>
</TouchableOpacity>
);
}
return null;
}
render() {
2019-12-04 16:39:53 +00:00
const { theme } = this.props;
return (
2019-12-04 16:39:53 +00:00
<SafeAreaView
style={[
styles.container,
{ backgroundColor: themes[theme].backgroundColor }
]}
testID='onboarding-view'
>
<StatusBar theme={theme} />
<Image style={styles.onboarding} source={{ uri: 'onboarding' }} fadeDuration={0} />
2019-12-04 16:39:53 +00:00
<Text style={[styles.title, { color: themes[theme].titleText }]}>{I18n.t('Welcome_to_RocketChat')}</Text>
<Text style={[styles.subtitle, { color: themes[theme].auxiliaryText }]}>{I18n.t('Open_Source_Communication')}</Text>
2019-11-25 20:01:17 +00:00
<View style={[styles.buttonsContainer, isTablet && sharedStyles.tabletScreenContent]}>
<Button
type='secondary'
title={I18n.t('Connect_to_a_server')}
2019-12-04 16:39:53 +00:00
icon={<CustomIcon name='permalink' size={30} color={themes[theme].actionTintColor} />}
onPress={this.connectServer}
testID='connect-server-button'
2019-12-04 16:39:53 +00:00
theme={theme}
/>
<Button
type='secondary'
title={I18n.t('Join_the_community')}
subtitle='open.rocket.chat'
icon={<Image source={{ uri: 'logo_onboarding' }} style={{ width: 32, height: 27 }} fadeDuration={0} />}
onPress={this.joinCommunity}
testID='join-community-button'
2019-12-04 16:39:53 +00:00
theme={theme}
/>
<Button
type='primary'
title={I18n.t('Create_a_new_workspace')}
2019-12-04 16:39:53 +00:00
icon={<CustomIcon name='plus' size={30} color={themes[theme].buttonText} />}
onPress={this.createWorkspace}
testID='create-workspace-button'
2019-12-04 16:39:53 +00:00
theme={theme}
/>
</View>
{this.renderClose()}
</SafeAreaView>
);
}
}
const mapStateToProps = state => ({
currentServer: state.server.server,
adding: state.server.adding
});
const mapDispatchToProps = dispatch => ({
initAdd: () => dispatch(serverInitAdd()),
finishAdd: () => dispatch(serverFinishAdd()),
selectServer: server => dispatch(selectServerRequest(server)),
appStart: root => dispatch(appStartAction(root))
});
2019-12-04 16:39:53 +00:00
export default connect(mapStateToProps, mapDispatchToProps)(withTheme(OnboardingView));