LoginRegisterLogout.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import { actionFullLogin, actionFullRegister, actionClearPromise}
  2. from '../actions'
  3. import React, { useState } from 'react'
  4. import { connect } from 'react-redux'
  5. import { Button, Input, Checkbox, Form } from 'antd'
  6. import { Link } from 'react-router-dom'
  7. import { actionClearUserData } from '../redux/saga'
  8. import { message } from 'antd'
  9. import { useEffect } from 'react'
  10. const LoginForm = ({ onLogin, children, auth,register, onClearPromise }) => {
  11. const [login, setLogin] = useState('')
  12. const [password, setPassword] = useState('')
  13. const [checked, setChecked] = useState(false)
  14. useEffect(() => {
  15. if (auth?.status === 'FULFILLED' && auth?.payload === null) {
  16. message.error({
  17. content: 'You entered wrong login or password',
  18. style: {
  19. marginTop: '80px',
  20. },
  21. })
  22. }
  23. }, [auth])
  24. useEffect(() => {
  25. if (register?.status === 'FULFILLED' && register?.payload === null) {
  26. message.error({
  27. content: 'This login is already in the database',
  28. style: {
  29. marginTop: '80px',
  30. },
  31. })
  32. &&
  33. onClearPromise('register')
  34. }
  35. }, [register])
  36. const input = () => {
  37. onLogin(login, password)
  38. // &&
  39. // onClearPromise('auth')
  40. }
  41. return (
  42. <>
  43. <center>
  44. <Form
  45. className="Form"
  46. size="large"
  47. name="basic"
  48. style={{
  49. marginTop: '200px',
  50. padding: '100px',
  51. width:'50%'
  52. }}
  53. labelCol={{ span: 8 }}
  54. wrapperCol={{ span: 10 }}
  55. initialValues={{ remember: true }}
  56. autoComplete="off"
  57. >
  58. <h1> {children} </h1>
  59. <h1> Login and password must be at least 5 characters </h1>
  60. <Form.Item
  61. label="Login"
  62. name="login"
  63. size="large"
  64. rules={[{ required: true, message: 'Please input login!' }]}
  65. >
  66. <Input
  67. value={login}
  68. size="large"
  69. onChange={(e) => setLogin(e.target.value)}
  70. />
  71. </Form.Item>
  72. <Form.Item
  73. label="Password"
  74. name="password"
  75. size="large"
  76. rules={[{ required: true, message: 'Please input your password!' }]}
  77. >
  78. <Input
  79. size="large"
  80. type={checked ? 'password' : 'text'}
  81. value={password}
  82. onChange={(e) => setPassword(e.target.value)}
  83. />
  84. </Form.Item>
  85. <Form.Item
  86. name="remember"
  87. valuePropName="checked"
  88. wrapperCol={{ offset: 8, span: 10 }}
  89. >
  90. <Checkbox
  91. checked={checked}
  92. onChange={(e) => {
  93. setChecked(e.target.checked)
  94. }}
  95. size="large"
  96. >
  97. See the password
  98. </Checkbox>
  99. </Form.Item>
  100. <Form.Item wrapperCol={{ offset: 8, span: 10 }}>
  101. <Button
  102. size="large"
  103. type="primary"
  104. htmlType="submit"
  105. primary
  106. style={{ width: '100%' }}
  107. className="Btn"
  108. disabled={login.length < 5 || password.length < 5}
  109. onClick={input}
  110. >
  111. {children}
  112. </Button>
  113. </Form.Item>
  114. </Form>
  115. </center>
  116. </>
  117. )
  118. }
  119. export const CLoginForm = connect(
  120. (state) => ({
  121. children: `Sign In`,
  122. auth: state.promise?.auth,
  123. }),
  124. {
  125. onLogin: actionFullLogin,
  126. onClearPromise:actionClearPromise
  127. },
  128. )(LoginForm)
  129. export const CRegisterForm = connect(
  130. (state) => ({
  131. children: `Register`,
  132. register: state.promise?.register
  133. }),
  134. {
  135. onLogin: actionFullRegister,
  136. onClearPromise:actionClearPromise
  137. },
  138. )(LoginForm)
  139. export const CLogout = connect(
  140. (state) => ({
  141. children: `Logout`,
  142. }),
  143. { onClick: actionClearUserData },
  144. )('a')
  145. export const InputForm = ({}) => {
  146. return (
  147. <>
  148. <center>
  149. <div className="InputForm">
  150. <h1>
  151. {' '}
  152. If you have account, click on Sign In,
  153. <br />
  154. else - click on Register
  155. </h1>
  156. <div>
  157. <Link to={`/login`}>
  158. <Button
  159. style={{
  160. margin: '50px',
  161. width: '100px',
  162. boxShadow:
  163. '0 5px 10px rgba(0,0,0,0.25), 0 10px 10px rgba(0,0,0,0.22)',
  164. }}
  165. size="large"
  166. type="primary"
  167. >
  168. Sign In
  169. </Button>
  170. </Link>
  171. <Link to={`/register`}>
  172. <Button
  173. style={{
  174. marginLeft: '100px',
  175. width: '100px',
  176. boxShadow:
  177. '0 5px 10px rgba(0,0,0,0.25), 0 10px 10px rgba(0,0,0,0.22)',
  178. }}
  179. size="large"
  180. type="primary"
  181. >
  182. Register
  183. </Button>
  184. </Link>
  185. </div>
  186. </div>
  187. </center>
  188. </>
  189. )
  190. }
  191. export const CInputForm = connect()(InputForm)