Advertisement
Guest User

Untitled

a guest
Jul 25th, 2018
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Login component
  2.  
  3. import React from 'react';
  4. import { View,Image, Text,ToastAndroid, StyleSheet, TextInput,Alert, AsyncStorage,TouchableOpacity,ActivityIndicator,Linking } from 'react-native';
  5. import { Item,Container,Content,Form,Input,Icon,Button } from 'native-base';
  6. import { Font } from 'expo';
  7. import Dimensions from 'Dimensions';
  8. import { LinearGradient } from 'expo';
  9. //import startMainTabs from './Main';
  10.  
  11.  
  12.  
  13. //width-height of device
  14. const DEVICE_WIDTH = Dimensions.get('window').width;
  15. const DEVICE_HEIGHT = Dimensions.get('window').height;
  16.  
  17. export default class Login extends React.Component {
  18.  
  19.   constructor(props) {
  20.     super(props);
  21.    
  22.    
  23.     this.state = {
  24.       username: 'api',
  25.       password: 'api1234',
  26.       pin: '1267',
  27.       loginRight: false,
  28.       click: false,
  29.       errors: '',
  30.       allData: false,
  31.     };
  32.  
  33.     //await AsyncStorage.removeItem('user');
  34.   }
  35.   onLostPassPress() {
  36.  
  37.   }
  38.   onLogin() {
  39.     const { navigate } = this.props.navigation;
  40.  
  41.     this.setState({
  42.       click: true,
  43.     });
  44.  
  45.  
  46.     _storeData = async (user) => {
  47.         try {
  48.           //await AsyncStorage.removeItem('user');
  49.           await AsyncStorage.setItem('user', JSON.stringify(user));
  50.         } catch (error) {
  51.           // Error saving data
  52.         }
  53.       }
  54.  
  55.  
  56.     //method for API call
  57.     const checkLoginData = async () => {
  58.       await fetch('http://192.168.0.108/login/', {
  59.         method: 'POST',
  60.         headers: {
  61.           Accept: 'application/json',
  62.           'Content-Type': 'application/json',
  63.         },
  64.         body: JSON.stringify({
  65.           user: this.state.username,
  66.           password: this.state.password,
  67.           pin: this.state.pin,
  68.         }),
  69.       }).then((response) => response.json())
  70.         .then((json) => {
  71.           //alert(json);
  72.           // api response user?
  73.           if(json.user != null)
  74.           {
  75.             this.setState({
  76.               loginRight: true,
  77.  
  78.             });
  79.             var obj = {
  80.               id: json.user.id,
  81.               lastname: json.user.lastname,
  82.               name: json.user.name,
  83.               email: json.user.email,
  84.               user: json.user.user,
  85.             };
  86.             _storeData(obj);
  87.             //wait 1sec before open Main screen
  88.              //navigate("Main",{user: JSON.stringify(obj)});
  89.             setTimeout(() => {
  90.               navigate("Main",{user: JSON.stringify(obj)});
  91.               /*navigate('Main', {} , {
  92.                 type: 'Navigate',
  93.                 routeName: 'Main',
  94.                 action: {
  95.                   type: 'Navigate',
  96.                   routeName: 'Domov',
  97.                   params: {user: JSON.stringify(obj)}
  98.                 }
  99.               }); */
  100.  
  101.               //set state
  102.               this.setState({
  103.                 loginRight: false,
  104.                 click: false,
  105.               });
  106.  
  107.               //clear fields after succesfull login
  108.               this.refs.textInputUser._root.clear();
  109.               this.refs.textInputPass._root.clear();
  110.               this.refs.textInputPin._root.clear();
  111.             },1300);  
  112.  
  113.            
  114.  
  115.             //navigate("Main", {ime: user});
  116.  
  117.           }
  118.           else if (json.errors)
  119.           {
  120.             //if errors
  121.             //alert(json.errors);
  122.             this.setState({
  123.               loginRight: false,
  124.               errors: json.errors,
  125.  
  126.             });
  127.  
  128.             //console.log(this.refs.textInputPass._root);
  129.             this.refs.textInputPass._root.clear();
  130.             this.refs.textInputPin._root.clear();
  131.             //this.refs.textInputPass.clear();
  132.             //this.textInputPin.clear();
  133.  
  134.           }
  135.         })
  136.         .catch((error) => {
  137.           console.error(error);
  138.           this.setState({
  139.             loginRight: false,
  140.           });
  141.         });
  142.     };
  143.  
  144.     //check if input is set(not empty) && api call
  145.     if(this.state.username != '' && this.state.password != '' && this.state.pin != '') {
  146.       this.setState({
  147.         allData: true,
  148.       });
  149.       checkLoginData();
  150.     }
  151.    
  152.   }  
  153.   render() {
  154.     return (
  155.       <LinearGradient
  156.           colors={["#48BFD3", "#269BB9","#087CA3"]}
  157.           start={[1,0]}
  158.           end={[0,1]}
  159.           //colors={["#087CA3","#269BB9","#48BFD3"]}
  160.           style={{flex: 1}}>
  161.  
  162.           <View style={styles.container}>
  163.  
  164.  
  165.            <Text style={styles.appName}>eGŠ Učitelj</Text>
  166.            
  167.        
  168.           {
  169.             !this.state.loginRight && this.state.click && this.state.allData &&
  170.            
  171.             <View style={styles.errorMsg}>
  172.               <Text style={styles.errorHeader}>{this.state.errors}</Text>
  173.               <View style={styles.line}/>
  174.               <Text>
  175.                {`Popravite svoj vnos in poskusite\nznova! `}
  176.               </Text>
  177.             </View>
  178.           }
  179.               <Item>
  180.                 <Icon active name='person' style={{fontSize: 20, color: '#FFF'}}/>
  181.                 <Input
  182.                   placeholder='Uporabniško ime'
  183.                   placeholderTextColor="rgba(255,255,255,0.5)"
  184.                   value={this.state.username}
  185.                   onChangeText={(username) => this.setState({ username })}
  186.                   ref='textInputUser'
  187.                   //ref={input => {this.textInputUsr = input}}
  188.                   //getRef={(input) => this.textInputUsr = input}
  189.  
  190.                   style={styles.input} />
  191.  
  192.               </Item>
  193.  
  194.               {
  195.                 !this.state.username && this.state.click && !this.state.allData && <Text style={styles.txtIfEmpty}>Vnesite uporabniško ime</Text>
  196.               }
  197.    
  198.            
  199.               <Item>              
  200.                 <Icon active name='key' style={{fontSize: 20, color: '#FFF'}} />
  201.                 <Input
  202.                   value={this.state.password}
  203.                   onChangeText={(password) => this.setState({ password })}
  204.                   placeholder={'Geslo'}
  205.                   placeholderTextColor="rgba(255,255,255,0.5)"
  206.                   secureTextEntry={true}
  207.                   underlineColorAndroid="transparent"
  208.                   //ref={input => {this.textInputPass = input}}
  209.                   //getRef={(input) => this.textInputPass = input}
  210.                   ref='textInputPass'
  211.                   style={styles.input}/>
  212.               </Item>
  213.  
  214.               {
  215.                 !this.state.password && this.state.click && !this.state.allData && <Text style={styles.txtIfEmpty}>Vnesite geslo</Text>
  216.               }
  217.  
  218.               <Item>              
  219.                 <Icon active name='lock' style={{fontSize: 20, color: '#FFF'}}/>
  220.                 <Input
  221.                   value={this.state.pin}
  222.                   onChangeText={(pin) => this.setState({ pin })}
  223.                   placeholder={'Pin'}
  224.                   placeholderTextColor="rgba(255,255,255,0.5)"                  
  225.                   //placeholderStyle={{opacity: 0.5}}
  226.                   keyboardType="numeric"
  227.                   secureTextEntry={true}
  228.                   underlineColorAndroid="transparent"
  229.                   //ref={input => {this.textInputPin = input}}
  230.                   //getRef={(input) => this.textInputPin = input}
  231.                   ref='textInputPin'
  232.                   style={styles.input}/>
  233.               </Item>
  234.  
  235.               {
  236.                 !this.state.pin && this.state.click && !this.state.allData && <Text style={styles.txtIfEmpty}>Vnesite pin</Text>
  237.               }
  238.  
  239.              
  240.               {
  241.                 !this.state.loginRight &&
  242.                 <TouchableOpacity
  243.                         style={styles.btn}
  244.                         color='#033D51'
  245.                         onPress={this.onLogin.bind(this)}
  246.                         //onPress={() => this.props.navigation.navigate("Main",{user: 'neki'})}
  247.                     >
  248.                     <Text style={styles.txtBtn}>VPIS</Text>
  249.                 </TouchableOpacity> ||
  250.                  <ActivityIndicator size="large" color="#033D51" style={{marginTop: 30, height: 30}}/>
  251.               }
  252.                
  253.              
  254.  
  255.             <TouchableOpacity                                              
  256.                         onPress={ () => {Linking.openURL('https://www.eglasbenasola.si/login/forgotten_password')}}
  257.                     >
  258.                     <Text style={styles.txtLost}>Ste pozabili geslo?</Text>
  259.             </TouchableOpacity>
  260.            
  261.              
  262.          </View>
  263.  
  264.     </LinearGradient>
  265.      
  266.        
  267.     );
  268.   }
  269. }
  270.  
  271. //style for this screen
  272. const styles = StyleSheet.create({
  273.   container: {
  274.     flex: 1,
  275.     alignItems: 'center',
  276.     justifyContent: 'center',
  277.     paddingLeft: 30,
  278.     paddingRight: 30,
  279.    
  280.   },
  281.   errorMsg: {
  282.     position: 'absolute',
  283.     top: '22%',
  284.     paddingLeft: 15,
  285.     paddingRight: 15,
  286.     backgroundColor: '#fff',
  287.     borderRadius: 8,
  288.     shadowOffset: {
  289.       width: 5,
  290.       height: 5
  291.     },
  292.     shadowOpacity: 8,
  293.     shadowColor: '#000'
  294.   },
  295.   errorHeader: {
  296.     fontSize: 20,
  297.     fontWeight: 'bold',
  298.   },
  299.     line: {
  300.     borderBottomColor: 'black',
  301.     borderBottomWidth: 2,
  302.     marginBottom: 10,
  303.   },
  304.   appName: {
  305.     paddingTop: 25,
  306.     paddingBottom: 25,
  307.     fontSize: 24,
  308.     fontWeight: 'bold',
  309.     color: '#FFF',
  310.   },
  311.   txtIfEmpty: {
  312.     justifyContent: 'flex-start',
  313.     alignSelf: 'flex-start',
  314.   },
  315.   input: {
  316.     color: '#FFF',
  317.    
  318.   },
  319.   txtBtn: {
  320.     fontSize: 22,
  321.     textAlign: 'center',
  322.     fontWeight:'bold',
  323.     justifyContent: 'center',
  324.     textAlign: 'center',
  325.     color: '#FFF',
  326.   },
  327.   txtLost: {
  328.     fontSize: 18,
  329.     fontWeight: 'bold',
  330.     color: '#FFF',
  331.     paddingTop: 15,
  332.     textDecorationLine: 'underline',
  333.     textDecorationStyle: 'double',
  334.     textDecorationColor: '#FFF',
  335.     justifyContent: 'center',
  336.  
  337.   },
  338.   btn: {
  339.     marginTop: 30,
  340.     height: 55.6,
  341.     //flexDirection: 'row',
  342.     backgroundColor: '#033D51',
  343.     justifyContent: 'center',
  344.     borderRadius: 15,
  345.     width: 300,
  346.     shadowOffset: {
  347.       width: 3,
  348.       height: 3,
  349.     },
  350.     shadowRadius: 8,
  351.    
  352.   },
  353.  
  354. });
  355.  
  356.  
  357. //Main component
  358. import React from 'react';
  359. import { BackHandler,
  360.     ToastAndroid,Text, View,StyleSheet,TouchableOpacity,Button,ScrollView,AsyncStorage,Alert } from 'react-native';
  361. import Ionicons from 'react-native-vector-icons/Ionicons';
  362. import TicketDiaryNews from '../../components/ticketMainScreen';
  363.  
  364.  
  365.  
  366. export default class HomeScreen extends React.Component {
  367.  
  368.  
  369.    
  370.  
  371.  
  372.     constructor(props) {
  373.         super(props);
  374.  
  375.         this.state = {
  376.             user: this.retrieveItem('user'),
  377.         }  
  378.  
  379.         alert(JSON.stringify(this.props.navigation.state));
  380.        
  381.     }
  382.  
  383.     componentDidMount() {
  384.         BackHandler.addEventListener('hardwareBackPress', this.handleBackButton);
  385.     }
  386.  
  387.     componentWillUnmount() {
  388.         BackHandler.removeEventListener('hardwareBackPress', this.handleBackButton);
  389.     }
  390.  
  391.     handleBackButton() {
  392.         ToastAndroid.show('Back button is pressed', ToastAndroid.SHORT);
  393.         return true;
  394.     }
  395.  
  396.    async retrieveItem(key) {
  397.     try {
  398.       const retrievedItem =  await AsyncStorage.getItem(key);
  399.       const item = JSON.parse(retrievedItem);
  400.       return item;
  401.     } catch (error) {
  402.       console.log(error.message);
  403.     }
  404.     return
  405.   }
  406.  
  407.  
  408.     render() {
  409.             return (
  410.                 <View style={styles.background}>
  411.                   <View style={styles.mainContent}>
  412.                   <ScrollView>
  413.  
  414.                     <View style={styles.header}>
  415.                         <View style={styles.idSchool}>
  416.                             <Text style={styles.textStyle}>Ime sole </Text>
  417.                         </View>
  418.                         <View  style={styles.headerYear}>
  419.                             <Text style={styles.textStyle}>Solsko leto</Text>      
  420.                         </View>              
  421.                     </View>
  422.                     <View style={styles.line} />
  423.  
  424.  
  425.                     <TicketDiaryNews navigation={this.props.navigation}/>
  426.  
  427.                     <TicketDiaryNews navigation={this.props.navigation}/>
  428.  
  429.                     <TicketDiaryNews navigation={this.props.navigation}/>
  430.  
  431.                     </ScrollView>
  432.                      
  433.                   </View>
  434.                 </View>
  435.            
  436.            
  437.             );
  438.  
  439.    
  440.   }
  441. }
  442.  
  443. const styles = StyleSheet.create({
  444.     background: {
  445.         flex:1,
  446.         backgroundColor: '#269BB9',
  447.     },
  448.     mainContent: {
  449.         flex: 1,
  450.         borderRadius: 15,
  451.         borderBottomLeftRadius: 0,
  452.         borderBottomRightRadius: 0,
  453.         shadowOffset: {
  454.             width: 3,
  455.             height: 3,
  456.         },
  457.         shadowRadius: 8,
  458.         backgroundColor: 'white',
  459.     },
  460.     header: {
  461.         marginTop: 15,
  462.         marginLeft: 30,
  463.         marginRight: 30,
  464.         flexDirection: 'row',
  465.        
  466.     },
  467.     idSchool: {
  468.         alignSelf: 'flex-start',
  469.     },
  470.     headerYear: {
  471.         position:'absolute',
  472.         right: 0,
  473.  
  474.     },
  475.     textStyle: {
  476.         fontSize: 17,
  477.         fontWeight: '600', //semi bold
  478.     },
  479.     line: {
  480.         marginRight: 30,
  481.         marginLeft: 30,
  482.         borderBottomColor: 'black',
  483.         borderBottomWidth: 2,
  484.     },
  485.  
  486. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement