LoginRegisterLogout.js 5.1 KB

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