Nav apraksta

fetchData.js 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import * from styles
  2. import DropDownPicker from 'react-native-dropdown-picker';
  3. import React, {useState, useEffect} from 'react';
  4. //ALL REQUIRED TO GET DATA
  5. DropDownPicker.setListMode("SCROLLVIEW")
  6. //data parsing goes here
  7. function getInfo(information){
  8. //console.log(information[6])
  9. if(!information)
  10. {Alert.alert("Please try again later! Error retriving data")}
  11. else
  12. {return information}
  13. }
  14. //data display
  15. class DataButton extends React.Component{
  16. state = {
  17. click: false
  18. }
  19. pullData = () => {createQuery(pueblo, table); information = loadDoc(query); this.setState({click: true})}
  20. render() {
  21. return(
  22. <View>
  23. <Button
  24. onPress = {this.pullData}
  25. title = "Fetch Data"
  26. color = "black"
  27. />
  28. <Text style={styles.sub}>{getInfo()}</Text>
  29. </View>
  30. );}
  31. }
  32. function createQuery(pueblo, table) {
  33. const queryBase = "https://api.census.gov/data/2020/acs/acs5/profile?get=group"
  34. const queryEnd = "&in=state:72"
  35. console.log("fetching data")
  36. var query = queryBase + "(" + table + ")&for=county:" + pueblo + queryEnd
  37. if(pueblo && table)
  38. {return query}
  39. else
  40. {Alert.alert("Please fill every parameter before calling!")}
  41. }
  42. function loadDoc(query) {
  43. var xhttp = new XMLHttpRequest();
  44. xhttp.onreadystatechange = function() {
  45. if (this.readyState == 4 && this.status == 200) {
  46. var information = this.responseText;
  47. return information
  48. }
  49. };
  50. xhttp.open("GET", query, true);
  51. xhttp.send();
  52. }