Register.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { useState } from "react";
  2. import { Form, Row, Col, Button, Alert } from "react-bootstrap";
  3. import { connect } from "react-redux";
  4. import { Link } from "react-router-dom";
  5. import { validateEmail, validatePassword } from "./../helpers/index";
  6. import { actionFullRegister } from "./../actions/types";
  7. const RegisterForm = ({ promise, auth, onRegister }) => {
  8. const [login, setLogin] = useState("");
  9. const [password, setPassword] = useState("");
  10. const [passwordShown, setPasswordShown] = useState(false);
  11. return (
  12. <div className="AuthForm mx-auto mt-5">
  13. <Form>
  14. {login.length === 0 ? null : validateEmail(login) ? (
  15. password.length === 0 ? null : validatePassword(password) ? null : (
  16. <Alert>
  17. Пароль должен быть от 6 символов, иметь хотя бы одну цифру и
  18. заглавную букву.
  19. </Alert>
  20. )
  21. ) : (
  22. <Alert>Email должен быть в формате: email@gmail.com.</Alert>
  23. )}
  24. {Object.keys(auth).length === 0 &&
  25. promise?.registration?.status === "RESOLVED" ? (
  26. <Alert>
  27. Произошла ошибка при регистрации, пожалуйста, повторите ещё раз.
  28. Возможно, такой пользователь уже существует.
  29. </Alert>
  30. ) : null}
  31. <h1 className="text-center">Зарегистрировать аккаунт</h1>
  32. <Form.Group as={Row} className="mb-3" controlId="formHorizontalEmail">
  33. <Form.Label column sm={2}>
  34. Почта:
  35. </Form.Label>
  36. <Col sm={10}>
  37. <Form.Control
  38. type="email"
  39. required
  40. placeholder="Введите вашу почту"
  41. onChange={(e) => setLogin(e.target.value)}
  42. />
  43. </Col>
  44. </Form.Group>
  45. <Form.Group
  46. as={Row}
  47. className="mb-3"
  48. controlId="formHorizontalPassword"
  49. >
  50. <Form.Label column sm={2}>
  51. Пароль:
  52. </Form.Label>
  53. <Col sm={10}>
  54. <Form.Control
  55. type={passwordShown ? "text" : "password"}
  56. placeholder="Введите ваш пароль"
  57. onChange={(e) => setPassword(e.target.value)}
  58. />
  59. <Button
  60. className="mt-2"
  61. variant="secondary"
  62. onClick={() => setPasswordShown(!passwordShown)}
  63. >
  64. {`${passwordShown ? "Скрыть" : "Показать"} пароль`}
  65. </Button>
  66. </Col>
  67. </Form.Group>
  68. <Form.Group as={Row} className="mb-3">
  69. <Col sm={{ span: 10, offset: 2 }} className="my-3">
  70. <Button
  71. id="signupBtn"
  72. variant="success"
  73. disabled={
  74. validateEmail(login) && validatePassword(password)
  75. ? false
  76. : true
  77. }
  78. onClick={() => onRegister(login, password)}
  79. >
  80. Зарегистрироваться
  81. </Button>
  82. </Col>
  83. <Col sm={{ span: 10, offset: 2 }}>
  84. Есть аккаунт?{" "}
  85. <Link className="btn btn-warning" to={"/login"}>
  86. Авторизоваться
  87. </Link>
  88. </Col>
  89. </Form.Group>
  90. </Form>
  91. </div>
  92. );
  93. };
  94. export const CSignUpForm = connect(
  95. (state) => ({ auth: state.auth, promise: state.promise }),
  96. {
  97. onRegister: actionFullRegister,
  98. }
  99. )(RegisterForm);