registrationPage.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import React, {useState} from 'react';
  2. import {Link} from 'react-router-dom';
  3. import { actionAuthLogin } from '../store/authReducer';
  4. import { store } from '../store/store';
  5. import { connect } from 'react-redux';
  6. import {FontAwesomeIcon} from "@fortawesome/react-fontawesome";
  7. import {faEyeSlash, faEye} from "@fortawesome/free-solid-svg-icons";
  8. import { Button, Form, InputGroup } from 'react-bootstrap';
  9. export async function sendForm (url, data) {
  10. let error = await fetch(`http://player-api/api/${url}`, {
  11. method: 'POST',
  12. body: data
  13. }).then(res => res.json())
  14. .then(data => {
  15. if(data.token) {
  16. store.dispatch(actionAuthLogin(data.token, data.user));
  17. return data
  18. } else {
  19. console.log(data);
  20. return data.login[0];
  21. }
  22. })
  23. return error;
  24. }
  25. export const RegisterForm = ({authState}) => {
  26. const [login, setLogin] = useState('');
  27. const [password, setPassword] = useState('');
  28. const [name, setName] = useState('');
  29. const [passwordConfirm, setPasswordConfirm] = useState('');
  30. const [textModal, setTextModal] = useState('');
  31. const [showPass, setShowPass] = useState(false);
  32. const [showConfPass, setShowConfPass] = useState(false);
  33. const postForm = async (event) =>{
  34. event.preventDefault();
  35. const data = new FormData();
  36. data.append("login", login);
  37. data.append("password", password);
  38. data.append("name", name);
  39. setTextModal(( typeof(await sendForm('register', data))==='string')? (await sendForm('register', data)) : '');
  40. }
  41. return (
  42. <Form onSubmit={postForm} id="register_form">
  43. <div className="register container align-items-center justify-content-center vw-100 vh-100 d-flex">
  44. <div className="col-4">
  45. <h4 className="w-100 text-center">Sign Up</h4>
  46. <hr/>
  47. <Form.Group>
  48. <Form.Label className="form-label">*Login</Form.Label>
  49. <Form.Control type="text" id="login" className='input form-control mb-3' placeholder="asya123"
  50. value={login} onChange={e => setLogin(e.target.value)} />
  51. </Form.Group>
  52. <Form.Group>
  53. <Form.Label className="form-label">*Full Name</Form.Label>
  54. <Form.Control type="text" id="username" className='input form-control mb-3' placeholder="Anastasiia"
  55. value={name} onChange={e => setName(e.target.value)} />
  56. </Form.Group>
  57. <Form.Group>
  58. <Form.Label className="form-label">*Password (8+ symbols)</Form.Label>
  59. <InputGroup>
  60. <Form.Control type={showPass ? "text" : "password"} id="password" className='form-control mb-3'
  61. value={password} onChange={e => setPassword(e.target.value)} />
  62. <InputGroup.Text className='mb-3'>
  63. <FontAwesomeIcon icon={showPass ? faEye : faEyeSlash} onClick={() => showPass? setShowPass(false) : setShowPass(true)}/>
  64. </InputGroup.Text>
  65. </InputGroup>
  66. </Form.Group>
  67. <Form.Group>
  68. <Form.Label className="form-label">*Confirm password</Form.Label>
  69. <InputGroup>
  70. <Form.Control type={showConfPass ? "text" : "password"} id="confirm_password" className='form-control mb-3'
  71. value={passwordConfirm} onChange={e => setPasswordConfirm(e.target.value)} />
  72. <InputGroup.Text className='mb-3'>
  73. <FontAwesomeIcon icon={showConfPass ? faEye : faEyeSlash} onClick={() => showConfPass? setShowConfPass(false) : setShowConfPass(true)}/>
  74. </InputGroup.Text>
  75. </InputGroup>
  76. </Form.Group>
  77. <p className='text-danger'>{textModal ? ('*' + textModal) : ''}</p>
  78. <div className="d-flex justify-content-between align-items-center">
  79. <h6>Already a member? <Link to="/login" className="">Log in</Link></h6>
  80. <Button variant='outline-danger' type="submit" disabled={password !== passwordConfirm || password.length < 8 || login.length < 3} onClick={() => console.log('/user')}>
  81. Sign Up
  82. </Button>
  83. </div>
  84. </div>
  85. </div>
  86. </Form>
  87. )
  88. }
  89. export const CRegisterForm = connect(state => ({ authState: state.auth?.token }))(RegisterForm);