Advertisement
Guest User

Login Screen

a guest
Jan 20th, 2020
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, {Component} from 'react';
  2. import {View, Text, Image, TextInput, TouchableOpacity, ActivityIndicator} from 'react-native';
  3.  
  4. import styles from './styles';
  5. import Images from 'res/images';
  6. import Constant from 'res/constant';
  7. import Strings from 'res/strings';
  8. import Colors from 'res/colors';
  9. import { ScrollView } from 'react-native-gesture-handler';
  10. import AsyncStorage from '@react-native-community/async-storage';
  11. import axios from 'axios';
  12. import Modal from 'react-native-modal';
  13. import Toast from 'react-native-simple-toast';
  14. // import Icon from 'react-native-vector-icons/Entypo';
  15.  
  16.  
  17. export default class LoginScreen extends React.Component {
  18.   constructor (props) {
  19.     super (props);
  20.  
  21.     this.state = {
  22.       // secureTextEntry: true,
  23.  
  24.       alertVisibility: false,
  25.       alertContentTitle: Strings.request_failed,
  26.       alertContentInfo: Strings.email_not_valid,
  27.  
  28.       loadingVisibility: false,
  29.       nik:'',
  30.       password:''
  31.     }
  32.  
  33.     // this.isLogin();
  34.   }
  35.  
  36.   // async isLogin() {
  37.   //   const { navigation } = this.props;
  38.   //     navigation.navigate('Dashboard');
  39.   // }
  40.  
  41.   handleEmail(val) {
  42.     this.setState({ nik: val });
  43.   }
  44.  
  45.   handlePassword(val) {
  46.     this.setState({ password: val });
  47.   }
  48.  
  49.   validateLogin(nik, password) {
  50.  
  51.     if (nik === '') {
  52.       this.setState({
  53.         alertContentTitle: Strings.login_failed,
  54.         alertContentInfo: Strings.email_empty,
  55.         alertVisibility: true
  56.       });
  57.     }
  58.    
  59.     else if (password === '') {
  60.       this.setState({
  61.         alertContentTitle: Strings.login_failed,
  62.         alertContentInfo: Strings.password_empty,
  63.         alertVisibility: true
  64.       });
  65.     } else {
  66.       this.login();
  67.     }
  68.   }
  69.  
  70.   login() {
  71.     const { navigation } = this.props;
  72.     const { nik, password } = this.state;
  73.  
  74.     this.setState({ loadingVisibility: true });
  75.  
  76.     const body = {
  77.       nik,
  78.       password
  79.     };
  80.  
  81.     axios.post(`${Constant.BASE_URL}`, body)
  82.       .then((response) => {
  83.         console.log('res',response);
  84.         if (response.data.message === true) {
  85.           console.log('Success Login');
  86.          
  87.           // Login data
  88.           AsyncStorage.setItem('userData', response.data);
  89.           AsyncStorage.setItem('user_id', response.data.data.user_id);
  90.           AsyncStorage.setItem('user_name', response.data.data.user_name);
  91.           AsyncStorage.setItem('user_email', response.data.data.user_email);
  92.           AsyncStorage.setItem('user_desc', response.data.data.user_desc);
  93.           AsyncStorage.setItem('bu_org', response.data.data.bu_org);
  94.           AsyncStorage.setItem('cmpy_code', response.data.data.cmpy_code.toString());
  95.           AsyncStorage.setItem('cmpy_name', response.data.data.cmpy_name);
  96.           AsyncStorage.setItem('token', response.data.data.token);
  97.  
  98.  
  99.           navigation.replace('Dashboard');
  100.           this.setState({ loadingVisibility: false });
  101.         } else {
  102.           this.setState({ loadingVisibility: false });
  103.           Toast.show(response.data.message, Toast.LONG);
  104.           console.log('failed_login:', response.data.status);
  105.         }
  106.       })
  107.       .catch((error) => {
  108.         this.setState({ loadingVisibility: false });
  109.         Toast.show(error.response.data.error_msg, Toast.LONG);
  110.         console.log('error_login:', error);
  111.       });
  112.   }
  113.  
  114.   // checkIsNullInt(param) {
  115.   //   if (param != null) {
  116.   //     return param;
  117.   //   }
  118.  
  119.   //   return 0;
  120.   // }
  121.  
  122.   // checkIsNullString(param) {
  123.   //   if (param != null) {
  124.   //     return param;
  125.   //   }
  126.  
  127.   //   return '';
  128.   // }
  129.  
  130.  
  131.     render() {
  132.  
  133.       const {
  134.         nik, password, alertVisibility, alertContentTitle, alertContentInfo, loadingVisibility
  135.       } = this.state;
  136.         return(
  137.           <View>
  138.             <ScrollView>
  139.             <View style={styles.viewContainer}>
  140.               <View style={{flexDirection:'row'}}>
  141.                 <Text style={styles.txtTitle}>Mobile </Text>
  142.                 <Text style={styles.txtTitleCRM}>CRM</Text>
  143.               </View>
  144.               <Image source={Images.user} style={styles.imgUser}></Image>
  145.  
  146.               <TextInput style={styles.InputTxt}
  147.                 placeholder="  NIK"
  148.                 onChangeText={(val) => this.handleEmail(val)}
  149.               />
  150.  
  151.               <View >
  152.                 <TextInput style={styles.InputTxt}
  153.                   placeholder="  Password"
  154.                   onChangeText={(val) => this.handlePassword(val)}
  155.                 />
  156.                
  157.  
  158.                   {/* <TouchableOpacity
  159.                   style={styles.containerIconEye}
  160.                   onPress={() => this.setState({ secureTextEntry: !secureTextEntry })}
  161.                 >
  162.                   <Icon
  163.                     style={styles.iconEye}
  164.                     name={this.state.secureTextEntry === true ? 'eye' : 'eye-with-line'}
  165.                     size={20}
  166.                   />
  167.                 </TouchableOpacity> */}
  168.                
  169.               </View>
  170.  
  171.               <TouchableOpacity onPress={() => this.validateLogin(nik, password)} style={styles.btnLogin}>
  172.                   <Text style={{textAlign:'center', marginTop:10}}>Login</Text>
  173.               </TouchableOpacity>
  174.  
  175.               {/* <TouchableWithoutFeedback >
  176.                 <View style={{marginTop:10}}>
  177.                   <Text style={{color:'#3642ad', textAlign:'center'}}>forgot password</Text>
  178.                   <View style={{height:1, width:105, backgroundColor:'#3642ad', alignSelf:'center'}}></View>
  179.                 </View>
  180.               </TouchableWithoutFeedback> */}
  181.  
  182.               <View style={{flexDirection:'row', marginTop:120}}>
  183.                 <Text>Having Issue? </Text>
  184.                 <Text>please contact</Text>
  185.               </View>
  186.               <Text style={{color:'#3642ad', textAlign:'center'}}>mis@sigma.co.id</Text>
  187.               <View style={{height:1, width:105, backgroundColor:'#3642ad', alignSelf:'center'}}></View>
  188.  
  189.             </View>
  190.             </ScrollView>
  191.  
  192.             <Modal
  193.             Visible={alertVisibility}
  194.             backdropColor={Colors.blackTransparent}
  195.             onBackdropPress={() => { this.setState({ alertVisibility: false }); }}
  196.             onRequestClose={() => { this.setState({ alertVisibility: false }); }}
  197.             style={styles.containerAlert}
  198.           >
  199.             <View style={styles.containerContentInAlert}>
  200.               {/* Title */}
  201.               <Text style={styles.titleInAlert}>{alertContentTitle}</Text>
  202.  
  203.               {/* Information */}
  204.               <Text style={styles.informationInAlert}>{alertContentInfo}</Text>
  205.  
  206.               {/* Button OK */}
  207.               <TouchableOpacity onPress={() => { this.setState({ alertVisibility: false }); }}>
  208.                 <Text style={styles.ok}>{Strings.ok}</Text>
  209.               </TouchableOpacity>
  210.             </View>
  211.           </Modal>
  212.  
  213.             {/* Loading */}
  214.             <Modal
  215.               visible={loadingVisibility}
  216.               animationType="fade"
  217.               backdropColor={Colors.blackTransparent}
  218.             >
  219.               <View style={styles.loading}>
  220.                 <ActivityIndicator size="large" color={Colors.blueButton} />
  221.               </View>
  222.             </Modal>
  223.           </View>
  224.         );
  225.     }
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement