Ingen beskrivning

MocionScreen.js 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import React, { useEffect, useState } from 'react';
  2. import { ActivityIndicator, Button, FlatList, Text, View } from 'react-native';
  3. import { CryptoJS } from "crypto-js/aes";
  4. import { globalStyles } from '../styles/global';
  5. export default App = ({ navigation }) => {
  6. const [mocion, setMocion] = useState(null);
  7. const [description, setDescription] = useState(null);
  8. const [pin, setPIN] = useState(null);
  9. const [response, setResponse] = useState(null);
  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=${navigation.state.params.Pin}`); // connection to the website
  14. const json = await response.json();
  15. setMocion(json.Mocion);
  16. setDescription(json.Description);
  17. setPIN(json.PIN);
  18. } catch (error) {
  19. console.error(error);
  20. }
  21. }
  22. useEffect(() => {
  23. getMociones();
  24. }, []);
  25. // this recieves the value of the button pressed and sends it to the api
  26. const sendVotes = (value) => {
  27. console.log(value); // testing that we recied the value
  28. //sending to the API
  29. const PIN = pin;
  30. const message = 'buenos dias';
  31. const votos = value;
  32. // this is for encrypting the message
  33. var CryptoJS = require('crypto-js');
  34. const privKey = "Zr4u7x!z%C*F-JaN";
  35. const Token = CryptoJS.SHA256(message);
  36. const privKey_encrypt = CryptoJS.SHA256(privKey);
  37. const url = `http://10.190.1.140:5000/vote?PIN=${PIN}&Token=${Token.toString()}&votos=${votos}&privKey_encrypt=${privKey_encrypt.toString()}`;
  38. //connecting to API
  39. fetch(url);
  40. alert("Su voto ha sido procesado!!")
  41. navigation.navigate('Pinpage'); // redirects back to PinPage
  42. console.log(response);
  43. };
  44. // here we want to display each mocion in a flatlist
  45. // it's supposed to be like buttons. Once clicked it would let you vote inside
  46. return (
  47. <View>
  48. <View style={globalStyles.tittleText}>
  49. <Text style={{fontSize: 24}}>{mocion}</Text>
  50. </View>
  51. <View>
  52. <Text style={globalStyles.paragraph}>{description}</Text>
  53. </View>
  54. {/* View container for the buttons style */}
  55. <View style={globalStyles.button}>
  56. <Button title='A favor' color={'#e81b39'} onPress={() => sendVotes('A Favor')}/>
  57. </View>
  58. <View style={globalStyles.button}>
  59. <Button title='En Contra' color={'#e81b39'} onPress={() => sendVotes('En Contra')} />
  60. </View>
  61. <View style={globalStyles.button}>
  62. <Button title='Abstenido/a' color={'#e81b39'} onPress={() => sendVotes('Abstenido/a')} />
  63. </View>
  64. </View>
  65. );
  66. };