Guest User

react-native login.js

a guest
Mar 4th, 2017
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react';
  2.  
  3. import {
  4.     View,
  5.     StyleSheet,
  6.     Text,
  7.     TouchableHighlight,
  8.     Image,
  9.     TextInput,
  10.     ActivityIndicator,
  11.     ToastAndroid
  12. } from 'react-native';
  13.  
  14. import authService from'./AuthService/AuthService';
  15.  
  16.  
  17. export default class Login extends Component {
  18.     constructor(props){
  19.         super(props);
  20.         this.state={
  21.             username:undefined,
  22.             password:undefined,
  23.             animation:false,
  24.             badCredentials:false,
  25.             success:false
  26.         };
  27. }
  28.    
  29.     render() {
  30.          var errorCtrl=<View/>
  31.          if(!this.state.success && this.state.badCredentials){
  32.                 errorCtrl=<Text style={styles.error}>that username and password combination did not work</Text>
  33.          }
  34.         return (
  35.             <View style={styles.container}>
  36.                 <Image source={require('./img/Octocat.png')} style={styles.logo} />
  37.                 <Text style={styles.heading}>Github browser</Text>
  38.                 <TextInput style={styles.input}
  39.                     placeholder={"Github username"}
  40.                     onChangeText={(text) => this.setState({username:text})}
  41.                    
  42.                     />
  43.                 <TextInput style={styles.input}
  44.                     placeholder={"Github user password"}
  45.                     secureTextEntry={true}
  46.                     onChangeText={(text)=>this.setState({password:text})}
  47.                    
  48.                     />
  49.                 <TouchableHighlight
  50.                     style={styles.button}
  51.                     onPress={this.onLoginPressed.bind(this)}
  52.                 >
  53.                 <Text style={styles.buttonText}>Log in</Text>
  54.                 </TouchableHighlight>    
  55.                     {errorCtrl}
  56.                 <ActivityIndicator
  57.                     animating={this.state.animation}
  58.                     size="large"
  59.                 />
  60.             </View>
  61.         )
  62.     }
  63.  
  64.     onLoginPressed(){
  65.         this.setState({animation:true});
  66.         authService.login({
  67.             username : this.state.username,
  68.             password: this.state.password
  69.         },(results)=>{
  70.             this.setState(results);
  71.             if(this.state.success && this.props.onLogin){
  72.                 this.props.onLogin();
  73.             }
  74.         });
  75.      }
  76. }
  77. const styles = StyleSheet.create({
  78.     container: {
  79.         flex: 1,
  80.         backgroundColor: '#F5FCFF',
  81.         paddingTop: 40,
  82.         alignItems: 'center',
  83.         padding: 10,
  84.        
  85.     },
  86.     logo: {
  87.         width: 88,
  88.         height: 77
  89.     },
  90.     heading: {
  91.         fontSize: 30,
  92.         marginTop: 10
  93.     },
  94.     input: {
  95.         height: 50,
  96.         marginTop: 10,
  97.         padding: 4,
  98.         fontSize: 18,
  99.         borderWidth: 1,
  100.         alignSelf: 'stretch',
  101.         borderColor: '#48bbec'
  102.     },
  103.  
  104.     button:{
  105.         height:50,
  106.         backgroundColor:"#48BBEC",
  107.         alignSelf:'stretch',
  108.         marginTop:10,
  109.         justifyContent:'center',
  110.  
  111.     },
  112.     buttonText:{
  113.         fontSize:22,
  114.         color:"#fff",
  115.         alignSelf:'center'
  116.     },
  117.    error:{
  118.        color:'red',
  119.        paddingTop: 10
  120.    }
  121.  
  122. })
Advertisement
Add Comment
Please, Sign In to add comment