RegisterForm.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import React, { useState, useEffect } from "react";
  2. import { connect } from "react-redux";
  3. import { Form, Input, Button, Checkbox } from "antd";
  4. import { UserOutlined, LockOutlined } from "@ant-design/icons";
  5. import { Link } from 'react-router-dom';
  6. import { doLogin, newUser } from "../actions/authActions";
  7. import history from "../utils/history";
  8. // import { logOut } from "../actions/authActions";
  9. // import { BrowserRouter as Router, Route } from 'react-router-dom';
  10. // import {getGQL, url, headers }from "../utils/getGQL";
  11. import {getGQL }from "../utils/getGQL";
  12. import "../styles/RegisterForm.css";
  13. // const history = createHistory()
  14. const RegisterForm = ({store, history, newUser, state, id, doLogOut, doLogin }) => {
  15. const [login, setLogin] = useState("");
  16. const [password, setPassword] = useState("");
  17. const [confPassword, setConfPassword] = useState("");
  18. const [form] = Form.useForm();
  19. useEffect (() => {
  20. // newUser && newUser(login, password);
  21. if(id){
  22. history.push("/my_playlists")
  23. }
  24. console.log(login, password);
  25. }, [id])
  26. const onFinish = (values) => {
  27. // console.log("Success:", values);
  28. };
  29. const onFinishFailed = (errorInfo) => {
  30. // console.log("Failed:", errorInfo);
  31. };
  32. // console.log('state: ',state)
  33. // console.log('store: ',store)
  34. return (
  35. <div className="registerForm">
  36. {
  37. console.log('state: ',state),
  38. console.log('id: ',state.auth._id)
  39. }
  40. <div>
  41. <Form
  42. name="basic"
  43. form={form}
  44. onFinish={onFinish}
  45. onFinishFailed={onFinishFailed}
  46. >
  47. <Form.Item onChange={(e)=> setLogin(e.target.value)}
  48. label="Username"
  49. name="username"
  50. value={login}
  51. rules={[
  52. {
  53. required: true,
  54. message: "Please input your username!",
  55. },
  56. ]}
  57. >
  58. <Input
  59. prefix={<UserOutlined className="site-form-item-icon" />}
  60. onChange={(e) => {
  61. form.setFieldsValue({ username: e.target.value });
  62. }}
  63. />
  64. </Form.Item>
  65. <Form.Item onChange={(e)=> setPassword(e.target.value)}
  66. label="Password"
  67. name="password"
  68. value={password}
  69. rules={[
  70. {
  71. required: true,
  72. message: "Please input your password!",
  73. },
  74. ]}
  75. >
  76. <Input.Password
  77. prefix={<LockOutlined className="site-form-item-icon" />}
  78. />
  79. </Form.Item>
  80. <Form.Item onChange={(e)=> setConfPassword(e.target.value)}
  81. label="Confirm password"
  82. name="Confirm password"
  83. value={confPassword}
  84. rules={[
  85. {
  86. required: true,
  87. message: "Please input your password!",
  88. },
  89. ]}
  90. >
  91. <Input.Password
  92. prefix={<LockOutlined className="site-form-item-icon" />}
  93. />
  94. </Form.Item>
  95. <Form.Item>
  96. <Button type="primary" htmlType="submit"
  97. disabled = {password !== confPassword}
  98. onClick={() => (login && password)? newUser(login, password) && console.log('state: ',state) : ''}
  99. // if (!response.errors && login && password) {
  100. // useEffect(doLogin(login, password), [])
  101. // doLogin && doLogin(login, password)? history.push("/private"): alert('Do not correct doLogin use');
  102. // } else {
  103. // }
  104. // async () => {
  105. // let response = await getGQL("/graphql")(
  106. // // let response = await getGQL(url)(
  107. // `mutation User{
  108. // createUser (login: "${login}", password: "${password}"){
  109. // _id login
  110. // }
  111. // }`
  112. // )
  113. // .then((response) => {
  114. // console.log(response.data.createUser);
  115. // if (!response.errors && login && password) {
  116. // history.push("/login");
  117. // } else {
  118. // }
  119. // });
  120. // }
  121. // }
  122. type="primary"
  123. >
  124. Submit
  125. </Button>
  126. <Link to="/" className="link-approval">I have an account</Link>
  127. {/* <Link to="/login" className="link-approval">Sign_in</Link> */}
  128. </Form.Item>
  129. </Form>
  130. </div>
  131. </div>
  132. );
  133. };
  134. const mapStateToProps = (state) => ({
  135. isCreateUser: !!state.auth.token,
  136. state: state,
  137. id: state && state.auth && state.auth.data && state.auth.data.data && state.auth.data.data.sub && state.auth.data.data.sub.id,
  138. });
  139. const mapDispatchToProps = (dispatch) => ({
  140. newUser: (login, password) => dispatch(newUser(login, password)),
  141. // dologOut: logOut,
  142. });
  143. export default connect(mapStateToProps, mapDispatchToProps)(RegisterForm);
  144. // export default RegisterForm;