Advertisement
djbuzro

login.js

Jan 18th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react';
  2. import { connect } from 'react-redux';
  3. import {
  4.     AppRegistry,
  5.     StyleSheet,
  6.     TextInput,
  7.     View,
  8.     Alert,
  9.     Button,
  10.     Text,
  11.     AsyncStorage,
  12.     Image,
  13.     TouchableOpacity,
  14.     ActivityIndicator,
  15.     KeyboardAvoidingView
  16. } from 'react-native';
  17. import axios from 'axios';
  18. import Ionicons from 'react-native-vector-icons/Ionicons';
  19. import Setting from '../../Setting';
  20.  
  21. class LoginScreen extends Component {
  22.     constructor(props) {
  23.         super(props)
  24.         this.state = {
  25.             username: '',
  26.             password: '',
  27.             isLoading: false
  28.         }
  29.  
  30.  
  31.         this.root = this.props.component.root;
  32.  
  33.         //save token
  34.         //this.root.saveToken('asdasd');
  35.  
  36.         //ambil token
  37.         //this.root.state.token
  38.     }
  39.  
  40.     UserLoginFunction = (username,password) => {
  41.  
  42.         // const { username } = this.state;
  43.         // const { password } = this.state;
  44.  
  45.         this.setState({
  46.             isLoading: true
  47.         });
  48.         axios({
  49.             method: 'get',
  50.             url: Setting.baseUrl + '/auth/login',
  51.  
  52.             params: {
  53.                 email: username,
  54.                 password: password,
  55.             }
  56.         }).then((response) => {
  57.            
  58.             const token = response.data.data.token;
  59.  
  60.             if (token != '') {
  61.                 let str = token;
  62.                 this.root.saveToken(str)
  63.                
  64.             }
  65.             else {
  66.                 Alert.alert(response);
  67.             }
  68.             this.setState({
  69.                 isLoading: false
  70.             });
  71.         }).catch((error) => {
  72.             alert("Username / Password salah !");
  73.             this.setState({
  74.                 isLoading: false
  75.             });
  76.         });
  77.     }
  78.  
  79.     render() {
  80.         return (
  81.             <View style={{flexDirection:'column',flex:1,maxHeight:600}}>
  82.             <View style={{
  83.                 alignItems:'center',alignContent:'center',flexDirection:'column',flex:1
  84.             }}>
  85.             {/* gambar backgroundColor */}
  86.             <Image
  87.             source={require('../../img/background.jpg')}
  88.             style={{
  89.                 position: 'absolute',
  90.                 width: '100%',
  91.                 height: '100%',
  92.                 resizeMode:'cover'
  93.             }}/>
  94.             {/* gambar logo */}
  95.                 <Image
  96.                     style ={{
  97.                         width:120,height:120,marginTop:80
  98.                     }}
  99.                     source = {require('./../../img/icon.png')}
  100.                 />
  101.                     {/* username */}
  102.                     <View style={{marginTop:150}}/>
  103.                 <View style={{
  104.                     flexDirection:'row',marginLeft:20,marginRight:20, borderColor:'#F2526E',borderWidth:1,borderRadius:5,
  105.                     paddingLeft:20,alignContent:'center',alignItems:'center',width:270,backgroundColor:'#fff'
  106.                 }}>
  107.                 <Ionicons name='ios-person-outline'
  108.                 color={'#F2526E'}
  109.                 size={30}
  110.                 MarginLeft={10}/>
  111.  
  112.             <TextInput
  113.         placeholder="Username"
  114.                     onChangeText={username => this.setState({ username })}
  115.                     underlineColorAndroid='transparent'
  116.                     style={styles.Textform}
  117.                 />
  118.  
  119.                 </View>
  120.                 {/* end of username */}
  121.                 {/* password */}
  122.                 <View style={{
  123.                     marginTop:10,
  124.                     flexDirection:'row',marginLeft:20,marginRight:20, borderColor:'#F2526E',borderWidth:1,borderRadius:5,
  125.                     paddingLeft:20,alignContent:'center',alignItems:'center',width:270,backgroundColor:'#fff'
  126.                 }}>
  127.                 <Ionicons name='ios-lock-outline'
  128.                 color={'#F2526E'}
  129.                 size={30}
  130.                 MarginLeft={10}/>
  131.  
  132.             <TextInput
  133.         placeholder="password"
  134.                     onChangeText={username => this.setState({ username })}
  135.                     underlineColorAndroid='transparent'
  136.                     style={styles.Textform}
  137.                     secureTextEntry={true}
  138.                 />
  139.                 </View>
  140.                
  141.                 {/* end of passsword */}
  142.                 <View style={{marginTop:20}}></View>
  143.                 {this.state.isLoading == true && (
  144.                     <View style={styles.container}>
  145.                         <ActivityIndicator size="large" />
  146.                     </View>
  147.                 )}
  148.                 {this.state.isLoading == false && (
  149.                     <TouchableOpacity onPress={()=>{this.UserLoginFunction(this.state.username,this.state.password)}}>
  150.                         <View style={styles.button}>
  151.                             <Text style={styles.TextInputStyleClass}>
  152.                                 Login
  153.                                 </Text>
  154.                         </View>
  155.                     </TouchableOpacity>
  156.                 )}
  157.                 {this.state.isLoading == false && (
  158.                     <TouchableOpacity onPress={() => {
  159.                         this.props.component.root.navigate('Register', {
  160.                         });
  161.                       }}>
  162.  
  163.                         <View style={styles.buttonRegister}>
  164.                         <Text style={styles.TextInputStyleClass}>
  165.                                 Register
  166.                                 </Text>
  167.                         </View>
  168.                     </TouchableOpacity>
  169.                 )}
  170.                 {/* end of parent */}
  171.                
  172.                 <Text
  173.                 style={{
  174.                 fontSize:10,
  175.                 width:270,
  176.                 textAlign:'center',
  177.                 color: '#F2526E'
  178.                 }}>
  179.                     dengan masuk berarti anda sudah menyetujui syarat dan ketentuan kami
  180.                 </Text>
  181.                 </View>              
  182.             </View>
  183.  
  184.         );
  185.     }
  186. }
  187.  
  188. const styles = StyleSheet.create({
  189.     container: {
  190.         alignItems: "center"
  191.     },
  192.     TextInputStyleClass:{
  193.         flex:1,
  194.         fontSize:20,
  195.         color:'#FFF',
  196.         textAlign:'center',
  197.         justifyContent:'center',
  198.         marginTop:10,
  199.         // alignItems:'center',
  200.     },
  201.     Textform:{
  202.         flex:1,
  203.         fontSize:17,
  204.         color:'#F2526E',
  205.         marginLeft:20
  206.     },
  207.     button:{
  208.         backgroundColor:'#F2526E',
  209.         borderColor:'#F2526E',
  210.         borderRadius:5,
  211.         width:270,
  212.         height:50,  
  213.         marginBottom:15,
  214.     },
  215.     buttonRegister:{
  216.         backgroundColor:'#2ECC71',
  217.         borderColor:'#2ECC71',
  218.         borderRadius:5,
  219.         width:270,
  220.         height:50,  
  221.         marginBottom:15
  222.     },
  223. });
  224.  
  225. function mapStateToProps(state) {
  226.     return {
  227.         component: state.component,
  228.     }
  229. }
  230.  
  231. function mapDispatchToProps(dispatch) {
  232.     return {
  233.         setRoot: (root) => dispatch({
  234.             type: 'set_root',
  235.             root: root
  236.         })
  237.     }
  238. }
  239.  
  240. export default connect(
  241.     mapStateToProps,
  242.     mapDispatchToProps
  243. )(LoginScreen);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement