Advertisement
Guest User

Untitled

a guest
Feb 9th, 2018
768
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.          } from "react-native";
  16.  
  17.  
  18. export default class App extends React.Component {
  19.  
  20.  
  21.   static navigationOptions = ({ navigation }) => {
  22.     return {
  23.  header: null,
  24. }
  25. }
  26.  
  27.  
  28.  
  29.  constructor(props) {
  30.  
  31.     super(props)
  32.     this.state = {
  33.       error: null,
  34.       UserInput: "",
  35.     }
  36.   }
  37.  
  38.  
  39.   GetValue = () => {
  40.  
  41.   this.setState({ loading: true})
  42.  
  43.   let userstock = this.state.UserInput;
  44.   let path = "https://api.iextrading.com/1.0/stock/";
  45.   let end = "/company";
  46.   let url = path + userstock + end;
  47.  
  48.   fetch(url)
  49.       .then((response) => response.json())
  50.       .then((responseJson) => {
  51.         this.setState({
  52.           symbol: responseJson.symbol,
  53.           company: responseJson.companyName,
  54.           loading: false,
  55.           dataLoaded: true,
  56.         });
  57.       })
  58.       .catch((error) => {
  59.         Alert.alert( 'Symbol not found', 'Please try again.')
  60.         this.setState({loading: false})
  61.       });
  62.       Keyboard.dismiss()
  63.   };
  64.  
  65.   FetchValue = () => {
  66.     AsyncStorage.getItem("Favorites").then((value) => {
  67.       this.setState({
  68.         favs: JSON.parse(value)
  69.       });
  70.     }).done();
  71.   };
  72.  
  73.   SaveValue = () => {
  74.     const newFavs = [...this.state.favs, this.state.UserInput];
  75.     this.setState({ favs: newFavs, UserInput: '' }, () => {
  76.         AsyncStorage.setItem("Favorites", JSON.stringify(this.state.favs));
  77.         Keyboard.dismiss()
  78.     });
  79.   };
  80.  
  81.   RemoveValue(item){
  82.       if(item !== null &&  item !== undefined){
  83.       const index = this.state.favs.indexOf(item);
  84.       const newArray = [...this.state.favs];
  85.       newArray.splice(index,1);
  86.       this.setState({ favs: newArray });
  87.       AsyncStorage.setItem("Favorites", JSON.stringify(newArray));
  88.     };
  89.   };
  90.  
  91.   render() {
  92.     return (
  93.  
  94.         <View style={styles.container}>
  95.  
  96.         <TextInput selectionColor= '#ffffff' style={{
  97.         height: 40, textAlign: 'center',
  98.         color: '#000000', borderWidth: 0, width: 200,
  99.         marginLeft: 20, alignSelf: 'center'
  100.         }}
  101.         onChangeText={(UserInput) => this.setState({UserInput})}
  102.         placeholder= "Search"
  103.         value={this.state.UserInput} />
  104.  
  105.         <TouchableOpacity
  106.         style={styles.button}
  107.         onPress={this.GetValue}>
  108.         <Text style={styles.textbutton}>Search</Text>
  109.         </TouchableOpacity>
  110.  
  111.         {this.state.loading ?
  112.         <View style={styles.loading}>
  113.           <ActivityIndicator
  114.           size="large" color="#222222" />
  115.         </View>
  116.         : null }
  117.  
  118.         <Text style={styles.datatext}>{this.state.company}</Text>
  119.  
  120.         <TouchableOpacity
  121.         style={styles.button}
  122.         onPress={this.SaveValue}>
  123.         <Text style={styles.textbutton}>Save</Text>
  124.         </TouchableOpacity>
  125.  
  126.         <TouchableOpacity
  127.         onPress={this.FetchValue}>
  128.         <Text style={styles.textbutton}>Fetch</Text>
  129.         </TouchableOpacity>
  130.  
  131.         <TouchableOpacity
  132.         onPress={this.RemoveValue}>
  133.         <Text style={styles.textbutton}>Remove</Text>
  134.         </TouchableOpacity>
  135.  
  136.         <FlatList
  137.             data={this.state.favs}
  138.             renderItem={({ item, index }) => (
  139.             <View style={styles.favcontainer}>
  140.               <Text>{item}</Text>
  141.               <TouchableOpacity
  142.               onPress= {(item) => this.RemoveValue(item)}
  143.               >
  144.               <Text>Delete</Text>
  145.               </TouchableOpacity>
  146.             </View>
  147.             )}
  148.           />
  149.  
  150.         </View>
  151.     );
  152.   }
  153. }
  154.  
  155.  
  156. const styles = StyleSheet.create({
  157.   container: {
  158.     margin: 25
  159.   },
  160.   textbutton: {
  161.     fontWeight: '900',
  162.     fontSize: 25,
  163.     margin: 10,
  164.   },
  165.   datatext: {
  166.     fontSize: 20,
  167.      margin: 10,
  168.   },
  169.   loading: {
  170.     margin: 40,
  171.   },
  172.   favcontainer: {
  173.     flex: 1,
  174.     flexDirection: 'row',
  175.     justifyContent: 'space-between',
  176.     alignItems: 'stretch',
  177.     marginTop: 5,
  178.     marginHorizontal: 10,
  179.   },
  180.  
  181. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement