Guest User

Untitled

a guest
Nov 18th, 2017
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from "react";
  2. import { View, Text, StyleSheet, Button, Alert } from "react-native";
  3. import { Color } from "../values";
  4. import { Container, EditText } from "../components";
  5. import { signInUser } from "../services/UserManager";
  6. import * as Validators from "../utils/Validators";
  7. import { NavigationActions } from "react-navigation";
  8.  
  9. class LoginComponent extends React.Component {
  10.   constructor(props) {
  11.     super(props);
  12.     this.state = {
  13.       userName: "",
  14.       password: "",
  15.       usernameError: "",
  16.       passwordError: "",
  17.       disableLoginButton: true
  18.     };
  19.  
  20.     this._onLoginClicked = this._onLoginClicked.bind(this);
  21.   }
  22.  
  23.   _onLoginClicked() {
  24.     console.log("Login Clicked");
  25.     signInUser(this.state.userName, this.state.password)
  26.       .then(user => {
  27.         this.props.onLoginSuccess(user)
  28.       })
  29.       .catch(error => this.onLoginError(error));
  30.   }
  31.  
  32.   onLoginError(error) {
  33.     Alert.alert("Login Failed", error.message);
  34.   }
  35.  
  36.   checkForLoginButton() {
  37.     let areInputsValid =
  38.       Validators.isEmailValid(this.state.userName) &&
  39.       this.state.password.length > 6;
  40.     console.log(areInputsValid);
  41.     this.setState({
  42.       disableLoginButton: !areInputsValid
  43.     });
  44.   }
  45.  
  46.   render() {
  47.     console.log("render occured");
  48.     return (
  49.       <View style={styles.rootContainer}>
  50.         <View style={styles.loginFormContainer}>
  51.           <Container showEmpty={true}>
  52.             <EditText
  53.               hint="Username"
  54.               onChangeText={changedUsername => {
  55.                 this.setState(
  56.                   { userName: changedUsername },
  57.                   this.checkForLoginButton
  58.                 );
  59.               }}
  60.             />
  61.           </Container>
  62.           <Container showEmpty={true}>
  63.             <EditText
  64.               hint="Password"
  65.               onChangeText={password => {
  66.                 this.setState({ password: password }, this.checkForLoginButton);
  67.               }}
  68.               secureText={true}
  69.               errorHandler={this.state.passwordError}
  70.             />
  71.           </Container>
  72.           <View style={{ padding: 4 }}>
  73.             <Button
  74.               color={Color.colorPrimary}
  75.               title="Login"
  76.               onPress={this._onLoginClicked}
  77.               disabled={this.state.disableLoginButton}
  78.             />
  79.           </View>
  80.         </View>
  81.         <Text style={styles.orStyle}>- OR -</Text>
  82.         <Text style={styles.creatAccountStyle} onPress={this.props.onCreateAccountClicked}>
  83.           Create Account
  84.         </Text>
  85.       </View>
  86.     );
  87.   }
  88. }
  89.  
  90. export default LoginComponent;
  91.  
  92. const styles = StyleSheet.create({
  93.   loginFormContainer: {
  94.     marginLeft: 16,
  95.     marginRight: 16,
  96.     marginTop: 24
  97.   },
  98.   orStyle: {
  99.     color: Color.colorPrimary,
  100.     alignSelf: "center",
  101.     fontSize: 12
  102.   },
  103.   creatAccountStyle: {
  104.     color: Color.colorPrimary,
  105.     alignSelf: "center",
  106.     fontSize: 16,
  107.     textDecorationLine: "underline",
  108.     margin: 16
  109.   }
  110. });
Add Comment
Please, Sign In to add comment