LoginRegisterLogout.js 5.0 KB

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