LoginForm.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import React, { Component} from 'react';
  2. import { Text } from 'react-native';
  3. import { connect } from 'react-redux';
  4. import { Card, CardSection, Input, Button, Spinner } from './common';
  5. import { emailChanged, passwordChanged, loginUser } from '../actions/';
  6. import { Actions } from 'react-native-router-flux';
  7. class LoginForm extends Component {
  8. onEmailChange(text) {
  9. this.props.emailChanged(text);
  10. }
  11. onPasswordChange(text){
  12. this.props.passwordChanged(text);
  13. }
  14. onRegisterPress() {
  15. Actions.register();
  16. }
  17. onLoginPress(){
  18. const { email, password } = this.props;
  19. this.props.loginUser({ email, password });
  20. }
  21. renderButton() {
  22. if (this.props.loading) {
  23. return <Spinner size="large" />;
  24. }
  25. return (
  26. <Button onPress={this.onLoginPress.bind(this)}>
  27. Ingresar
  28. </Button>
  29. );
  30. }
  31. render() {
  32. return (
  33. <Card>
  34. <CardSection>
  35. <Input
  36. label="Email"
  37. placeholder="email@gmail.com"
  38. value={this.props.email}
  39. onChangeText={this.onEmailChange.bind(this)}
  40. />
  41. </CardSection>
  42. <CardSection>
  43. <Input
  44. secureTextEntry
  45. label="Clave"
  46. placeholder="clave"
  47. value={this.props.password}
  48. onChangeText={this.onPasswordChange.bind(this)}
  49. />
  50. </CardSection>
  51. <Text style={styles.errorTextStyle}>
  52. {this.props.error}
  53. </Text>
  54. <CardSection>
  55. {this.renderButton()}
  56. </CardSection>
  57. <CardSection>
  58. <Button onPress={this.onRegisterPress.bind(this)}>
  59. Registrarse
  60. </Button>
  61. </CardSection>
  62. </Card>
  63. );
  64. }
  65. }
  66. const styles = {
  67. errorTextStyle: {
  68. fontSize: 20,
  69. alignSelf: 'center',
  70. color: 'red'
  71. }
  72. }
  73. const mapStateToProps = ({ auth }) => {
  74. const { email, password, error, loading } = auth;
  75. return { email, password, error, loading };
  76. };
  77. export default connect(mapStateToProps, {
  78. emailChanged, passwordChanged, loginUser
  79. } )(LoginForm);