index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import React, { Component } from "react";
  2. import axios from "axios";
  3. import { Redirect, withRouter } from "react-router-dom";
  4. import InputComponent from "../common";
  5. class SignIn extends Component {
  6. componentDidMount() {
  7. const token = localStorage.getItem("token");
  8. if (token) {
  9. this.setState({ token });
  10. }
  11. }
  12. state = {
  13. singIn: {
  14. email: {
  15. value: "",
  16. config: {
  17. type: "email",
  18. name: "email",
  19. placeholder: "Enter your email"
  20. },
  21. valid: false,
  22. touch: false,
  23. validation: {
  24. required: true
  25. }
  26. },
  27. password: {
  28. value: "",
  29. config: {
  30. type: "password",
  31. name: "password",
  32. placeholder: "Enter your password"
  33. },
  34. valid: false,
  35. touch: false,
  36. validation: {
  37. required: true,
  38. minLength: 5
  39. }
  40. }
  41. },
  42. isValid: false,
  43. token: null
  44. };
  45. submit = e => {
  46. e.preventDefault();
  47. const { singIn } = this.state;
  48. // https://test-app-a-level.herokuapp.com
  49. const values = Object.keys(singIn).reduce((prev, elem) => ({ ...prev, [elem]: singIn[elem].value }), {});
  50. console.log("values", values);
  51. axios({
  52. url: "https://test-app-a-level.herokuapp.com/auth/login",
  53. method: "POST",
  54. headers: {
  55. "Content-Type": "application/json"
  56. },
  57. data: values
  58. })
  59. .then(res => {
  60. this.setState({ token: res.data.token });
  61. localStorage.setItem("token", res.data.token);
  62. })
  63. .catch(err => console.log(err.response));
  64. };
  65. validator = (value, rules) => {
  66. let isValid = true;
  67. if (rules.required) {
  68. isValid = value.trim() !== "" && isValid;
  69. }
  70. if (rules.minLength) {
  71. isValid = value.length >= rules.minLength && isValid;
  72. }
  73. return isValid;
  74. };
  75. change = e => {
  76. const { name, value } = e.target;
  77. this.setState(prevState => {
  78. const valid = this.validator(value, prevState.singIn[name].validation);
  79. const otherValid = Object.keys(prevState.singIn).some(el => !prevState.singIn[el].valid);
  80. return {
  81. singIn: {
  82. ...prevState.singIn,
  83. [name]: {
  84. ...prevState.singIn[name],
  85. value,
  86. touch: true,
  87. valid
  88. }
  89. },
  90. isValid: otherValid && valid
  91. };
  92. });
  93. };
  94. render() {
  95. const { singIn, isValid, token } = this.state;
  96. if (token) {
  97. return <Redirect to="/" />;
  98. }
  99. return (
  100. <form className="auth-box__sign-in-form" onSubmit={this.submit}>
  101. {Object.keys(singIn).map(el => (
  102. <InputComponent
  103. key={el}
  104. touch={singIn[el].touch}
  105. valid={!singIn[el].valid}
  106. config={{ ...singIn[el].config, value: singIn[el].value, onChange: this.change }}
  107. />
  108. ))}
  109. <button disabled={!isValid} type="submit">
  110. Sign in
  111. </button>
  112. </form>
  113. );
  114. }
  115. }
  116. export default withRouter(SignIn);