Advertisement
Guest User

Untitled

a guest
Nov 18th, 2018
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. Login ---Component---
  2.  
  3. import React, { Component } from "react";
  4. import { withRouter } from "react-router-dom";
  5. import PropTypes from "prop-types";
  6. import { connect } from "react-redux";
  7. import { loginAction } from "../actions/loginActions";
  8. import "bootstrap/dist/css/bootstrap.min.css";
  9. import "../style/signin.css";
  10.  
  11. export class LoginForm extends Component {
  12. state = {
  13. user: {
  14. email: "",
  15. password: ""
  16. },
  17. status: "none"
  18. };
  19.  
  20. onChange = e => {
  21. this.setState({ [e.target.name]: e.target.value });
  22. };
  23.  
  24. handleSubmit = e => {
  25. e.preventDefault();
  26. const userdata = {
  27. user: {
  28. email: this.state.email,
  29. password: this.state.password
  30. }
  31. };
  32. this.props.loginAction(userdata).then(response => {
  33. if (response.success) {
  34. const { history } = this.props;
  35. history.push("/homepage");
  36. }
  37. });
  38. };
  39.  
  40. listErrors = message => {
  41. const errors = message;
  42. const msg = [];
  43. msg.push(<hr />);
  44. for (var key in errors) {
  45. msg.push(
  46. <span key={key}>
  47. {`${key}: ${errors[key]}`}
  48. <br />
  49. </span>
  50. );
  51. }
  52. return msg;
  53. };
  54.  
  55. toggleModal = () => {
  56. this.props.toggleModal();
  57. };
  58.  
  59. render() {
  60. const { message, status } = this.props;
  61. return (
  62. <div>
  63. <div className="auth-form">
  64. <form onSubmit={this.handleSubmit} id="login-form">
  65. <h1 className="centred">Login</h1>
  66. <br />
  67. <div className="form-group row">
  68. <input
  69. className="form-control auth-input"
  70. type="text"
  71. name="email"
  72. id="email"
  73. placeholder="Enter email here"
  74. onChange={this.onChange}
  75. />
  76. </div>
  77. <div className="form-group row">
  78. <input
  79. className="form-control auth-input"
  80. type="password"
  81. name="password"
  82. id="password"
  83. placeholder="Enter password here"
  84. onChange={this.onChange}
  85. />
  86. </div>
  87. <button type="submit" className="btn btn-submit" id="submit">
  88. Submit
  89. </button>
  90. <br />
  91. <p className="small-text">
  92. <u>
  93. <a href="/homepage">forgot password?</a>
  94. </u>{" "}
  95. /{" "}
  96. <u>
  97. <a href="/users/">don't have an account?</a>
  98. </u>
  99. </p>
  100. <hr />
  101. <button type="button" className="btn btn-block google">
  102. Login with Google
  103. </button>
  104. <br />
  105. <button type="button" className="btn btn-block facebook">
  106. Login with Facebook
  107. </button>
  108. <br />
  109. <button type="button" className="btn btn-block twitter">
  110. Login with Twitter
  111. </button>
  112. </form>
  113. <div className="auth-error">
  114. <b>
  115. {status === "error"
  116. ? this.listErrors(message)
  117. : status === "loading"
  118. ? this.toggleModal()
  119. : null}
  120. </b>
  121. </div>
  122. </div>
  123. </div>
  124. );
  125. }
  126. }
  127.  
  128. LoginForm.propTypes = {
  129. loginAction: PropTypes.func.isRequired,
  130. email: PropTypes.string.isRequired,
  131. password: PropTypes.string.isRequired
  132. };
  133.  
  134. const mapStateToProps = state => ({
  135. user: state.login.user,
  136. message: state.login.message,
  137. status: state.login.status
  138. });
  139.  
  140. export default withRouter(
  141. connect(
  142. mapStateToProps,
  143. { loginAction }
  144. )(LoginForm)
  145. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement