Skip to content
Snippets Groups Projects
LoginView.js 4.79 KiB
Newer Older
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
import React from 'react';
Guilherme Gazzo's avatar
Guilherme Gazzo committed

Guilherme Gazzo's avatar
Guilherme Gazzo committed
import Spinner from 'react-native-loading-spinner-overlay';
Guilherme Gazzo's avatar
Guilherme Gazzo committed

Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
import PropTypes from 'prop-types';
import { Keyboard, Text, TextInput, View, TouchableOpacity, SafeAreaView } from 'react-native';
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
Guilherme Gazzo's avatar
Guilherme Gazzo committed
// import * as actions from '../actions';
import * as loginActions from '../actions/login';
import KeyboardView from '../presentation/KeyboardView';
Guilherme Gazzo's avatar
Guilherme Gazzo committed
// import { Keyboard } from 'react-native'
import styles from './Styles';
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed

Guilherme Gazzo's avatar
Guilherme Gazzo committed
class LoginView extends React.Component {
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
	static propTypes = {
Guilherme Gazzo's avatar
Guilherme Gazzo committed
		loginSubmit: PropTypes.func.isRequired,
		Accounts_EmailOrUsernamePlaceholder: PropTypes.string,
Diego Sampaio's avatar
Diego Sampaio committed
		Accounts_PasswordPlaceholder: PropTypes.string,
gilmarsquinelato's avatar
gilmarsquinelato committed
		login: PropTypes.object,
		navigation: PropTypes.object.isRequired
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
	static navigationOptions = () => ({
		title: 'Login'
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
	});

	constructor(props) {
		super(props);

		this.state = {
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
			username: '',
			password: ''
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
		};
	submit = () => {
Guilherme Gazzo's avatar
Guilherme Gazzo committed
		const {	username, password, code } = this.state;
		if (username.trim() === '' || password.trim() === '') {
			return;
		}

Guilherme Gazzo's avatar
Guilherme Gazzo committed
		this.props.loginSubmit({	username, password, code });
		Keyboard.dismiss();
gilmarsquinelato's avatar
gilmarsquinelato committed
	register = () => {
		this.props.navigation.navigate('Register');
	}

Diego Mello's avatar
Diego Mello committed
	termsService = () => {
		this.props.navigation.navigate('TermsService');
	}

	privacyPolicy = () => {
		this.props.navigation.navigate('PrivacyPolicy');
	}

Diego Mello's avatar
Diego Mello committed
	forgotPassword = () => {
		this.props.navigation.navigate('ForgotPassword');
	}

Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
	renderTOTP = () => {
Guilherme Gazzo's avatar
Guilherme Gazzo committed
		if (/totp/ig.test(this.props.login.error.error)) {
			return (
				<TextInput
					ref={ref => this.codeInput = ref}
					style={styles.input_white}
					onChangeText={code => this.setState({ code })}
					keyboardType='numeric'
					autoCorrect={false}
					returnKeyType='done'
					autoCapitalize='none'
					onSubmitEditing={this.submit}
					placeholder='Code'
					underlineColorAndroid='transparent'
				/>
			);
Guilherme Gazzo's avatar
Guilherme Gazzo committed
		return null;
Guilherme Gazzo's avatar
Guilherme Gazzo committed
	// {this.props.login.isFetching && <Text> LOGANDO</Text>}
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
	render() {
		return (
gilmarsquinelato's avatar
gilmarsquinelato committed
			<KeyboardView
				contentContainerStyle={styles.container}
				keyboardVerticalOffset={128}
			>
				<View style={styles.loginView}>
						<View style={styles.formContainer}>
							<TextInput
								style={styles.input_white}
								onChangeText={username => this.setState({ username })}
								keyboardType='email-address'
								autoCorrect={false}
								returnKeyType='next'
								autoCapitalize='none'
								underlineColorAndroid='transparent'
								onSubmitEditing={() => { this.password.focus(); }}
								placeholder={this.props.Accounts_EmailOrUsernamePlaceholder || 'Email or username'}
							/>
							<TextInput
								ref={(e) => { this.password = e; }}
								style={styles.input_white}
								onChangeText={password => this.setState({ password })}
								secureTextEntry
								autoCorrect={false}
								returnKeyType='done'
								autoCapitalize='none'
								underlineColorAndroid='transparent'
								onSubmitEditing={this.submit}
								placeholder={this.props.Accounts_PasswordPlaceholder || 'Password'}
							/>

							{this.renderTOTP()}

							<TouchableOpacity
								style={styles.buttonContainer}
								onPress={this.submit}
							>
								<Text style={styles.button}>LOGIN</Text>
							</TouchableOpacity>

							<View style={styles.loginSecondaryButtons}>
								<TouchableOpacity style={styles.buttonContainer_inverted} onPress={this.register}>
									<Text style={styles.button_inverted}>REGISTER</Text>
								</TouchableOpacity>

								<TouchableOpacity style={styles.buttonContainer_inverted} onPress={this.forgotPassword}>
									<Text style={styles.button_inverted}>FORGOT MY PASSWORD</Text>
								</TouchableOpacity>
							</View>

							<TouchableOpacity>
								<Text style={styles.loginTermsText}>
									By proceeding you are agreeing to our
									<Text style={styles.link} onPress={this.termsService}> Terms of Service </Text>
									and
									<Text style={styles.link} onPress={this.privacyPolicy}> Privacy Policy</Text>
								</Text>
							</TouchableOpacity>
							{this.props.login.failure && <Text style={styles.error}>{this.props.login.error.reason}</Text>}
						</View>
Guilherme Gazzo's avatar
Guilherme Gazzo committed
						<Spinner visible={this.props.login.isFetching} textContent='Loading...' textStyle={{ color: '#FFF' }} />
			</KeyboardView>
Guilherme Gazzo's avatar
Guilherme Gazzo committed

function mapStateToProps(state) {
Guilherme Gazzo's avatar
Guilherme Gazzo committed
	// console.log(Object.keys(state));
Guilherme Gazzo's avatar
Guilherme Gazzo committed
	return {
		server: state.server.server,
Guilherme Gazzo's avatar
Guilherme Gazzo committed
		Accounts_EmailOrUsernamePlaceholder: state.settings.Accounts_EmailOrUsernamePlaceholder,
		Accounts_PasswordPlaceholder: state.settings.Accounts_PasswordPlaceholder,
Guilherme Gazzo's avatar
Guilherme Gazzo committed
	};
}

function mapDispatchToProps(dispatch) {
	return bindActionCreators(loginActions, dispatch);
}

export default connect(mapStateToProps, mapDispatchToProps)(LoginView);