Geen omschrijving

MocionScreen.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import React, { useEffect, useState } from 'react';
  2. import { ActivityIndicator, FlatList, Text, TouchableOpacity, View } from 'react-native';
  3. import { globalStyles } from '../styles/global';
  4. export default App = () => {
  5. const [isLoading, setLoading] = useState(true);
  6. const [Description, setDescription] = useState([]); // this is looking for 'Description' and it's content
  7. const [Mocion, setMocion] = useState([]); // this is looking for 'Mocion' and it's content
  8. const [PIN, setPIN] = useState([]); // this is looking for 'PIN' and it's content
  9. const [Votos, setVotos] = useState(0); // this is looking for 'Votos' and it's content
  10. // this connects us to the API and fetches the json file with the mociones
  11. const getMociones = async () => {
  12. try {
  13. const response = await fetch('http://10.190.1.140:5000/send?PIN=121071'); // connection to the website
  14. const json = await response.json();
  15. // setting the content of each category
  16. setMocion(json.Mocion);
  17. setDescription(json.Description);
  18. setPIN(json.PIN);
  19. setVotos(json.Votos);
  20. } catch (error) {
  21. console.error(error);
  22. } finally {
  23. setLoading(false); // once found the loading icon will be replaced with the content of the json
  24. }
  25. }
  26. function getVotosNums() {
  27. const [countFavor, setCountFavor] = useState(0);
  28. const [countContra, setCountContra] = useState(0);
  29. const [countAbstenid, setCountAbstenid] = useState(0);
  30. return (
  31. <div>
  32. <button onClick={() => setCountFavor(countFavor + 1)}>
  33. A favor
  34. </button>
  35. <button onClick={() => setCountContra(countContra + 1)}>
  36. En contra
  37. </button>
  38. <button onClick={() => setCountAbstenid(countAbstenid + 1)}>
  39. Abstenido
  40. </button>
  41. </div>
  42. );
  43. }
  44. const getVotos = async () => {
  45. try {
  46. const response = await fetch('http://10.190.1.140:5000/send?PIN=121071'); // connection to the website
  47. const json = await response.json();
  48. // Getting votes count
  49. getVotoNums();
  50. } catch (error) {
  51. console.error(error);
  52. } finally {
  53. setLoading(false); // once found the loading icon will be replaced with the content of the json
  54. }
  55. }
  56. useEffect(() => {
  57. getMociones();
  58. getVotos();
  59. }, []);
  60. // this is for displaying the mocion, its description, and votes' options on the screen
  61. return (
  62. <View style={{ flex: 1, padding: 24 }}>
  63. <Text>{Mocion}</Text>
  64. <Text>{Description}</Text>
  65. <Button>
  66. title = "A favor"
  67. onPress={() => Alert.alert('Voto a favor')}
  68. </Button>
  69. <Button>
  70. title = "En contra"
  71. onPress={() => Alert.alert('Voto en contra')}
  72. </Button>
  73. <Button>
  74. title = "Abstenido"
  75. onPress={() => Alert.alert('Se abstuvo al voto')}
  76. </Button>
  77. </View>
  78. );
  79. };