暫無描述

PincodeScreen.js 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import React from "react";
  2. import { StyleSheet, View, Text, Button } from 'react-native';
  3. import { globalStyles } from "../styles/global";
  4. import SmoothPinCodeInput from 'react-native-smooth-pincode-input';
  5. import { useEffect, useState } from "react";
  6. export default class App extends React.Component {
  7. // this checks the pin the user inputs to see if there is a mocion with that pin
  8. getPin = async (code) => {
  9. try {
  10. const response = await fetch(`http://10.0.0.65:5000/send?PIN=${code}`); // connection to the website
  11. const json = await response.json();
  12. console.log(json);
  13. // checks if the mocion exists
  14. if (JSON.stringify(json) == '{"Error":"No hay mocion con ese PIN."}' ){
  15. alert("Error: No hay mocion con ese PIN.")
  16. this.pinInput.current.shake()
  17. .then(() => this.setState({ code: '' }));
  18. } else {
  19. this.setState({ pincode: true});
  20. }
  21. } catch (error) {
  22. console.error(error);
  23. }
  24. }
  25. pressHandler = (code) => {
  26. {/*Dentro del parentesis va el path al screen para redirigir*/}
  27. this.props.navigation.navigate('Mocion', {Pin:code})
  28. this.setState({ pincode: false});
  29. this.setState({ code: '' })
  30. }
  31. state = {
  32. code: '',
  33. pincode: false,
  34. };
  35. pinInput = React.createRef();
  36. render() {
  37. const { code } = this.state;
  38. const { pincode } = this.state;
  39. return (
  40. <View style={globalStyles.container}>
  41. {/* Pin container */}
  42. <View style={globalStyles.section}>
  43. <Text style={globalStyles.titleText}>Entra el Pin de la mocion</Text>
  44. <SmoothPinCodeInput
  45. ref={this.pinInput}
  46. value={code}
  47. codeLength={6}
  48. onTextChange={code => this.setState({ code })}
  49. onFulfill={this.getPin}
  50. onBackspace={() => console.log('No more back.')}
  51. />
  52. </View>
  53. <View style={globalStyles.container}>
  54. { pincode && this.pressHandler(code)}
  55. </View>
  56. </View>
  57. );
  58. }
  59. }