Advertisement
Guest User

Untitled

a guest
May 16th, 2017
533
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react';
  2. import { Button, Card, CardSection, Input, Spinner } from './common';
  3. import { Text } from 'react-native';
  4. import firebase from 'firebase';
  5. import { Actions } from 'react-native-router-flux';
  6.  
  7. class RegisterForm extends Component {
  8.   state = { email: '',
  9.   password: '',
  10.   passwordRepeat: '',
  11.   error: '',
  12.   loading: false,
  13.   loggedIn: false
  14.  };
  15.  // componentWillUpdate(nextProps, nextState){
  16.  //    if (nextState.loggedIn == true && this.state.loggedIn == false) {
  17.  //      Actions.Home();
  18.  //    }
  19.  // }
  20.   onButtonPress(){
  21.     Actions.login();
  22.   }
  23.  
  24.   onRegisterButtonPress(){
  25.     const { email, password, passwordRepeat } = this.state;
  26.  
  27.     this.setState({erorr:'', loading: true})
  28.  
  29.     if(this.state.password === this.state.passwordRepeat){
  30.       firebase.auth().createUserWithEmailAndPassword(email, password)
  31.         .then(this.onRegisterSuccess.bind(this))
  32.         .catch ( this.onRegisterFail.bind(this));
  33.     }
  34.   }
  35.  
  36.   onRegisterFail() {
  37.     this.setState({ error:'Authentication Failed', loading: false})
  38.   }
  39.   onRegisterSuccess(){
  40.     this.setState({
  41.       email: '',
  42.       password: '',
  43.       passwordRepeat: '',
  44.       loading: false,
  45.       error: '',
  46.       loggedIn: true
  47.     });
  48.     this.onLoggedIn();
  49.   }
  50.   onLoggedIn(){
  51.     console.log("blisko");
  52.     Actions.home();
  53.   }
  54.  
  55.  
  56.   renderButton() {
  57.     if(this.state.loading) {
  58.       return <Spinner size = {'small'} />;
  59.     }
  60.     console.log(this.state.loggedIn);
  61.     return(
  62.     <Button onPress={this.onButtonPress.bind(this)}>
  63.       Log in
  64.     </Button>
  65.   );
  66. }
  67. renderRegisterButton() {
  68.    if(this.state.loading) {
  69.       return <Spinner size = {'small'} />;
  70.     }
  71.     console.log(this.state.loggedIn);
  72.     return(
  73.     <Button onPress={this.onRegisterButtonPress.bind(this)}>
  74.       Register
  75.     </Button>
  76.   );
  77. }
  78.   render () {
  79.     return (
  80.       <Card>
  81.         <CardSection>
  82.           <Input
  83.           placeholder = "user@gmail.com"
  84.           label = 'Email'
  85.           value = {this.state.email}
  86.           onChangeText = {email => this.setState({ email })}
  87.           />
  88.         </CardSection>
  89.  
  90.         <CardSection>
  91.           <Input
  92.             secureTextEntry
  93.             placeholder = "password"
  94.             label = "Password"
  95.             value = {this.state.password}
  96.             onChangeText = {password => this.setState({ password })}
  97.           />
  98.         </CardSection>
  99.         <CardSection>
  100.           <Input
  101.             secureTextEntry
  102.             placeholder = "password"
  103.             label = "Repeat Password"
  104.             value = {this.state.passwordRepeat}
  105.             onChangeText = {passwordRepeat => this.setState({ passwordRepeat })}
  106.           />
  107.         </CardSection>
  108.         <Text style={styles.errorTextStyle}>
  109.           {this.state.error}
  110.         </Text>
  111.         <CardSection>
  112.           {this.renderButton()}
  113.           {this.renderRegisterButton()}
  114.         </CardSection>
  115.       </Card>
  116.     );
  117.   }
  118. }
  119.  
  120. const styles = {
  121.     errorTextStyle: {
  122.       fontSize: 20,
  123.       alignSelf: 'center',
  124.       color: 'red'
  125.     }
  126. }
  127. export default RegisterForm;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement