Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from "react";
  2. import {
  3.     View,
  4.     Text,
  5.     StyleSheet,
  6.     TouchableOpacity,
  7.     Keyboard
  8.    
  9. } from "react-native";
  10. import BahanaIcon from '../../components/BahanaIcon'
  11. import FormLogin from '../../components/FormLogin'
  12. import GreenLogin from '../../components/GreenLogin'
  13. import LinearGradient from "react-native-linear-gradient"
  14. import {connect} from 'react-redux'
  15.  
  16. class Login extends Component {
  17.   constructor(props) {
  18.     super(props);
  19.  
  20.     this.state = {
  21.       user: '',
  22.       password: '',
  23.       datas: '',
  24.       userZ: '',
  25.       userV: '',
  26.       redux : "2"
  27.     };
  28.     this.socket = new WebSocket('wss://bahana.co.id:12000/');
  29.   }
  30.  
  31.   successLogin = () => {
  32.     this.props.login();
  33.     this.props.navigation.navigate('CobaRedux');
  34.   }
  35.  
  36.  
  37.   componentDidUpdate(prevProps, prevState){
  38.     if (this.state.userZ !== prevState.userZ){
  39.       this.successLogin();
  40.       // return this.props.navigation.navigate('CobaRedux', {
  41.       //     user: this.state.user,
  42.       //     password: this.state.password,
  43.       //   });
  44.     }
  45.   }
  46.  
  47.   componentDidMount() {
  48.     // 1. pertama kita apakah koneksi telah tersambung.(pakai this.socket.onopen)
  49.     // Tanda koneksi telah dibuka
  50.     this.socket.onopen = () => console.log('Koneksi Telah Dibuka');
  51.    
  52.     // 3.Aksi untuk mengetahui respon yang akan dilakukan setelah onSend
  53.    
  54.     this.socket.onmessage = ({data}) => {
  55.       let obj = JSON.parse(data);
  56.       this.setState({datas: obj});
  57.  
  58.       //Pengondisian untuk berhasil login
  59.       if (obj.action_type === 'REPLY-DB' && obj.sub_type === "USER_PROFILE"){
  60.         this.setState({userZ : this.state.user})
  61.         console.log('berhasil')
  62.       }if (obj.action_type === 'LOGIN-RESPONSE' && obj.status === 'OK') {
  63.        
  64.  
  65.       }
  66.        else if (obj.action_type === 'LOGIN-RESPONSE' && obj.status !== 'OK') {
  67.         console.log(this.state.datas.status)
  68.       }
  69.     };
  70.    
  71.     //Aksi jika koneksi error atau pun Ditutup
  72.     this.socket.onerror = () => {
  73.       console.log('Koneksi Error');
  74.     };
  75.     this.socket.onclose = () => {
  76.       console.log('koneksi ditutup');
  77.     };
  78.   }
  79.  
  80.   render() {
  81.     // 2. kita kirim user and password ke server(dengan this.socket.send)
  82.     //fungsi untuk merequets LOGIN via ws
  83.     const sendAuth = () => {
  84.       Keyboard.dismiss();
  85.       this.socket.send(
  86.         JSON.stringify({user: this.state.user, password: this.state.password}),
  87.       );
  88.     };
  89.  
  90.     return (
  91.      <LinearGradient colors={['#10101F',  '#2D3152']} style={styles.container}>
  92.      
  93.         {/* Komponen untuk Simbol Bahana */}
  94.         <BahanaIcon />
  95.         <View>
  96.           {/* Form Username */}
  97.           <FormLogin
  98.             icon="account-circle"
  99.             placeholder="Username"
  100.             security= {false}
  101.             onChangeText={user => this.setState({user})}
  102.           />
  103.  
  104.           {/* Form Password  */}
  105.           <FormLogin
  106.             icon="lock"
  107.             placeholder="Password"
  108.             security={true}
  109.             onChangeText={password => this.setState({password})}
  110.           />
  111.  
  112.           {/* Lupa Password Aksi */}
  113.           <View style={styles.forgotPassword}>
  114.             <Text style={{color: 'white'}}>Forgot your Password?</Text>
  115.             <TouchableOpacity
  116.               onPress={() => this.props.navigation.navigate('ForgotPass')}>
  117.               <Text style={{color: '#0b3f91'}}> Click here</Text>
  118.             </TouchableOpacity>
  119.           </View>
  120.         </View>
  121.  
  122.         <View>
  123.           {/* Login Aksi */}
  124.           <GreenLogin onPress={() => sendAuth()} title="Login" />
  125.  
  126.           {/* Disclaimer Aksi */}
  127.           <TouchableOpacity
  128.             style={{justifyContent: 'center', alignItems: 'center'}}
  129.             onPress={() => this.props.navigation.navigate('Disclaimer')}>
  130.             <Text style={styles.Disclaimer}>Disclaimer</Text>
  131.           </TouchableOpacity>
  132.         </View>
  133.         {/* Alert Gagal Login Aksi */}
  134.         <View
  135.           style={[
  136.             styles.warningAlert,
  137.             this.state.datas.status === "FAILED"
  138.               ? styles.alertShow
  139.               : styles.alertNone,
  140.           ]}>
  141.           <Text
  142.             style={{color: 'white', fontWeight: '300', textAlign: 'center'}}>
  143.             The username or password did not match our records.Please try again
  144.           </Text>
  145.         </View>
  146.  
  147.         {/* SignUp Aksi */}
  148.         <View style={styles.signUp}>
  149.           <Text style={{color: 'white'}}>New to BIPS? </Text>
  150.           <TouchableOpacity onPress={() => alert('belum ditentukan')}>
  151.             <Text style={{color: '#0b3f91'}}>Sign Up</Text>
  152.           </TouchableOpacity>
  153.         </View>
  154.      
  155.     </LinearGradient>  
  156.     );
  157.   }
  158. }
  159. export default connect(mapStateToProps,mapDispatchToProps)(Login);
  160.  
  161. // var newUser2 = this.state.userZ
  162. var newAction2 = function ({this.state.userZ}) {
  163.   return {
  164.       type: 'login',
  165.       newUser: newUser2
  166.   }
  167. }
  168.  
  169.  
  170. function mapStateToProps(state){
  171.     return{
  172.         user : state.user
  173.     }
  174. }
  175.  
  176. function mapDispatchToProps(dispatch){
  177.   return{
  178.     login : () => dispatch(newAction2())
  179.   }
  180. }
  181.  
  182. const styles = StyleSheet.create({
  183.   container: {
  184.     flex: 1,
  185.    
  186.     padding: 5,
  187.     alignItems: 'center',
  188.     justifyContent: 'space-around',
  189.   },
  190.   forgotPassword: {
  191.     flexDirection: 'row',
  192.     justifyContent: 'center',
  193.   },
  194.   Disclaimer: {
  195.     color: '#0b3f91',
  196.   },
  197.   signUp: {
  198.     flexDirection: 'row',
  199.   },
  200.   warningAlert: {
  201.     display: 'none',
  202.     backgroundColor: 'red',
  203.     height: 50,
  204.     width: '80%',
  205.     justifyContent: 'center',
  206.   },
  207.   alertShow: {
  208.     display: 'flex',
  209.   },
  210.   alertNone: {
  211.     display: 'none',
  212.   },
  213. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement