Advertisement
social1986

Untitled

Jan 12th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react';
  2. import { Paths } from '../../config/paths';
  3.  
  4. import {
  5.     StyleSheet,
  6.     View,
  7.     Image,
  8.     Text,
  9.     TextInput,
  10.     TouchableOpacity,
  11.     KeyboardAvoidingView,
  12.     ScrollView,
  13.     Alert,
  14.     AsyncStorage
  15. } from 'react-native';
  16. import { images } from '../../assets/images/Images';
  17. import ButtonOpacity from '../Buttons/buttonOpacity';
  18.  
  19. export default class Login extends Component {
  20.     static navigationOptions = {
  21.         headerTitle: '', // "Login Menu",
  22.         headerTintColor: '#2980b9',
  23.         headerStyle: { elevation: 0, shadowOpacity: 0 }
  24.     };
  25.  
  26.     constructor(props) {
  27.         super(props);
  28.         this.state = {
  29.             emailInput: '',
  30.             passInput: ''
  31.         };
  32.     }
  33.  
  34.     _onPressButtonTest = () => {
  35.         Alert.alert('Click Click!');
  36.     };
  37.  
  38.     _onPressButtonForgotPass = () => {
  39.         this.props.navigation.navigate('ForgotPassword');
  40.     };
  41.  
  42.     // here we save the token provided from the backend
  43.     async saveItem(item, selectedValue) {
  44.         try {
  45.             await AsyncStorage.setItem(item, selectedValue);
  46.         } catch (error) {
  47.             Alert.alert('AsyncStorage error: ' + error.message);
  48.             console.error('AsyncStorage error: ' + error.message);
  49.         }
  50.     }
  51.  
  52.     _onPressUserLogin = () => {
  53.         if (!this.state.emailInput || !this.state.passInput) return;
  54.         console.log(Paths);
  55.         fetch(Paths.loginpath, {
  56.             method: 'POST',
  57.             headers: {
  58.                 Accept: 'application/json',
  59.                 'Content-Type': 'application/json'
  60.             },
  61.             body: JSON.stringify({
  62.                 username: this.state.emailInput,
  63.                 password: this.state.passInput
  64.             })
  65.         })
  66.             .then(response => response.json())
  67.             .then(responseData => {
  68.                 Alert.alert('Login Success!');
  69.                 this.saveItem('id_token', responseData.id_token);
  70.                 this.props.navigation.navigate('YourCards');
  71.             })
  72.             .done();
  73.     };
  74.  
  75.     render() {
  76.         return (
  77.             <ScrollView style={{ backgroundColor: 'white' }}>
  78.                 <KeyboardAvoidingView behavior="padding">
  79.                     <View style={styles.logoContainer}>
  80.                         <Image style={styles.logo} source={images.logoMain} />
  81.                         <Text style={styles.logoText}>IS Retail Wallet</Text>
  82.                     </View>
  83.                     <View style={styles.container}>
  84.                         <TextInput
  85.                             underlineColorAndroid="#2980b9"
  86.                             placeholder="E-mail address"
  87.                             placeholderTextColor="black"
  88.                             returnKeyType="next"
  89.                             autoCapitalize="none"
  90.                             autoCorrect={false}
  91.                             keyboardType="email-address"
  92.                             onChangeText={emailInput => this.setState({ emailInput })}
  93.                             value={this.state.emailInput}
  94.                             onSubmitEditing={() => this.passwordInput.focus()}
  95.                             style={styles.input}
  96.                         />
  97.                         <TextInput
  98.                             underlineColorAndroid="#2980b9"
  99.                             placeholder="Password"
  100.                             secureTextEntry
  101.                             autoCapitalize="none"
  102.                             placeholderTextColor="black"
  103.                             returnKeyType="done"
  104.                             style={styles.input}
  105.                             onChangeText={passInput => this.setState({ passInput })}
  106.                             value={this.state.passInput}
  107.                             ref={input => (this.passwordInput = input)}
  108.                         />
  109.                         <ButtonOpacity
  110.                             label={'Login'}
  111.                             onPress={this._onPressUserLogin}
  112.                             disabled={!this.state.emailInput || !this.state.passInput}
  113.                             buttonStyle={
  114.                                 !this.state.emailInput || !this.state.passInput
  115.                                     ? styles.buttonStyleDisabled
  116.                                     : styles.buttonStyle
  117.                             }
  118.                             textStyle={
  119.                                 !this.state.emailInput || !this.state.passInput
  120.                                     ? styles.textStyleDisabled
  121.                                     : styles.textStyle
  122.                             }
  123.                         />
  124.                         <ButtonOpacity
  125.                             label={'Forgot password?'}
  126.                             buttonStyle={styles.forgotPassButton}
  127.                             textStyle={styles.forgotPassButtonText}
  128.                             activeOpacity={0.5}
  129.                             onPress={this._onPressButtonForgotPass}
  130.                         />
  131.                     </View>
  132.                 </KeyboardAvoidingView>
  133.             </ScrollView>
  134.         );
  135.     }
  136. }
  137.  
  138. const styles = StyleSheet.create({
  139.     container: {
  140.         flex: 1,
  141.         backgroundColor: 'white',
  142.         alignItems: 'center',
  143.         justifyContent: 'center'
  144.     },
  145.     input: {
  146.         width: '90%',
  147.         backgroundColor: 'white',
  148.         color: 'black',
  149.         marginTop: 10,
  150.         marginBottom: 10
  151.     },
  152.     logoContainer: {
  153.         flexGrow: 1,
  154.         backgroundColor: 'white',
  155.         alignItems: 'center',
  156.         justifyContent: 'center',
  157.         marginTop: 20
  158.     },
  159.     logo: {
  160.         width: 150,
  161.         height: 150
  162.     },
  163.     logoText: {
  164.         color: '#2980b9',
  165.         marginTop: 10,
  166.         textAlign: 'center',
  167.         fontFamily: 'OldStandard-Bold',
  168.         fontSize: 30
  169.     },
  170.     forgotPassButton: {
  171.         width: '50%',
  172.         marginTop: 10,
  173.         marginBottom: 10,
  174.         backgroundColor: 'white'
  175.     },
  176.     forgotPassButtonText: {
  177.         color: 'black',
  178.         textAlign: 'center'
  179.     }
  180. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement