loginOnPage.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import {BrowserRouter as Router, Route, Link, Switch, Redirect, useHistory} from 'react-router-dom';
  2. import {Provider, connect} from 'react-redux';
  3. import React, {Component, useState, useEffect} from 'react';
  4. import {createStore, combineReducers, applyMiddleware} from 'redux';
  5. import thunk from 'redux-thunk';
  6. import jwt_decode from "jwt-decode"
  7. import { bindActionCreators } from 'redux';
  8. import {actionLogin, authReducer, actionAuthLogout, actionReg} from "../reducers/";
  9. const ButtonLogout = ({onLogout, isLoggedIn}) =>
  10. <button onClick={onLogout}
  11. disabled={!isLoggedIn}>Logout</button>
  12. const CButtonLogout = connect(s => ({isLoggedIn: s.auth.payload}),{onLogout: actionAuthLogout})(ButtonLogout)
  13. // const DashboardLink = ({login}) =>{
  14. // console.log(login)
  15. // return(
  16. // <div className = "DashboardLink">{login ? <Link to='/dashboard'>{login}</Link> : <Link to='/login'>Anon</Link>}</div>
  17. // )
  18. // }
  19. // const CDashboardLink = connect(s => ({login: s.auth.payload && s.auth.payload.sub.login, }))(DashboardLink)
  20. const PasswordConfirm = ({state, login, isLoggedIn, onLogin = null }) => {
  21. const [pass1, setPass1] = useState("");
  22. const [pass2, setPass2] = useState("");
  23. const history = useHistory();
  24. useEffect(() => login ? history.push(`/profile/` + login) : history.push(`/login`),[login])
  25. console.log(state)
  26. return (
  27. <>
  28. <div>
  29. <input
  30. type="text"
  31. placeholder = "login"
  32. onChange={(e) => {
  33. setPass1(e.target.value);
  34. }}
  35. onKeyDown = {(e) => e.keyCode == 13 && onLogin(pass1, pass2)}
  36. />
  37. <input
  38. type="password"
  39. placeholder = "password"
  40. onChange={(e) => {
  41. setPass2(e.target.value);
  42. }}
  43. onKeyDown = {(e) => e.keyCode == 13 && onLogin(pass1, pass2)}
  44. />
  45. </div>
  46. <div>
  47. <button disabled = {isLoggedIn} onClick={() => onLogin(pass1, pass2)}>Login</button>
  48. <CButtonLogout />
  49. </div>
  50. </>
  51. );
  52. };
  53. const FormReg = ({onReg, onLogin, login, state, isReg}) => {
  54. const [pass1, setPass1] = useState("");
  55. const [pass2, setPass2] = useState("");
  56. const history = useHistory();
  57. useEffect(() => login ? history.push(`/profile/` + login) : history.push(`/login`),[login])
  58. console.log(isReg)
  59. return (
  60. <>
  61. <div>
  62. <input
  63. type="text"
  64. placeholder = "login"
  65. onChange={(e) => {
  66. setPass1(e.target.value);
  67. }}
  68. onKeyDown = {(e) => {
  69. if (e.keyCode == 13 ) {
  70. onReg(pass1, pass2)
  71. if(!isReg) {
  72. onLogin(pass1, pass2)
  73. } else {
  74. history.push('/loginError')
  75. }
  76. }
  77. }}
  78. />
  79. <input
  80. type="password"
  81. placeholder = "password"
  82. onChange={(e) => {
  83. setPass2(e.target.value);
  84. }}
  85. onKeyDown = {(e) => {
  86. if (e.keyCode == 13 ) {
  87. onReg(pass1, pass2)
  88. debugger
  89. if(!isReg) {
  90. onLogin(pass1, pass2)
  91. } else {
  92. history.push('/loginError')
  93. }
  94. }
  95. }}
  96. />
  97. </div>
  98. <div>
  99. <button disabled = {!pass2 || !pass1} onClick={() => {
  100. onReg(pass1, pass2)
  101. debugger
  102. if(!isReg) {
  103. onLogin(pass1, pass2)
  104. } else {
  105. history.push('/loginError')
  106. }
  107. }
  108. }>Запишіть</button>
  109. </div>
  110. </>
  111. )
  112. }
  113. const CFormReg = connect((state) => ({
  114. state: state,
  115. login: state.auth.payload && state.auth.payload.sub.login,
  116. isReg: state.promiseRed && state.promiseRed.registration &&
  117. state.promiseRed.registration.payload &&
  118. state.promiseRed.registration.payload.errors
  119. }), {
  120. onReg:actionReg,
  121. onLogin:actionLogin})(FormReg);
  122. const CFormLogin = connect((s) => ({state: s, isLoggedIn: s.auth.payload, login: s.auth.payload && s.auth.payload.sub.login}), {
  123. onLogin:actionLogin})(PasswordConfirm);
  124. // const LoginError = () => {
  125. // const history = useHistory();
  126. // return(
  127. // <>
  128. // <div>Упс, щось пішло не так. Можливо це ім'я вже зайнято. Спробуйте ще.</div>
  129. // <button onClick = {() => history.push("/login")}>Перейти на сторінку регістрації</button>
  130. // </>
  131. // )}
  132. const LoginForm = ({}) => {
  133. return(
  134. <>
  135. <div className = "loginWrapper">
  136. <h2>Будь ласка, увійдіть в свій аккаунд</h2>
  137. <div className = "loginForm">
  138. {/* <CDashboardLink/> */}
  139. <CFormLogin />
  140. </div>
  141. <h2>Не маєте аккаунта? То не біда, зареєструйтесь</h2>
  142. <div className = "loginForm">
  143. <CFormReg/>
  144. </div>
  145. </div>
  146. </>
  147. )
  148. }
  149. export default LoginForm;