Advertisement
Guest User

Untitled

a guest
Feb 13th, 2018
555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from "react";
  2. import { StatusBar,
  3.          View,
  4.          Text,
  5.          FlatList,
  6.          StyleSheet,
  7.          ActivityIndicator,
  8.          ScrollView,
  9.          Dimensions,
  10.          TouchableOpacity,
  11.          TextInput,
  12.          Alert,
  13.          AsyncStorage,
  14.          Keyboard,
  15.          ListView,
  16.          } from "react-native";
  17. import firebase from 'firebase';
  18.  
  19. export default class App extends React.Component {
  20. constructor(props) {
  21.    super(props)
  22.    this.state = {
  23.      currentUser: '',
  24.      error: null,
  25.      UserInput: "",
  26.      loggedIn: null,
  27.      loggedOut: null,
  28.      email: '', password: '',
  29.    }
  30.  }
  31.  
  32. componentWillMount() {
  33. StatusBar.setHidden(true);
  34. this.setState({ loading: true})
  35. firebase.initializeApp({
  36.   apiKey: "AIzaSyAqpqo8aWpC_sJ18CIUvWYUktGMf8w1ylE",
  37.   authDomain: "course-4895c.firebaseapp.com",
  38.   databaseURL: "https://course-4895c.firebaseio.com",
  39.   projectId: "course-4895c",
  40.   storageBucket: "",
  41.   messagingSenderId: "369023837879"
  42.     });
  43.  
  44.   firebase.auth().onAuthStateChanged((user) => {
  45.     if(user) {
  46.       this.setState({ loggedIn: true, loggedOut: false, loading: false  });
  47.     }
  48.     else {
  49.       this.setState({ loggedIn: false, loggedOut: true, loading: false });
  50.     }
  51.   });
  52. }
  53.  
  54. GetValue = () => {
  55.  
  56. this.setState({ loading: true})
  57.  
  58. let userstock = this.state.UserInput;
  59. let path = "https://api.iextrading.com/1.0/stock/";
  60. let end = "/company";
  61. let url = path + userstock + end;
  62.  
  63. fetch(url)
  64.     .then((response) => response.json())
  65.     .then((responseJson) => {
  66.       this.setState({
  67.         symbol: responseJson.symbol,
  68.         company: responseJson.companyName,
  69.         loading: false,
  70.         dataLoaded: true,
  71.       });
  72.     })
  73.     .catch((error) => {
  74.       Alert.alert( 'Symbol not found', 'Please try again.')
  75.       this.setState({loading: false})
  76.     });
  77.     Keyboard.dismiss()
  78. };
  79.  
  80.  
  81. onLogIn = () => {
  82.   const { email, password } = this.state;
  83.   this.setState({ error: '', loading: true });
  84.   Keyboard.dismiss()
  85.   firebase.auth().signInWithEmailAndPassword(email, password)
  86.   .then(this.onSuccess.bind(this))
  87.   .catch(() => {
  88.     alert('incorrect username or password')
  89.   })
  90. };
  91.  
  92. onSignUp = () => {
  93.   const { email, password } = this.state;
  94.   this.setState({ error: '', loading: true });
  95.   Keyboard.dismiss()
  96.   firebase.auth().createUserWithEmailAndPassword(email, password)
  97.   .then(this.onSuccess.bind(this))
  98.   .catch(() => {
  99.     alert('incorrect username or password')
  100.     this.setState({loading: false})
  101.   })
  102. };
  103.  
  104. onLogOut() {
  105.   firebase.auth().signOut()
  106. };
  107.  
  108. onSuccess() {
  109.   this.setState({
  110.     email: '',
  111.     password: '',
  112.     loading: false,
  113.     error: ''
  114.   });
  115. };
  116.  
  117. saveData = () => {
  118.   this.setState({ loading: true})
  119.   let favdata = this.state.company;
  120.   const { currentUser } = firebase.auth();
  121.   firebase.database().ref(`/users/${currentUser.uid}/favorites/`)
  122.    .push({ favdata })
  123.    .then(() => {
  124.       this.setState({ loading: false})
  125.       alert('Data saved!')
  126.   });
  127. }
  128.  
  129. FetchFav = () => {
  130.   const { currentUser } = firebase.auth();
  131.   firebase.database().ref(`/users/${currentUser.uid}/favorites/`)
  132.     .once('value', snapshot => {
  133.       const favs = snapshot.val();
  134.       const array = Object.keys(favs).map(function (key) { return favs[key]; });
  135.       console.log(array);
  136.     });
  137. }
  138.  
  139.   render() {
  140.     const { currentUser } = firebase.auth();
  141.     return (
  142.  
  143.       <View style={styles.container}>
  144.  
  145.       <TextInput selectionColor= '#ffffff' style={{
  146.        height: 40, textAlign: 'center',
  147.        color: '#000000', borderWidth: 0, width: 200,
  148.        marginLeft: 20, alignSelf: 'center'
  149.        }}
  150.        onChangeText={(UserInput) => this.setState({UserInput})}
  151.        placeholder= "Stock Symbol Engine"
  152.        value={this.state.UserInput} />
  153.  
  154.        <TouchableOpacity
  155.        style={styles.button}
  156.        onPress={this.GetValue}>
  157.        <Text style={styles.textbutton}>Search</Text>
  158.        </TouchableOpacity>
  159.  
  160.        <TouchableOpacity
  161.        style={styles.button}
  162.        onPress={this.FetchFav}>
  163.        <Text style={styles.textbutton}>Favorites</Text>
  164.        </TouchableOpacity>
  165.  
  166.        {this.state.loggedIn ?
  167.        <TouchableOpacity
  168.        style={styles.button}
  169.        onPress={this.saveData.bind(this)}>
  170.        <Text style={styles.textbutton}>Save</Text>
  171.        </TouchableOpacity>
  172.        : null }
  173.  
  174.       {this.state.loading ?
  175.       <View style={styles.loading}>
  176.         <ActivityIndicator
  177.         size="large" color="#222222" />
  178.       </View>
  179.       : null }
  180.  
  181.       <Text style={styles.datatext}>{this.state.company}</Text>
  182.  
  183.  
  184.       {this.state.loggedIn ?
  185.       <Text style={styles.datatext}>Hello {currentUser.email}</Text>
  186.       : null }
  187.  
  188.       {this.state.loggedOut ?
  189.        <View>
  190.          <TextInput
  191.            placeholder="user@mail.com"
  192.            label="Email"
  193.            value={this.state.email}
  194.            onChangeText={email => this.setState({ email })}
  195.          />
  196.  
  197.          <TextInput
  198.            secureTextEntry={true}
  199.            placeholder="password"
  200.            label="Password"
  201.            value={this.state.password}
  202.            onChangeText={password => this.setState({ password })}
  203.          />
  204.        </View>
  205.        : null }
  206.  
  207.       {this.state.loggedOut ?
  208.       <View style={styles.authbtn}>
  209.         <TouchableOpacity
  210.           style={styles.button}
  211.           onPress={this.onLogIn}>
  212.           <Text style={styles.textbutton}>Log In</Text>
  213.         </TouchableOpacity>
  214.  
  215.         <TouchableOpacity
  216.           style={styles.button}
  217.           onPress={this.onSignUp}>
  218.           <Text style={styles.textbutton}>Sign Up</Text>
  219.         </TouchableOpacity>
  220.       </View>
  221.       : null }
  222.  
  223.  
  224.       {this.state.loggedIn ?
  225.       <TouchableOpacity
  226.       style={styles.button}
  227.       onPress={this.onLogOut}>
  228.       <Text style={styles.textbutton}>Log Out</Text>
  229.       </TouchableOpacity>
  230.       : null }
  231.  
  232.      </View>
  233.     );
  234.   }
  235. }
  236.  
  237. const styles = StyleSheet.create({
  238.   container: {
  239.     margin: 25,
  240.     flex: 1,
  241.   },
  242.   textbutton: {
  243.     fontWeight: '400',
  244.     fontSize: 18,
  245.     margin: 5,
  246.     backgroundColor: '#0c1c2c',
  247.     paddingHorizontal: 60,
  248.     paddingVertical: 7,
  249.     textAlign: 'center',
  250.     color: '#40b2a4'
  251.   },
  252.   datatext: {
  253.     fontSize: 20,
  254.      margin: 10,
  255.   },
  256.   loading: {
  257.     margin: 40,
  258.   },
  259.   authbtn:{
  260.     flexDirection : 'row',
  261.     alignItems: 'stretch',
  262.     justifyContent: 'space-between',
  263.   },
  264. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement