Skip to content
Snippets Groups Projects
login.js 3.74 KiB
Newer Older
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
import React from 'react';
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
import PropTypes from 'prop-types';
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
import { Text, TextInput, StyleSheet } 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 '../components/KeyboardView';

Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
const styles = StyleSheet.create({
	view: {
		flex: 1,
		flexDirection: 'column',
		justifyContent: 'center',
		alignItems: 'stretch',
		backgroundColor: '#fff'
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
	},
	input: {
		height: 40,
		borderColor: '#aaa',
		marginLeft: 20,
		marginRight: 20,
		marginTop: 10,
		padding: 5,
		borderWidth: 0,
		backgroundColor: '#f6f6f6'
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
	},
	error: {
		textAlign: 'center',
		color: 'red',
		paddingTop: 5
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
		loginRequest: PropTypes.func.isRequired,
		server: PropTypes.string.isRequired,
		Accounts_EmailOrUsernamePlaceholder: PropTypes.string,
		Accounts_PasswordPlaceholder: PropTypes.string
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: ''
		this.props.navigator.setTitle({
			title: 'Login'
		});
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
	}
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
	componentWillReceiveProps(nextProps) {
		this.props.navigator.setSubTitle({
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
			subtitle: nextProps.server
		});
	}
	submit = () => {
Guilherme Gazzo's avatar
Guilherme Gazzo committed
		const {	username, password, code } = this.state;
Guilherme Gazzo's avatar
Guilherme Gazzo committed
		console.log({	username, password, code });
Guilherme Gazzo's avatar
Guilherme Gazzo committed
		this.props.loginRequest({	username, password, code });
Guilherme Gazzo's avatar
Guilherme Gazzo committed
		this.props.navigator.dismissModal();
Guilherme Gazzo's avatar
Guilherme Gazzo committed
		//
		//
		// this.setState({
		// 	error: undefined
		// });
		//
		//
		// RocketChat.loginWithPassword(credentials, (error) => {
		// 	if (error) {
		// 		if (error.error === 'totp-required') {
		// 			this.setState({ totp: true });
		// 			this.codeInput.focus();
		// 		} else {
		// 			this.setState({
		// 				error: error.reason
		// 			});
		// 		}
		// 	} else {
		// 		this.props.navigator.dismissModal();
		// 	}
		// });
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
	renderTOTP = () => {
		if (this.state.totp) {
			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}>
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
				<TextInput
					style={styles.input}
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
					onChangeText={username => this.setState({ username })}
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
					keyboardType='email-address'
					autoCorrect={false}
					returnKeyType='done'
					autoCapitalize='none'
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
					autoFocus
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
					onSubmitEditing={this.submit}
					placeholder={this.props.Accounts_EmailOrUsernamePlaceholder || 'Email or username'}
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
				/>
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
				<TextInput
					style={styles.input}
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
					onChangeText={password => this.setState({ password })}
					secureTextEntry
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
					autoCorrect={false}
					returnKeyType='done'
					autoCapitalize='none'
					onSubmitEditing={this.submit}
					placeholder={this.props.Accounts_PasswordPlaceholder || 'Password'}
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
				/>
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
				{this.renderTOTP()}
				<Text style={styles.error}>{this.state.error}</Text>
			</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,
		Accounts_EmailOrUsernamePlaceholder: state.settings.Accounts_EmailOrUsernamePlaceholder,
		Accounts_PasswordPlaceholder: state.settings.Accounts_PasswordPlaceholder,
Guilherme Gazzo's avatar
Guilherme Gazzo committed
		login: state.login || state.default
Guilherme Gazzo's avatar
Guilherme Gazzo committed
	};
}

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

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