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

import Spinner from 'react-native-loading-spinner-overlay';

Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
import PropTypes from 'prop-types';
import { Keyboard, Text, TextInput, View, Image, TouchableOpacity } 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 = {
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
		navigator: PropTypes.object.isRequired,
Guilherme Gazzo's avatar
Guilherme Gazzo committed
		loginSubmit: PropTypes.func.isRequired,
		server: PropTypes.string.isRequired,
		Accounts_EmailOrUsernamePlaceholder: PropTypes.string,
Diego Sampaio's avatar
Diego Sampaio committed
		Accounts_PasswordPlaceholder: PropTypes.string,
		login: PropTypes.object
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
		};
	}
	componentWillReceiveProps() {
		const { navigator } = this.props;
		navigator.setTitle({
			title: 'Login'
		});
		navigator.setSubTitle({
			subtitle: this.props.server
		});
		navigator.setButtons({
			rightButtons: [{
				id: 'close',
				title: 'Cancel'
			}]
		this.props.navigator.setOnNavigatorEvent(this.onNavigatorEvent.bind(this));
	}
	onNavigatorEvent = (event) => {
		if (event.type === 'NavBarButtonPress') {
			if (event.id === 'close') {
				this.props.navigator.resetTo({
					screen: 'ListServer',
					animated: false

				});
			}
		}
	}
	submit = () => {
Guilherme Gazzo's avatar
Guilherme Gazzo committed
		const {	username, password, code } = this.state;
Guilherme Gazzo's avatar
Guilherme Gazzo committed
		this.props.loginSubmit({	username, password, code });
		Keyboard.dismiss();
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
	renderTOTP = () => {
Diego Sampaio's avatar
Diego Sampaio committed
		if (this.props.login.errorMessage && this.props.login.errorMessage.error === 'totp-required') {
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
			return (
				<TextInput
					ref={ref => this.codeInput = ref}
					style={styles.input}
					onChangeText={code => this.setState({ code })}
					keyboardType='numeric'
					autoCorrect={false}
					returnKeyType='done'
					autoCapitalize='none'
					onSubmitEditing={this.submit}
					placeholder='Code'
				/>
			);
		}
	}

Guilherme Gazzo's avatar
Guilherme Gazzo committed
	// {this.props.login.isFetching && <Text> LOGANDO</Text>}
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
	render() {
		return (
			<KeyboardView style={styles.view} keyboardVerticalOffset={64}>
Guilherme Gazzo's avatar
Guilherme Gazzo committed
				<View style={styles.logoContainer}>
					<Image style={styles.logo} source={require('../images/logo.png')} />
				</View>
				<View style={styles.formContainer}>
					<TextInput
						placeholderTextColor={'rgba(255,255,255,.2)'}
						style={styles.input}
						onChangeText={username => this.setState({ username })}
						keyboardType='email-address'
						autoCorrect={false}
						returnKeyType='done'
						autoCapitalize='none'
						autoFocus

						underlineColorAndroid='transparent'
						onSubmitEditing={this.submit}
						placeholder={this.props.Accounts_EmailOrUsernamePlaceholder || 'Email or username'}
					/>
					<TextInput
						placeholderTextColor={'rgba(255,255,255,.2)'}
						style={styles.input}
						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}>
						<Text style={styles.button} onPress={this.submit}>LOGIN</Text>
					</TouchableOpacity>
					{this.props.login.error && <Text style={styles.error}>{this.props.login.error}</Text>}
				</View>
				<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);