Nenhuma descrição

MocionScreen.js 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 = 'abc123';
  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. console.log(response);
  41. };
  42. // here we want to display each mocion in a flatlist
  43. // it's supposed to be like buttons. Once clicked it would let you vote inside
  44. return (
  45. <View style={{ flex: 1, padding: 24 }}>
  46. <Text>{mocion}</Text>
  47. <Text>{description}</Text>
  48. <Button title='A favor' onPress={() => sendVotes('A Favor')}/>
  49. <Button title='En Contra'onPress={() => sendVotes('En Contra')} />
  50. <Button title='Abstenido/a' onPress={() => sendVotes('Abstenido/a')} />
  51. </View>
  52. );
  53. };